我是靠谱客的博主 彪壮舞蹈,最近开发中收集的这篇文章主要介绍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();

 

转载于:https://www.cnblogs.com/maosonglin/p/9191579.html

最后

以上就是彪壮舞蹈为你收集整理的android对话框显示异常报错:You need to use a Theme.AppCompat theme (or descendant) with this activity....的全部内容,希望文章能够帮你解决android对话框显示异常报错:You need to use a Theme.AppCompat theme (or descendant) with this activity....所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(32)

评论列表共有 0 条评论

立即
投稿
返回
顶部