java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)

时间:2016-02-06 07:19:20

标签: java android android-appcompat android-theme illegalstateexception

我正在尝试在删除按钮上使用警告对话框。但显示对话框显示异常。当我点击删除时,它崩溃并在.show上显示异常。

我尝试将Theme.AppCompat主题用于此活动,但仍然崩溃。

linux-vdso.so.1 =>  (0x00007ffc7bd42000)
libpython3.5m.so.1.0 => /usr/lib/libpython3.5m.so.1.0 (0x00007f2e42451000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2e42234000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f2e4202f000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f2e41e2c000)
librt.so.1 => /lib64/librt.so.1 (0x00007f2e41c24000)
libm.so.6 => /lib64/libm.so.6 (0x00007f2e4199f000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2e4160b000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2e42b83000)

我的主题

   <activity android:name=".AddEventActivity"
        android:theme="@style/Theme.AppCompat">
    </activity>


        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new AlertDialog.Builder(getApplicationContext())
                        .setTitle("Delete entry")
                        .setMessage("Are you sure you want to delete this entry?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                Toast.makeText(getApplicationContext(), "Yaay", Toast.LENGTH_SHORT).show();
                                i = new Intent();
                                db.deleteEvent(eventData);
                                Log.i("d", "delete");
                                setResult(RESULT_OK, i);
                                finish();

                                // continue with delete
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // do nothing
                            }
                        })
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();
            }
        });

谢谢。

1 个答案:

答案 0 :(得分:4)

你正在使用错误的上下文来使用活动上下文而不是应用程序上下文

变化:

  new AlertDialog.Builder(getApplicationContext())

  new AlertDialog.Builder(YourActivityName.this)