Android Studio 0.4.5

用于创建自定义对话框的Android文档:http://developer.android.com/guide/topics/ui/dialogs.html

如果您想要一个自定义对话框,您可以将活动显示为对话框,而不是使用对话框api。简单地创建一个活动,并将其主题设置为theme . holo . dialog in <activity> manifest元素:

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

创建这样的对话框是我一直在做的事情,因为我已经完成了布局。

谁能告诉我如何解决这个问题?


当前回答

我有同样的问题,但它解决了,当我把这个manifest: 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>

其他回答

你遇到这个问题的原因是因为你试图应用对话框主题的活动正在扩展ActionBarActivity,这需要应用AppCompat主题。

更新:扩展AppCompatActivity也会有这个问题

在本例中,将Java继承从ActionBarActivity更改为Activity,并在清单中保留对话框主题,即非theme。AppCompat价值


一般的规则是,如果你想让你的代码支持旧版本的Android,它应该有AppCompat主题,java代码应该扩展AppCompat活动。如果你有一个活动不需要这种支持,比如你只关心Android的最新版本和功能,你可以应用任何主题,但java代码必须扩展普通的旧活动。


注意:当从AppCompatActivity(或子类ActionBarActivity)更改为Activity时,也必须将各种带有“support”的调用更改为相应的不带“support”的调用。因此,调用getFragmentManager而不是getSupportFragmentManager。

这是当你想在一个片段中有一个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();

这是什么固定它为我:而不是指定在manifest主题,我定义它在onCreate为每个扩展ActionBarActivity的活动:

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

这里MyAppTheme是Theme的后代。在xml中定义。注意,主题必须设置在super之前。onCreate和setContentView。

对于这个错误,有很多解决方案。

你应该使用Activity或FragmentActivity而不是ActionbarActivity或AppCompatActivity 如果你想使用ActionbarActivity或AppCompatActivity,你应该把styles.xml Theme.Holo.xxxx改成Theme.AppCompat.Light(如果需要的话添加到DarkActionbar)

如果你不需要关于动作栏或AppCompat的高级属性,你不需要使用动作栏或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>