Programing

이 활동에는 Theme.AppCompat 테마 (또는 하위 항목)를 사용해야합니다.

lottogame 2020. 12. 6. 20:50
반응형

이 활동에는 Theme.AppCompat 테마 (또는 하위 항목)를 사용해야합니다. Theme.AppCompat로 변경하면 다른 오류가 발생합니다.


내 앱에서 appcompat v22.1.0을 사용하고 툴바를 사용합니다. 내가 사용할 때 모든 것이 좋았습니다 Theme.AppCompat.Light.NoActionBar. 구현을 시작 AlertDialog하면 다음과 같은 오류가 발생합니다.

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113)
        at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
        at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:47)
        at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:92)
        at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:882)
        at com.ramabmtr.map.findingmoo.MainActivity.onOptionsItemSelected(MainActivity.java:216)
        at android.app.Activity.onMenuItemSelected(Activity.java:2572)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
        at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
        at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
        at android.support.v7.internal.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:74)
        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:164)
        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:740)
        at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
        at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:598)
        at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
        at android.view.View.performClick(View.java:4084)
        at android.view.View$PerformClick.run(View.java:16989)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4812)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
        at dalvik.system.NativeStart.main(Native Method)

그 오류를 기반으로 내 테마를 다음 Theme.AppCompat같이 변경 합니다.

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

내 테마에. 그러나 동일한 오류가 발생합니다.

Style.xml (이전)

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
</resources>

Style.xml (신규)

<resources>
<style name="AppTheme" parent="Theme.AppCompat">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
</resources>

누구든지 그것을 고치는 방법을 알고 있습니까 ??

MainActivity.java

package com.ramabmtr.map.findingmoo;

import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TextView toolbarTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/panpizza.ttf");
    toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
    toolbarTitle.setTypeface(myTypeface);

    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
    builder.setTitle(R.string.filter_title);
    builder.setMessage("test");
    builder.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}
}

기본적으로 활동은 도구 모음 (작업 표시 줄을 대체 함)을 사용하므로와 같은 작업 표시 줄이없는 활동에 스타일을 사용해야 Theme.AppCompat.Light.NoActionBar합니다. 대화에 대한 고유 한 스타일이있는 경우 적절한 AppCompat 테마를 상속해야합니다.

<style name="myDialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    ...
</style>

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));

MainActivity.this (또는 YourActivityName.this)를 사용하여 내 문제를 수정했습니다.

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

Make sure you already Theme.AppCompat and extending AppCompatActivity.


In my case, this crash was caused because I was passing View.getContext().getApplicationContext() as Context to the Builder. This was fixed by using getActivity() instead.


Saw this same exception using Activity and Theme.Light theme. My problem was a wrong import, I was using the support one. import android.support.v7.app.AlertDialog; instead of import android.app.AlertDialog;


if you have this error when you creating a dialog (just in my case) you should use the following:

AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.Theme_AppCompat_Light);

instead of:

AlertDialog.Builder dialog = new AlertDialog.Builder(context);

This worked me perfectly!


The probem is because of Context which You are passing to build the Alert Dialog.Don't Use getApplicationContext().Try using your Activity context.Use AlertDialog.Builder builder = new AlertDialog.Builder(MainActiviy.this);


This work for me... after read several answer was...

Change my import like this:

import android.app.AlertDialog;

instead of

import android.support.v7.app.AlertDialog; 

this give error Unable to add window -- token null is not for an application... so I change the context of the builder from

AlertDialog.Builder builder = new 

AlertDialog.Builder(getAplicationContext()); to

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

Adding Android:theme="@style/Theme.AppCompat" like this in manifest

<activity
    android:theme="@style/Theme.AppCompat"
    android:name=".MainActivity"

solved the problem


If you are using support library your activity extends AppCompactActivity, if you use android studio to create activity this is default. In such case pass context to the builder as ActivityName.this or simply this if you are passing it in onCreate, passing getApplicationContext() will not work.

This is my style using appcompact

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textSize">18sp</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

And everything is working fine when I use this or ActivityName.this as mentioned above.

If you are using in fragment you should pass getActivity() as context to builder instead of getContext().


You need to pass your Activity's context instead to pass your Context. Try with "this" instead "context". This work for me


In my case, I had to check and make sure that all styles.xml files use Theme.AppCompat. The standard styles.xml file had that theme but there were also styles(v21).xml, styles(v11).xml and styles(sw600dp).xml that were auto-generated. The simple solution would be to copy and paste the customized default AppTheme style into these folders.

All the best!


Please be careful with such problem, i've waisted long long hours on such bug. it happens only on android 4.4.2 version, despite the fact that i'm using the Appcompat theme everywhere, tried to play with all styles and use many other threads and answers.

the problem was caused by this line:

 <style name="LooperLabTheme"
        parent.theme="@style/Theme.AppCompat.Light.NoActionBar"
        theme="@style/Theme.AppCompat.Light.NoActionBar">
<style/>

please notice the "parent.theme", i've copied it from some answer in the first days of launching the project that i'm woking on, it should be "parent" without the theme.

 <style name="LooperLabTheme"
            parent="@style/Theme.AppCompat.Light.NoActionBar"
            theme="@style/Theme.AppCompat.Light.NoActionBar">
    <style/>

I just want to help others who are running from thread to thread to find out the fix for his very specific problem, it might be the same as mine, save your time.


getSupportActionBar().getThemedContext()

AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    builder = new AlertDialog.Builder(getSupportActionBar().getThemedContext(), android.R.style.Theme_Material_Dialog_Alert);
} else {
    builder = new AlertDialog.Builder(getSupportActionBar().getThemedContext());
}
builder.setTitle("Alert Dialog")
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        })
       .setIcon(android.R.drawable.ic_dialog_alert)
       .show();

In kotlin this worked to me:

val dialog = AlertDialog.Builder(this)

참고URL : https://stackoverflow.com/questions/30180052/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity-chan

반응형