【问题标题】:Unable create alertDialog in ActionBarActivity无法在 ActionBarActivity 中创建 alertDialog
【发布时间】:2015-07-06 08:43:26
【问题描述】:

我有一个Activity,它扩展了ActionBarActivity。每当我尝试在其中创建 AlertDialog 时,都会在创建对话框的行崩溃并给出此错误

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

但我已经在使用Appcompat 主题Theme.AppCompat.Light.NoActionBar,因为我正在使用工具栏。这可能是什么原因? 这是我的活动:

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;



public class MyActivity extends ActionBarActivity {

    Toolbar toolbar;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);

        toolbar = (Toolbar)findViewById(R.id.tool_bar_comment);
        setSupportActionBar(toolbar);
                AlertDialog alertDialog;
                alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
                alertDialog.setTitle("Network error");
                alertDialog.setMessage("Check network connection and try again.");
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
}
      });
                  alertDialog.show();


       }
  }

这是我的主要文件:

 <application
        android:name=".Networking.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    ...



         <activity
        android:name=".MyActivity"
        android:label="@string/myactivity"
        >
 </activity>
</application>

还有,这里是styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

向 MainFest 中的活动添加 android:theme 属性根本没有帮助。

<activity
        android:name=".MyActivity"
        android:label="@string/myactivity"
        android:theme="@style/Theme.AppCompat.NoActionBar"
        >

【问题讨论】:

标签: android android-alertdialog android-appcompat android-theme android-actionbar-compat


【解决方案1】:

我无法重现完全相同的错误。 但是,我认为问题在于传递给 AlertDialog.Builder 构造函数的上下文。其实应该给它传递一个activity的Context。

尝试替换此行

                alertDialog = new AlertDialog.Builder(getApplicationContext()).create();

这个

                alertDialog = new AlertDialog.Builder(this).create();

如果这能解决问题,请告诉我。

【讨论】:

  • 很高兴听到这个消息!您还可以阅读此post 以更好地了解何时使用 Activity 或 Application 上下文
【解决方案2】:

我通过更改导入解决了这个问题:

 android.support.v7.app.AlertDialog 

 to 

 android.app.AlertDialog

【讨论】:

    【解决方案3】:

    改变

    new AlertDialog.Builder(this)
    

    new AlertDialog.Builder(MainActivity.this)
    

    【讨论】:

      【解决方案4】:

      如果您在适配器和 OnBindViewHolder() 方法中使用 alertDialog,如下代码所示:

      @Override
      public void onBindViewHolder(final UserListAdapter.ViewHolder holder, final int position) {
      
      //long click for delete the record
      holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View view) {
      
              new AlertDialog.Builder(mContext)
      
                      .setMessage("Are you want to delete?")
                      .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
                              Log.d(TAG, "onClick: yes");
                          }
                      })
                      .setNegativeButton("No", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
                              Log.d(TAG, "onClick: no");
                          }
                      })
                      .setCancelable(false)
                      .show();
              return true;
          }
      });
      

      }

      在这种情况下 new AlertDialog.Builder(context) 不能正常工作

      所以您需要将其替换为 new AlertDialog.Builder(view.getContext())。 这对我有用,ty。

      【讨论】:

        【解决方案5】:

        可以通过传递来解决。 Activityname.this 代替 getApplicationContext(); 就像你的情况 alertDialog = new AlertDialog.Builder(MyActivity.this)).create();

        【讨论】:

          【解决方案6】:

          将此行添加到&lt;application&gt; 文件中的AndroidManifest.xml

          android:theme="@style/Theme.AppCompat.Light"
          

          【讨论】:

            【解决方案7】:

            使用 AppCompatActivity 代替 ActionBarActivity

            public class MyActivity extends AppCompatActivity
            

            【讨论】:

            • 它不会改变任何东西 :-(
            猜你喜欢
            • 1970-01-01
            • 2014-01-09
            • 1970-01-01
            • 2011-03-29
            • 1970-01-01
            • 2015-11-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多