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>

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

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


当前回答

将您的主题样式父样式更改为

 parent="Theme.AppCompat"

这对我很管用……

其他回答

您的活动正在扩展ActionBarActivity,这需要AppCompat。应用的主题。 将ActionBarActivity更改为Activity或FragmentActivity,就可以解决问题。

如果你不使用操作栏,那么:

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

这个对我很有用:

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

我在三星设备上有这样的崩溃,即使活动使用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

我的解决方案只是删除android:launchMode="singleTask"

如果在扩展布局时使用getApplicationContext()作为参数,则可以尝试使用getBaseContext()。如。

    View.inflate(getBaseContext(),
            getLayoutId() == 0 ? R.layout.page_default : getLayoutId(),
            null);

我的活动与SectionsPagerAdapter和ViewPager & Fragment

public class MyActivity extends AppCompatActivity implements ActionBar.TabListener
...
...
     @Override
        public void onPostResume(){
            super.onPostResume();
            try {
               getSupportActionBar().setDisplayShowTitleEnabled(false);
            }catch (NullPointerException ignored){
            }
        }