简体   繁体   English

单击按钮显示警报对话框时,应用崩溃

[英]App crashing when clicking on the button to show Alert Dialog

I created a alert dialog behind the onclick of a button but when i click that button it crashes the app throw the following exception. 我在按钮的onclick后面创建了一个警告对话框,但是当我单击该按钮时,它使应用程序崩溃,引发以下异常。

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity 

07-13 11:51:12.445 21054-21054/com.vshine.neuron.riseshine E/MultiWindowProxy: getServiceInstance failed! 07-13 11:51:12.445 21054-21054 / com.vshine.neuron.riseshine E / MultiWindowProxy:getServiceInstance失败!

in logcat that I couldn't able to understand as I'm new to coding, .Java and Logcat screenshots are attached. 在我不懂编码的logcat中,.Java和Logcat屏幕快照已附加。 This is Logcat : 这是Logcat:

在此处输入图片说明

This is .java class : 这是.java类:

在此处输入图片说明

just try to inherit Activity instead of AppCompatActivity to your quiz.java activity 只是尝试将Activity而不是AppCompatActivity继承到您的quiz.java活动中

like at top of code 就像代码顶部

public class Quiz extends Activity{

for more check this post. 有关更多信息,请查看此帖子。

Ok i think you need to provide context of the Activity. 好的,我认为您需要提供活动的上下文。 Try this: 尝试这个:

new AlertDialog.Builder(YourClassName.this)
                .setTitle("Title")
                .setMessage("Message?")
                .setCancelable(false)
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Function on no
                })
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       //Function on yes
                    }
                })
                .show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM