ant*_*009 949 android android-layout
Android Studio 0.4.5
用于创建自定义对话框的Android文档:http://developer.android.com/guide/topics/ui/dialogs.html
如果需要自定义对话框,则可以将"活动"显示为对话框,而不是使用"对话框API".只需创建一个活动并将其主题设置为<activity>清单元素中的Theme.Holo.Dialog :
<activity android:theme="@android:style/Theme.Holo.Dialog" >
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试这个时,我得到以下异常:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
Run Code Online (Sandbox Code Playgroud)
我支持以下内容,我不能使用大于10的内容:
minSdkVersion 10
targetSdkVersion 19
Run Code Online (Sandbox Code Playgroud)
在我的风格中,我有以下内容:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
在我的清单中,我有这个活动:
<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>
Run Code Online (Sandbox Code Playgroud)
像我这样创建对话框是我要做的事情,因为我已经完成了布局.
谁能告诉我如何解决这个问题?
Bob*_*ke4 1088
您遇到此问题的原因是您尝试应用对话框主题的活动正在扩展ActionBarActivity,这需要AppCompat应用主题.
更新:扩展 AppCompatActivity也会出现此问题
在这种情况下,Java继承从改变ActionBarActivity到Activity和退出对话框主题清单,因为它是一个非Theme.AppCompat值
一般规则是,如果您希望您的代码支持旧版本的Android,它应该具有AppCompat主题并且Java代码应该扩展AppCompatActivity.如果你有一个不需要这种支持的活动,比如你只关心Android的最新版本和功能,你可以应用任何主题,但java代码必须扩展旧的Activity.
注意:当从AppCompatActivity(或子类,ActionBarActivity)Activity更改为,还必须将"支持"的各种调用更改为没有 "支持" 的相应调用.所以,而不是getSupportFragmentManager,打电话getFragmentManager.
ius*_*ing 530
您需要做的就是android:theme="@style/Theme.AppCompat.Light"在AndroidManifest.xml文件中添加应用程序标记.
A.K*_*.K. 251
在上面的评论中从@MarkKeen复制答案,因为我遇到了同样的问题.
我在帖子顶部说明了错误,并在我添加了一个警告对话框后发生了.我在清单中有所有相关的样式信息.通过更改警报生成器中的上下文引用来解决我的问题 - 我更改了:
new android.support.v7.app.AlertDialog.Builder(getApplicationContext())
Run Code Online (Sandbox Code Playgroud)
至:
new android.support.v7.app.AlertDialog.Builder(this)
Run Code Online (Sandbox Code Playgroud)
没有更多的问题.
Rag*_*dan 100
min sdk是10. ActionBar可从api level 11获得.因此对于10你将使用AppCompat你需要使用的支持库Theme.AppCompat或它的后代.
使用
android:theme="@style/Theme.AppCompat" >
Run Code Online (Sandbox Code Playgroud)
或者,如果你不想在顶部的动作栏
android:theme="@style/Theme.AppCompat.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
更多信息 @
http://developer.android.com/guide/topics/ui/actionbar.html
编辑:
我可能误读了op post.
似乎操作需要具有活动主题的Dialog.因此,正如Bobbake4已经建议的那样,Activity而不是ActionBarActivity.
还可以在下面的链接中查看@ Dialog Attributes
Dar*_*ush 83
这对我有用,我刚刚改变了:
final AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)
对此:
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
Run Code Online (Sandbox Code Playgroud)
Ger*_*eto 40
我遇到了这个问题,即使我的主题是一个AppCompat主题,我的活动是一个AppCompatActivity(或者Activity,正如其他人的答案所建议的那样).所以我清理,重建并重新运行项目.
(构建 - >清理项目;构建 - >重建项目;运行 - >运行)
它可能看起来很愚蠢,但现在效果很好!
希望它有所帮助!
k29*_*k29 24
这就是为我解决的问题:我没有在manifest中指定主题,而是onCreate为每个扩展的活动定义了它ActionBarActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.MyAppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
...
}
Run Code Online (Sandbox Code Playgroud)
这MyAppTheme是x的后代Theme.AppCompat,并在xml中定义.请注意,必须在super.onCreate和之前设置主题setContentView.
Yam*_*sif 19
去你的风格并把父母
parent="Theme.AppCompat"
Run Code Online (Sandbox Code Playgroud)
代替
parent="@android:style/Theme.Holo.Light"
Run Code Online (Sandbox Code Playgroud)
Ali*_*lli 16
在我的情况下,我的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>
Run Code Online (Sandbox Code Playgroud)
Jon*_*ira 13
如果需要在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. -->
Run Code Online (Sandbox Code Playgroud)
如果您将应用程序的主题设置为android:Theme.Material.Light而不是AppTheme.Base,那么您将获得"IllegalStateException:您需要使用此活动的Theme.AppCompat主题(或后代)"错误.
sha*_*nal 13
更改所需活动的主题.这对我有用:
<activity
android:name="HomeActivity"
android:screenOrientation="landscape"
android:theme="@style/Theme.AppCompat.Light"
android:windowSoftInputMode="stateHidden" />
Run Code Online (Sandbox Code Playgroud)
Ali*_*ram 13
做就是了
new AlertDialog.Builder(this)
Run Code Online (Sandbox Code Playgroud)
代替
new AlertDialog.Builder(getApplicationContext())
Run Code Online (Sandbox Code Playgroud)
goR*_*Gon 12
我在三星设备上遇到了这样的崩溃,即使该活动确实使用了Theme.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
Run Code Online (Sandbox Code Playgroud)
我的解决方案只是删除 android:launchMode="singleTask"
小智 12
我有同样的问题,但当我把它放在清单上时它解决了:android: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>
Run Code Online (Sandbox Code Playgroud)
Luc*_*lla 11
对我来说,在尝试了这里的所有解决方案之后,一个解决方案是改变
<activity
android:name="com.github.cythara.MainActivity"
android:label="Main">
</activity>
Run Code Online (Sandbox Code Playgroud)
包括一个主题:
<activity
android:name="com.github.cythara.MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:label="Main">
</activity>
Run Code Online (Sandbox Code Playgroud)
Mar*_*ler 10
如果 AndroidXSplashScreen库将您带到这里......
这是因为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.");
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
那么Theme.SplashScreen来自的人AndroidManifest.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>
Run Code Online (Sandbox Code Playgroud)
小智 9
我有一个带有主题的活动,该主题<android:theme="@android:style/Theme.Dialog">用于在我的appWidget中显示对话框,但我遇到了同样的问题
我通过更改活动代码来解决此错误,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_Dialog); //this line i added
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
Run Code Online (Sandbox Code Playgroud)
小智 8
对我来说是使用ContextThemeWrapper的解决方案:
private FloatingActionButton getFAB() {
Context context = new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
FloatingActionButton fab = new FloatingActionButton(context);
return fab;}
Run Code Online (Sandbox Code Playgroud)
我也有这个问题以及我做了什么来修复它,并且仍然使用Holo主题是采取以下步骤:
首先我替换了这个导入:
import android.support.v7.app.AppCompatActivity;
Run Code Online (Sandbox Code Playgroud)
这一个:
import android.app.Activity;
Run Code Online (Sandbox Code Playgroud)
然后改变我的扩展名:
public class MyClass extends AppCompatActivity {//...
Run Code Online (Sandbox Code Playgroud)
对此:
public class MyClass extends Activity {//...
Run Code Online (Sandbox Code Playgroud)
并且还必须更改此导入:
import android.support.v7.app.AlertDialog;
Run Code Online (Sandbox Code Playgroud)
这个导入:
import android.app.AlertDialog;
Run Code Online (Sandbox Code Playgroud)
然后您可以在活动级别的清单中使用主题标记:
android:theme="@android:style/Theme.Holo.Dialog" />
Run Code Online (Sandbox Code Playgroud)
最后,(除非您的项目中有其他类必须使用v7 appCompat),您可以清理并重建项目,也可以在应用程序级别的gradle构建文件中删除此条目:
compile 'com.android.support:appcompat-v7:23.2.1'
Run Code Online (Sandbox Code Playgroud)
如果您的项目中有其他类必须使用v7 appCompat,那么只需清理并重建项目即可.
小智 7
我遇到了同样的问题。因为我正在创建自定义导航抽屉。但我忘记在我的清单中提到这样的主题
android:theme="@style/Theme.AppCompat.NoActionBar"
Run Code Online (Sandbox Code Playgroud)
一旦我将上述主题添加到我的清单中,它就解决了问题。
以我为例,当我尝试显示Dialog时出现了此类问题。问题是在上下文中,我使用了getBaseContext(),该方法理论上应该返回Activity上下文,但是看起来不是,或者在应用任何Theme之前返回上下文。
因此,我只是将getBaseContexts()替换为“ this ”,现在它可以按预期工作。
Dialog.showAlert(this, title, message,....);
Run Code Online (Sandbox Code Playgroud)
小智 7
我也遇到过同样的问题。
如果您要为任何类或方法提供上下文,请提供 YourActivityName.this 而不是 getApplicationContext()。
做这个
builder = new AlertDialog.Builder(YourActivity.this);
Run Code Online (Sandbox Code Playgroud)
代替
builder = new AlertDialog.Builder(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)
您之所以这样,是因为您希望将以前的sdk版本中的主题样式中的Material Design应用到21. ActionBarActivity需要AppTheme如此,如果您还想阻止自己对AppTheme进行自定义,则只需更改styles.xml(之前的版本)以这种方式,可以继承App Compat主题.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
为了这:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
你有很多解决这个错误的方法.
您应该使用Activity或FragmentActivity而不是ActionbarActivity或AppCompatActivity
如果要使用ActionbarActivity或AppCompatActivity,则应将styles.xml Theme.Holo.xxxx更改为Theme.AppCompat.Light(如有必要,请添加到DarkActionbar)
如果您不需要有关操作栏或AppCompat的高级属性,则无需使用Actionbar或AppCompat.
小智 6
在Android清单中,只需将活动主题更改为AppTheme,如下面的代码片段
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
Run Code Online (Sandbox Code Playgroud)
这是你想在片段中使用AlertDialog的时候
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
adb.setTitle("My alert Dialogue \n");
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//some code
} });
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
} });
adb.show();
Run Code Online (Sandbox Code Playgroud)
小智 6
根据我的经验,问题出在显示对话的上下文中。在按钮内单击,我以这种方式实例化AlertDialog:
builder = new AlertDialog.Builder(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)
但是上下文不正确,并导致了错误。我已经使用这种方式使用应用程序上下文对其进行了更改:
在声明部分:
Context mContext;
Run Code Online (Sandbox Code Playgroud)
在onCreate方法中
mContext = this;
Run Code Online (Sandbox Code Playgroud)
最后在需要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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案。
我在使用时遇到了这个问题,当未在中调用SplashScreen Api时也会抛出此错误installSplashScreen()MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
Run Code Online (Sandbox Code Playgroud)
这个对我有用:
<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>
Run Code Online (Sandbox Code Playgroud)
您的Activity正在扩展ActionBarActivity,需要应用AppCompat.theme.从ActionBarActivity更改为Activity或FragmentActivity,它将解决问题.
如果您不使用操作栏,则:
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
Run Code Online (Sandbox Code Playgroud)
如果有人在运行 Android 12 启动画面时看到此错误,请按照以下简单步骤操作
中的应用程序主题values/themes.xml应values-night/themes.xml
作为Theme.AppCompat父主题。
<style name="Theme.Ace" parent="Theme.AppCompat.DayNight.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
使用此主题postSplashScreenTheme作为启动屏幕主题
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/orange_900</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_help_outline</item>
<item name="windowSplashScreenAnimationDuration">2000</item>
<item name="postSplashScreenTheme">@style/Theme.Ace</item>
Run Code Online (Sandbox Code Playgroud)
从启动活动中删除任何主题并用作Theme.App.Starting应用程序主题
<application...
android:theme="@style/Theme.App.Starting">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
765092 次 |
| 最近记录: |