非法状态异常错误-Android
Illegal State Exception error-Android
奇怪的是我得到了这个异常。
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity.
我在网上搜索了这个,但 none 的解决方案对我有帮助。
这是我的 styles.xml 文件。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textColor">#fff</item>
</style>
清单文件的一部分是。
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
添加警报对话框代码后发生此异常。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(),"It's me",Toast.LENGTH_SHORT).show();
PopupMenu homepopup = new PopupMenu(getApplicationContext(), view);
MenuInflater inflater = homepopup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, homepopup.getMenu());
homepopup.show();
homepopup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
InvitationBoard.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
return true;
}
});
}
});
构建对话框时,使用 activity 上下文而不是应用程序上下文:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
例如,如果您的代码位于 activity:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
奇怪的是我得到了这个异常。
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity.
我在网上搜索了这个,但 none 的解决方案对我有帮助。
这是我的 styles.xml 文件。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:textColor">#fff</item>
</style>
清单文件的一部分是。
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
添加警报对话框代码后发生此异常。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(),"It's me",Toast.LENGTH_SHORT).show();
PopupMenu homepopup = new PopupMenu(getApplicationContext(), view);
MenuInflater inflater = homepopup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, homepopup.getMenu());
homepopup.show();
homepopup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
InvitationBoard.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
return true;
}
});
}
});
构建对话框时,使用 activity 上下文而不是应用程序上下文:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
例如,如果您的代码位于 activity:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);