单击按钮以显示警报对话框时应用程序崩溃
App crashing when clicking on the button to show Alert Dialog
我在单击按钮后创建了一个警告对话框,但是当我单击该按钮时它崩溃了,应用程序抛出以下异常。
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!
在 logcat 中,由于我是编码新手,我无法理解,附上 .Java 和 Logcat 屏幕截图。
这是 Logcat :
这是 .java class :
只是尝试继承 Activity
而不是 AppCompatActivity
到您的 quiz.java
activity
喜欢在代码的顶部
public class Quiz extends Activity{
更多检查this post.
好的,我认为您需要提供 Activity 的上下文。
试试这个:
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();
我在单击按钮后创建了一个警告对话框,但是当我单击该按钮时它崩溃了,应用程序抛出以下异常。
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!
在 logcat 中,由于我是编码新手,我无法理解,附上 .Java 和 Logcat 屏幕截图。 这是 Logcat :
这是 .java class :
只是尝试继承 Activity
而不是 AppCompatActivity
到您的 quiz.java
activity
喜欢在代码的顶部
public class Quiz extends Activity{
更多检查this post.
好的,我认为您需要提供 Activity 的上下文。 试试这个:
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();