我搜索了所有互联网网站以修复此错误,但我不能。我只想用两个按钮Yes和No。
创建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>
这是我的风格(第21节):
<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)
这些链接都无法帮助我:
You need to use a Theme.AppCompat theme (or descendant) with this activity
ActionBarCompat: java.lang.IllegalStateException: You need to use a Theme.AppCompat
Need to use a Theme.AppCompat theme (or descendant) with this activity???
等等。
我该如何解决这个问题?
答案 0 :(得分:22)
如果您在另一个值文件夹中有另一个值文件夹,例如&#34; values-v11
&#34;,&#34; values-v14
&#34; ...也可以编辑主题在运行之前清理你的应用程序。
编辑:
从您的活动更改getApplicationContext()
到this
:
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
到
AlertDialog.Builder builder = new AlertDialog.Builder(this);
因为对话框还应扩展Appcompat
主题。
答案 1 :(得分:1)
如果有人遇到活动的这个问题,请尝试为您的活动明确配置主题。
<activity android:name=".activities.BLEControlActivity" android:theme="@style/Theme.AppCompat.DayNight"></activity>
答案 2 :(得分:1)
您遇到此错误是因为您的AlertDialog
在活动时未使用AppCompat主题。要解决此问题,请使用您活动的this
属性而不是getApplicationContext()
,如下所示:
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
应该始终使用活动名称,后跟.this
,在这种情况下为DialogActivity.this
,而不是仅使用this
,因为如果对话框是在另一个类中创建的,则例如某种适配器,您将收到一个编译时错误,指出应使用Context
而不是给定的适配器类。
答案 3 :(得分:0)
style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme.Base"
parent="@style/Theme.AppCompat.Light">
</style>
</resources>
还在您的活动中定义这样的主题
<activity
android:name=".DialogActivity"
android:label="@string/title_activity_test"
android:screenOrientation="portrait"
android:theme="@android:style/AppTheme.Dialog">
OR
<activity
android:name=".DialogActivity"
android:label="@string/title_activity_test"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
清理项目并再次运行..
答案 4 :(得分:0)
我面对这样的案件,并设法解决了这个问题。在这里:
public class MainActivity extends AppCompatActivity {
AlertDialog.Builder alertDialog;
alertDialog = new AlertDialog.Builder(this);
public void onFinish() {
alertDialog.setIcon(android.R.drawable.alert_light_frame);
alertDialog.setTitle("You are done");
alertDialog.setMessage("you got it");
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
runTheCode();
}
});
alertDialog.show();
答案 5 :(得分:-1)
尝试在proguard规则中添加以下内容:
-keep public class com.google.android.gms.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
-dontwarn com.google.android.gms.**
如果您正在使用
com.google.android.gms:播放服务的广告:8.1.0
查看Google问题190237