programing

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

lastcode 2023. 7. 1. 08:47
반응형

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

안드로이드 스튜디오 0.4.5

사용자 지정 대화 상자 생성을 위한 Android 설명서: http://developer.android.com/guide/topics/ui/dialogs.html

사용자 정의 대화상자를 원하는 경우 Dialog API를 사용하는 대신 Activity를 대화상자로 표시할 수 있습니다.활동을 만들고 테마를 테마로 설정하면 됩니다. . 대화 상자<activity>매니페스트 요소:

<activity android:theme="@android:style/Theme.Holo.Dialog" >

그러나 이를 시도하면 다음과 같은 예외가 발생합니다.

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

저는 다음을 지원하며, 최소 10보다 큰 값은 사용할 수 없습니다.

minSdkVersion 10
targetSdkVersion 19

제 스타일은 다음과 같습니다.

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

그리고 나의 매니페스트에는 활동을 위한 이것이 있습니다.

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:theme="@android:style/Theme.Holo.Light.Dialog"
            android:name="com.ssd.register.Dialog_update"
            android:label="@string/title_activity_dialog_update" >
        </activity>

이렇게 대화 상자를 만드는 것은 이미 레이아웃을 완료했기 때문에 제가 하고 싶은 일이었습니다.

제가 이 문제를 해결할 수 있는 방법을 알려주실 수 있나요?

이 문제가 발생하는 이유는 대화 상자 테마를 적용하려는 활동이 확장되고 있기 때문입니다.ActionBarActivity는 필다니합요은을 합니다.AppCompat적용할 테마입니다.

업데이트: 확장AppCompatActivity 이 할 수 .

Java 을 "" " " " " " " 에서 합니다.ActionBarActivityActivity비 대화제주그대에둡로니다매트니스페를▁the▁and둡▁in다.Theme.AppCompat


Android를 하려면 Android를 해야 한다는 입니다.AppCompat는 테와자코확합야니다어되장드는마바▁extend합니다▁and▁theme를 확장해야 합니다.AppCompatActivity 필요 없는 활동이수 코드는 old Android *전버이필요없는동경우있이어활떤만다어야니지이있로합래것된으적수반오할마든일코는용적테드는는있심관지이능원과기만에▁that▁ifan▁plain▁support,▁to▁extend▁code▁activity▁must▁old▁android▁the다어니야이▁you▁*▁java▁java로래된합것으적,▁theme▁as▁you▁such이오반의만▁any▁of▁this▁youActivity.


고참에서 경변: 시점시에서 AppCompatActivity 클래스, (또는하위클래스,클,스또래▁()ActionBarActivity), ~에게Activity또한 "지원"이 있는 다양한 통화를 "지원"이 없는 해당 통화로 변경해야 합니다.그래서 대신에getSupportFragmentManager, 전화 걸기getFragmentManager.

하면 됩니다.android:theme="@style/Theme.AppCompat.Light"의응프태로의 응용 합니다.AndroidManifest.xmljava.

위의 댓글에 있는 @MarkKeen의 답변을 복사하는 것은 저와 같은 문제가 있었습니다.

저는 게시물 상단에 오류가 명시되어 있었고 경고 대화 상자를 추가한 후 발생했습니다.나는 매니페스트에 관련된 모든 스타일 정보를 가지고 있습니다.알림 작성기에서 컨텍스트 참조를 변경하여 문제가 해결되었습니다. 변경 내용:

new android.support.v7.app.AlertDialog.Builder(getApplicationContext())

대상:

new android.support.v7.app.AlertDialog.Builder(this)

그리고 더 이상의 문제는 없습니다.

응용프로그램 컨텍스트를 사용하는 경우 다음과 같이 수행합니다.

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

다음과 같은 활동 컨텍스트로 변경합니다.

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

min sdk입니다. ActionBarAPI 레벨 11에서 사용할 수 있습니다.그래서 10개는 당신이 사용할 수 있습니다.AppCompat를 지원 할 수 있습니다.Theme.AppCompat또는 동일한 후손.

사용하다

android:theme="@style/Theme.AppCompat" >

또는 상단에 액션 바를 원하지 않는 경우.

android:theme="@style/Theme.AppCompat.NoActionBar">

추가 정보 @

http://developer.android.com/guide/topics/ui/actionbar.html

편집:

제가 oppost를 잘못 읽었을 수도 있습니다.

활동 테마가 있는 대화상자를 원하는 것 같습니다.그래서 이미 Bobbake4가 제안한 것처럼 확장.ActivityActionBarActivity.

아래 링크에서 @ Dialog Attributes도 확인하십시오.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/values/themes.xml/

나의 테마가 이 문제를 경험했음에도 불구하고.AppCompat주제와 나의 활동은AppCompatActivity(또는)Activity다른 사람의 답변에 제시된 바와 같이).그래서 저는 그 프로젝트를 청소하고, 다시 만들고, 재실행했습니다.

(Build -> Clean Project; Build -> Rebuild Project; Run -> Run)

바보처럼 보일 수도 있지만, 지금은 잘 작동합니다!

도움이 되기를 바랍니다!

매니페스트에 를 지정하는 에, 을 것이저를위그것고것친: 저매스트서주대신는다지정, 습니했의그것을에하정제이를다니페입는니로 했습니다.onCreate▁▁for▁each에동을 확장하는 각 활동에 대해ActionBarActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.MyAppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity_layout);
...
}

여기서MyAppTheme는 의후니다의 입니다.Theme.AppCompatxml에 .테마는 먼저 설정해야 합니다.super.onCreate그리고.setContentView.

당신의 스타일로 가서 부모님께.

parent="Theme.AppCompat"

대신에

parent="@android:style/Theme.Holo.Light"

원하는 활동의 테마를 변경합니다.이것은 나에게 도움이 됩니다.

<activity
            android:name="HomeActivity"
            android:screenOrientation="landscape"
            android:theme="@style/Theme.AppCompat.Light"
            android:windowSoftInputMode="stateHidden" />

제 경우에는 res 디렉토리에 values-v21 파일이 없습니다.그런 다음 작성하여 다음 코드를 추가했습니다.

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

저스트 두

new AlertDialog.Builder(this)

대신에

new AlertDialog.Builder(getApplicationContext())

테마를 사용한 활동임에도 불구하고 저는 삼성 기기에 큰 충격을 받았습니다.AppCompat.근본 원인은 삼성 측의 이상한 최적화와 관련이 있었습니다.

- if one activity of your app has theme not inherited from Theme.AppCompat
- and it has also `android:launchMode="singleTask"`
- then all the activities that are launched from it will share the same Theme

내 해결책은 단지 제거하는 것이었습니다.android:launchMode="singleTask"

style.xml에 필요한 ActionBarActivity를 확장해야 하는 경우:

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->

응용 프로그램의 주요 테마를 Android로 설정한 경우:테마. 소재.AppTheme 대신 빛을 사용합니다.기본적으로 "불법 상태 예외:테마를 사용해야 합니다.AppCompat 테마(또는 하위)에 이 활동" 오류가 있습니다.

여기서 모든 해결책을 시도한 에 저에게 해결책은 변화하는 것이었습니다.

    <activity
        android:name="com.github.cythara.MainActivity"
        android:label="Main">
    </activity>

테마를 포함합니다.

    <activity
        android:name="com.github.cythara.MainActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar"
        android:label="Main">
    </activity>

저도 같은 문제가 있었지만, 안드로이드:theme="@style/Theme.AppCompat을 매니페스트에 넣었을 때 해결되었습니다.

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name_test"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat">

        ...    

    </application>

저의 경우 Dialog를 표시하려고 할 때 이러한 문제가 발생했습니다.문제는 컨텍스트에서 발생했습니다. 이론적으로 활동 컨텍스트를 반환해야 하지만 그렇지 않은 것으로 보이는 getBaseContext()를 사용했습니다. 또는 테마가 적용되기 전에 컨텍스트를 반환합니다.

그래서 getBaseContexts()를 "이것"으로 바꾸었고, 이제는 예상대로 작동합니다.

        Dialog.showAlert(this, title, message,....);

이전 sdk 버전에서 테마 스타일로 재료 설계를 21에 적용하고 싶어서 이렇게 된 것입니다. ActionBarActivity을 요구합니다.AppTheme따라서 AppTheme에 대한 사용자 지정을 방지하려면 styles.xml(sdk 21 이전 버전)만 변경하면 되므로 AppCompat 테마를 상속할 수 있습니다.

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

이 경우:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

는 저는주제가활했습다니동을지고라는 주제로 활동을 했습니다.<android:theme="@android:style/Theme.Dialog">에 대화상자를 표시하는 데 사용됩니다. 위젯과 나는 같은 문제가 있었습니다.

아래와 같이 활동 코드를 변경하여 이 오류를 해결했습니다.

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_AppCompat_Dialog); //this line i added
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialog);
}

응용프로그램 또는 기본 컨텍스트가 아닌 새 알림 대화상자를 만들 때 활동 컨텍스트를 사용하는지 확인합니다.

ContextThemeWrapper를 사용할 수 있는 솔루션이었습니다.

private FloatingActionButton getFAB() {
Context context = new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
FloatingActionButton fab = new FloatingActionButton(context);
return fab;}

Android에서 - FAB를 프로그래밍 방식으로 생성하는 방법은 무엇입니까?

저도 이 문제가 있었고, 이 문제를 해결하기 위해 무엇을 했는가 하면, 여전히 홀로 테마를 사용하는 것은 다음과 같은 단계를 밟는 것이었습니다.

처음에 이 가져오기를 교체했습니다.

import android.support.v7.app.AppCompatActivity;

이것으로:

import android.app.Activity;

내 내선 번호를 다음에서 변경했습니다.

public class MyClass extends AppCompatActivity {//...

대상:

public class MyClass extends Activity {//...

또한 이 가져오기를 변경해야 했습니다.

import android.support.v7.app.AlertDialog;

이 가져오기로:

import android.app.AlertDialog;

그런 다음 활동 수준의 매니페스트에서 테마 태그를 사용할 수 있습니다.

android:theme="@android:style/Theme.Holo.Dialog" />

마지막으로 (프로젝트에 v7 appCompat을 사용해야 하는 다른 클래스가 없는 경우) 프로젝트를 정리 및 재구축하거나 앱 수준에서 gradle build 파일의 이 항목을 삭제할 수 있습니다.

compile 'com.android.support:appcompat-v7:23.2.1'

프로젝트에 v7 appCompat을 사용해야 하는 다른 클래스가 있는 경우 프로젝트를 정리하고 다시 빌드하면 됩니다.

AndroidX 가인 SplashScreen도서관에서 당신을 여기로 데려왔어요...

는 는이유 때문입니다.Theme.SplashScreen 한또없이 .R.styleable.AppCompatTheme_windowActionBar:

if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
    a.recycle();
    throw new IllegalStateException(
            "You need to use a Theme.AppCompat theme (or descendant) with this activity.");
}

이를 위해서는 테마를 다음으로 전환해야 합니다.postSplashScreenTheme전화하기 super():

@Override
protected void onCreate(Bundle savedInstanceState) {

    /* When switching the theme to dark mode. */
    if (savedInstanceState != null) {
        this.setTheme(R.style.AppTheme);
    }
    super.onCreate(savedInstanceState);
    
    /* When starting the Activity. */
    if (savedInstanceState == null) {
        SplashScreen.installSplashScreen(this);
    }
}

에 그면러그.Theme.SplashScreenAndroidManifest.xml간섭하지 않을 겁니다


또한 상당히 관련이 있습니다.을 할 때Theme.MaterialComponents브리지 테마가 포함되어 있으며, 이는 대체 기능을 합니다.Theme.AppCompat:Theme.MaterialComponents.DayNight.NoActionBar.Bridge.

것이.Bridge그럼에도 불구하고 테마는 작동합니다.Theme.MaterialComponents에서 상속되지 않음Theme.AppCompat:

<?xml version="1.0" encoding="utf-8"?>
<resources>    
    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge" />   
    <style name="AppTheme.SplashScreen" parent="Theme.SplashScreen" />    
</resources>

당신은 그 오류에 대한 많은 해결책을 가지고 있습니다.

  1. Actionbar Activity 또는 AppCompativity 대신 Activity 또는 Fragment Activity를 사용해야 합니다.

  2. ActionbarActivity 또는 AppCompatActivity를 사용하려면 styles.xml Theme에서 변경해야 합니다.Holo.xxxxx를 테마로.AppCompat.조명(필요한 경우 다크 액션 막대에 추가)

액션 모음이나 AppCompat에 대한 고급 특성이 필요하지 않으면 액션 모음이나 AppCompat을 사용할 필요가 없습니다.

Android 매니페스트에서 활동 테마를 App으로 변경합니다.후속 코드 스니펫과 같은 테마

<activity
  android:name=".MainActivity"
  android:label="@string/app_name"
  android:theme="@style/AppTheme">
</activity>

제 경험상 문제는 제가 대화를 보여준 상황이었습니다.단추 안에서 다음과 같은 방법으로 알림 대화 상자 인스턴스화를 클릭합니다.

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

하지만 문맥이 맞지 않아 오류가 발생했습니다.애플리케이션 컨텍스트를 사용하여 다음과 같은 방식으로 변경했습니다.

선언 섹션에서:

Context mContext;

onCreate 메서드에서

mContext = this;

그리고 마지막으로 AlertDialog가 필요한 코드:

start_stop = (Button) findViewById(R.id.start_stop);
start_stop.setOnClickListener( new View.OnClickListener()
     {
                @Override
                public void onClick(View v)
                {
                    if (!is_running)
                    {
                        builder = new AlertDialog.Builder(mContext);
                        builder.setMessage("MYTEXT")
                                .setCancelable(false)
                                .setPositiveButton("SI", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                    Task_Started = false;
                                    startTask();
                                    }
                                })
                                .setNegativeButton("NO",
                                        new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });
                        AlertDialog alert = builder.create();
                        alert.show();
                    }
            }
        }

이것이 저를 위한 해결책입니다.

저도 같은 문제를 겪고 있었습니다.사용자 지정 탐색 드로어를 만들고 있었기 때문입니다.하지만 저는 이런 식으로 제 매니페스트에 주제를 언급하는 것을 잊었습니다.

android:theme="@style/Theme.AppCompat.NoActionBar"

제 매니페스트에 위의 주제를 추가하자마자 문제가 해결되었습니다.

저도 같은 문제에 직면했습니다.

클래스 또는 메서드에 컨텍스트를 제공하는 경우 활동 이름을 제공합니다.getApplicationContext() 대신 사용합니다.

수행

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

대신에

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

테마 스타일 상위 항목 변경

 parent="Theme.AppCompat"

이건 나한테 효과가 있었어요

이것은 나에게 효과가 있었습니다.

<application
           android:allowBackup="true"
           android:icon="@mipmap/ic_launcher"
           android:label="@string/app_name"
           android:theme="@style/AppTheme" >
           <activity
               android:name=".MainActivity"
               android:label="@string/app_name"
               android:theme="@style/Theme.AppCompat.NoActionBar">

               <intent-filter>
                   <action android:name="android.intent.action.MAIN" />

                   <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
           </activity>
</application>

활동이 AppCompat을 필요로 하는 ActionBar 활동을 확장하고 있습니다.적용할 테마입니다.ActionBar Activity에서 Activity 또는 Fragment Activity로 변경하면 문제가 해결됩니다.

수행 표시줄을 사용하지 않는 경우:

android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 

언급URL : https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity

반응형