How can I fix this error: You need to use a Theme.AppCompat theme (or descendant) with this activity
How can I fix this error: You need to use a Theme.AppCompat theme (or descendant) with this activity
我搜索了所有 Internet 网站以修复此错误,但我做不到。我只想创建带有两个按钮 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>
这是我的风格(v21):
<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[=14=]0(DialogActivity.java:10)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at com.irannara.pda.pdaassistant.DialogActivity.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)
none 个链接无法帮助我:
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???
等等。
我该如何解决这个问题?
在您的 style.xml 文件中添加以下代码-
style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme.Base"
parent="@style/Theme.AppCompat.Light">
</style>
</resources>
也在你的 activity
中定义这样的主题
<activity
android:name=".DialogActivity"
android:label="@string/title_activity_test"
android:screenOrientation="portrait"
android:theme="@android:style/AppTheme.Dialog">
或
<activity
android:name=".DialogActivity"
android:label="@string/title_activity_test"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
清理项目并再次 运行..
如果您在另一个值文件夹中有另一个样式文件,例如“values-v11
”、“values-v14
”...还要编辑主题并尝试在 运行 之前清理您的应用.
已编辑:
从您的 activity 将 getApplicationContext()
更改为 this
:
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
到
AlertDialog.Builder builder = new AlertDialog.Builder(this);
因为对话框还应该扩展 Appcompat
主题。
尝试将以下内容添加到您的混淆规则中:
-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:play-services-ads:8.1.0
看看Google问题190237
如果有人遇到此活动问题,请尝试为您的 activity 显式配置主题。
<activity android:name=".activities.BLEControlActivity" android:theme="@style/Theme.AppCompat.DayNight"></activity>
我遇到过这样的情况,我设法解决了。这是:
- 在 MainActivity class 中定义您的警报对话框,示例:
public class MainActivity extends AppCompatActivity {
AlertDialog.Builder alertDialog;
- 然后在 OnCreate() 方法上初始化它,如下所示:
alertDialog = new AlertDialog.Builder(this);
- 然后您可以在任何您喜欢的地方进行其余的警报对话框自定义,如图标、标题、消息。在我的例子中,我在倒数计时器的 onFinish() 中使用了它,如下所示:
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();
您遇到此错误是因为您的 AlertDialog
没有使用 AppCompat 主题,而您的 activity 是。要修复它,请使用 activity 的 this
属性 而不是 getApplicationContext()
,如下所示:
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
您的 activity 的名称,后跟 .this
,在本例中应始终使用 DialogActivity.this
,而不是仅使用 this
,因为如果您的对话是在另一个 class 中创建,例如某种适配器,您将收到一个编译时错误,指出需要 Context
而不是给定的适配器 class。
我搜索了所有 Internet 网站以修复此错误,但我做不到。我只想创建带有两个按钮 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>
这是我的风格(v21):
<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[=14=]0(DialogActivity.java:10)
11-02 04:36:55.941 24352-24352/? E/AndroidRuntime: at com.irannara.pda.pdaassistant.DialogActivity.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)
none 个链接无法帮助我:
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???
等等。
我该如何解决这个问题?
在您的 style.xml 文件中添加以下代码-
style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme.Base"
parent="@style/Theme.AppCompat.Light">
</style>
</resources>
也在你的 activity
中定义这样的主题 <activity
android:name=".DialogActivity"
android:label="@string/title_activity_test"
android:screenOrientation="portrait"
android:theme="@android:style/AppTheme.Dialog">
或
<activity
android:name=".DialogActivity"
android:label="@string/title_activity_test"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
清理项目并再次 运行..
如果您在另一个值文件夹中有另一个样式文件,例如“values-v11
”、“values-v14
”...还要编辑主题并尝试在 运行 之前清理您的应用.
已编辑:
从您的 activity 将 getApplicationContext()
更改为 this
:
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
到
AlertDialog.Builder builder = new AlertDialog.Builder(this);
因为对话框还应该扩展 Appcompat
主题。
尝试将以下内容添加到您的混淆规则中:
-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:play-services-ads:8.1.0
看看Google问题190237
如果有人遇到此活动问题,请尝试为您的 activity 显式配置主题。
<activity android:name=".activities.BLEControlActivity" android:theme="@style/Theme.AppCompat.DayNight"></activity>
我遇到过这样的情况,我设法解决了。这是:
- 在 MainActivity class 中定义您的警报对话框,示例:
public class MainActivity extends AppCompatActivity {
AlertDialog.Builder alertDialog;
- 然后在 OnCreate() 方法上初始化它,如下所示:
alertDialog = new AlertDialog.Builder(this);
- 然后您可以在任何您喜欢的地方进行其余的警报对话框自定义,如图标、标题、消息。在我的例子中,我在倒数计时器的 onFinish() 中使用了它,如下所示:
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();
您遇到此错误是因为您的 AlertDialog
没有使用 AppCompat 主题,而您的 activity 是。要修复它,请使用 activity 的 this
属性 而不是 getApplicationContext()
,如下所示:
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
您的 activity 的名称,后跟 .this
,在本例中应始终使用 DialogActivity.this
,而不是仅使用 this
,因为如果您的对话是在另一个 class 中创建,例如某种适配器,您将收到一个编译时错误,指出需要 Context
而不是给定的适配器 class。