奇怪的是我得到了这个例外。
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity.
我在网上搜索了这个,但没有一个解决方案帮助了我。
这是我的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;
}
});
}
});
答案 0 :(得分:2)
构建对话框时,请使用活动上下文而不是应用程序上下文:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getApplicationContext());
例如,如果您的代码在活动中:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);