• android对话框显示异常报错:You need to use a Theme.AppCompat theme (or descendant) with this activity.


    今天写android实验碰到到一个问题,在用AlertDialog.Builder类构建一个对话框之后,调用Builder.show()方法时抛出异常如下:

    06-16 21:52:16.456 5017-5017/xyz.qlrr.sqliteapp W/System.err: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    06-16 21:52:16.457 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)
    06-16 21:52:16.457 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
    06-16 21:52:16.457 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
    06-16 21:52:16.457 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:83)
    06-16 21:52:16.457 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.support.v7.app.AlertController.installContent(AlertController.java:225)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.app.Dialog.dispatchOnCreate(Dialog.java:471)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.app.Dialog.show(Dialog.java:334)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at xyz.qlrr.sqliteapp.DisplayActivity.onItemClick(DisplayActivity.java:76)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.widget.AdapterView.performItemClick(AdapterView.java:310)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.widget.AbsListView.performItemClick(AbsListView.java:1187)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3108)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.widget.AbsListView.onTouchUp(AbsListView.java:4099)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3781)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.View.dispatchTouchEvent(View.java:9328)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2564)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2250)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2570)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2570)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2570)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2570)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2570)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2570)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2264)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2452)
    06-16 21:52:16.458 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1749)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.app.Activity.dispatchTouchEvent(Activity.java:2927)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2405)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.View.dispatchPointerEvent(View.java:9548)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4515)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4313)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3823)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3883)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3842)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3990)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3850)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4047)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3823)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3883)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3842)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3850)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3823)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6239)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6213)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6174)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6346)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.ViewRootImpl$ColorWindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7597)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
    06-16 21:52:16.459 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.os.MessageQueue.nativePollOnce(Native Method)
    06-16 21:52:16.460 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.os.MessageQueue.next(MessageQueue.java:323)
    06-16 21:52:16.460 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.os.Looper.loop(Looper.java:163)
    06-16 21:52:16.460 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5769)
    06-16 21:52:16.460 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    06-16 21:52:16.460 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    06-16 21:52:16.460 5017-5017/xyz.qlrr.sqliteapp W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)

    上网百度查找答案说是因为活动是继承了android.support.v7.app.AppCompatActivity,而且主题设置为了和这个活动父类不兼容的主题。我查看代码不是这个原因。后来发现在创建AlertDialog.Builder()的使用调用的上下文是用getApplicationContext()方法获取的,将上下文改为当前的活动对象this问题就解决了。

    修改前:

     1 AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
     2             builder.setMessage("确认").setMessage("您确定要删除这条记录吗?");
     3             builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
     4                 @Override
     5                 public void onClick(DialogInterface dialogInterface, int i) {
     6                     TextView idView = (TextView) view.findViewById(R.id.idView);
     7                     String s = idView.getText().toString();
     8                     Integer id = Integer.valueOf(s);
     9                     SQLiteHelper sqLiteHelper = new SQLiteHelper(getApplicationContext());
    10                     boolean b = sqLiteHelper.delete(id);
    11                     Toast.makeText(DisplayActivity.this,b?"删除成功":"删除失败",Toast.LENGTH_SHORT).show();
    12                     if(b){
    13                         loadlistview();
    14                     }
    15                 }
    16             });
    17             builder.setPositiveButton("取消",null);
    18             builder.show();

    修改后:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("确认").setMessage("您确定要删除这条记录吗?");
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        TextView idView = (TextView) view.findViewById(R.id.idView);
                        String s = idView.getText().toString();
                        Integer id = Integer.valueOf(s);
                        SQLiteHelper sqLiteHelper = new SQLiteHelper(getApplicationContext());
                        boolean b = sqLiteHelper.delete(id);
                        Toast.makeText(DisplayActivity.this,b?"删除成功":"删除失败",Toast.LENGTH_SHORT).show();
                        if(b){
                            loadlistview();
                        }
                    }
                });
                builder.setPositiveButton("取消",null);
                builder.show();
  • 相关阅读:
    数据库(2019年10月30日)
    (面试题)反射(2019年10月28日)
    反射(2019年10月28日)
    常微分复习重点
    重要定理及其证明
    实变函数复习重点
    泛函分析重点定理
    自旋玻璃简介
    Fnight博文发布规范
    [分析力学]解题思路
  • 原文地址:https://www.cnblogs.com/maosonglin/p/9191579.html
Copyright © 2020-2023  润新知