我搜索了所有的互联网网站来修复这个错误,但是我做不到。我只想用两个按钮“是”和“否”创建AlertDialog。
这是我的代码:
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DialogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
Button btnDialog= (Button) findViewById(R.id.btnDialog);
btnDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialogMessage();
}
});
}
private void showDialogMessage(){
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
// Add the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
这就是我的风格:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<resources>
<!-- extend one of the Theme.AppCompat themes -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- customize the color palette -->
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:colorControlHighlight">@color/colorHighLight</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.saly.rastari" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".DialogActivity"
android:label="@string/title_activity_test"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: FATAL EXCEPTION: main
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: Process: com.irannara.pda.pdaassistant, PID: 24352
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:252)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:76)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.support.v7.app.AlertController.installContent(AlertController.java:216)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.app.Dialog.show(Dialog.java:274)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at com.irannara.pda.pdaassistant.DialogActivity.showDialogMessage(DialogActivity.java:42)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at com.irannara.pda.pdaassistant.DialogActivity.access$000(DialogActivity.java:10)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at com.irannara.pda.pdaassistant.DialogActivity$1.onClick(DialogActivity.java:21)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.view.View.performClick(View.java:4780)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19866)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-02 04:36:55.959 718-718/? E/EGL_emulation: tid 718: eglCreateSyncKHR(1209): error 0x3004 (EGL_BAD_ATTRIBUTE)
这些链接都帮不了我:
>
您需要在此活动中使用theme.AppCompat主题(或后代)
ActionBarCompat:java.lang.IllegalStateException:您需要使用Theme.AppCompat
如果在另一个值文件夹中有另一个样式文件,如“values-v11
”、“values-v14
”...编辑主题,并尝试清理您的应用程序之前运行。
已编辑:从活动中将GetApplicationContext()
更改为This
:
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
至
AlertDialog.Builder builder = new AlertDialog.Builder(this);
我有困难运行我的Android应用程序在一个全屏模式下按视频的指示。当它试图运行时,应用程序会随着错误而崩溃。 可能有用的部分主体活动
上面是主题和样式。当我运行应用程序时,显示这个错误。我想也许应该是“theme.appcompat”而不是“Android:theme”,但不确定!
XML 错误logcat
我正试图在删除按钮上使用警报对话框。但是显示对话框时显示异常。当我单击delete时,它崩溃并在.Show上显示exception。 我试图使用theme.AppCompat主题进行此活动,但它仍然崩溃。
这个问题已经问了很多次了,但我仍然无法解决这个问题。 我正试图在活动中显示报警窗口。它会给我以下错误消息 java.lang.IllegalStateException:您需要在此活动中使用theme.AppCompat主题(或后代 这是我的密码。
我在启动应用程序时遇到以下错误: 我认为这与有关,但想不通。