我该如何解决此错误:您需要在此活动中使用Theme.AppCompat主题(或后代) [英] How can I fix this error: You need to use a Theme.AppCompat theme (or descendant) with this activity

查看:225
本文介绍了我该如何解决此错误:您需要在此活动中使用Theme.AppCompat主题(或后代)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了所有互联网网站,以解决此错误,但是无法。我只想用两个按钮是和否创建AlertDialog。

I searched all internet web sites to fix this error, but I couldn't. I just want to create AlertDialog with two button Yes and No.

这是我的代码:

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 ):

this is my 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>

当我单击对话框按钮时出现此错误:

When I click on Dialog button I get this error:

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)

此链接均无无法帮助我

ActionBarCompat:java.lang.IllegalStateException:您需要使用Theme.AppCompat

此活动需要使用Theme.AppCompat主题(或后代)吗? ?

等等。

我该如何解决?

推荐答案

如果在另一个值文件夹(如 values-v11 values-v14 )中保存其他样式文件...

If you have another styles files in side another values folders like "values-v11", "values-v14"... Edit theme also and try to clean your app before running.

编辑:
从您的活动更改中 getApplicationContext() this

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

AlertDialog.Builder builder = new AlertDialog.Builder(this);

因为该对话框还应该扩展 Appcompat 主题。

Because the dialog also should extends the Appcompat Theme.

这篇关于我该如何解决此错误:您需要在此活动中使用Theme.AppCompat主题(或后代)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆