无法在 ActionBarActivity 中创建 alertDialog
Unable create alertDialog in ActionBarActivity
我有一个扩展 ActionBarActivity
的 Activity
。每当我尝试在其中创建 AlertDialog
时,都会在创建对话框的行崩溃并给出此错误
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
但我已经在使用 Appcompat
主题 Theme.AppCompat.Light.NoActionBar
,因为我正在使用工具栏。这可能是什么原因?
这是我的 activity:
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 根本没有帮助。
<activity
android:name=".MyActivity"
android:label="@string/myactivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
>
使用 AppCompatActivity 而不是 ActionBarActivity
public class MyActivity extends AppCompatActivity
我无法重现完全相同的错误。
但是,我认为问题是传递给 AlertDialog.Builder 构造函数的上下文。事实上,应该向它传递一个 activity 上下文。
尝试替换此行
alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
用这个
alertDialog = new AlertDialog.Builder(this).create();
如果这能解决问题,请告诉我。
我通过更改导入解决了这个问题:
android.support.v7.app.AlertDialog
to
android.app.AlertDialog
改变
new AlertDialog.Builder(this)
至
new AlertDialog.Builder(MainActivity.this)
如果您在适配器和 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。
通过即可解决。 Activityname.this
代替 getApplicationContext();
就像你的情况
alertDialog = new AlertDialog.Builder(MyActivity.this)).create();
将此行添加到 AndroidManifest.xml
文件
中的 <application>
android:theme="@style/Theme.AppCompat.Light"
我有一个扩展 ActionBarActivity
的 Activity
。每当我尝试在其中创建 AlertDialog
时,都会在创建对话框的行崩溃并给出此错误
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
但我已经在使用 Appcompat
主题 Theme.AppCompat.Light.NoActionBar
,因为我正在使用工具栏。这可能是什么原因?
这是我的 activity:
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 根本没有帮助。
<activity
android:name=".MyActivity"
android:label="@string/myactivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
>
使用 AppCompatActivity 而不是 ActionBarActivity
public class MyActivity extends AppCompatActivity
我无法重现完全相同的错误。 但是,我认为问题是传递给 AlertDialog.Builder 构造函数的上下文。事实上,应该向它传递一个 activity 上下文。
尝试替换此行
alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
用这个
alertDialog = new AlertDialog.Builder(this).create();
如果这能解决问题,请告诉我。
我通过更改导入解决了这个问题:
android.support.v7.app.AlertDialog
to
android.app.AlertDialog
改变
new AlertDialog.Builder(this)
至
new AlertDialog.Builder(MainActivity.this)
如果您在适配器和 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。
通过即可解决。 Activityname.this
代替 getApplicationContext();
就像你的情况
alertDialog = new AlertDialog.Builder(MyActivity.this)).create();
将此行添加到 AndroidManifest.xml
文件
<application>
android:theme="@style/Theme.AppCompat.Light"