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>
创建这样的对话框是我一直在做的事情,因为我已经完成了布局。
谁能告诉我如何解决这个问题?
我也有这个问题,我做了什么来解决它,仍然使用Holo主题是采取以下步骤:
首先我替换了这个导入:
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构建文件中的这个条目:
compile 'com.android.support:appcompat-v7:23.2.1'
如果你的项目中有其他类必须使用v7 appCompat,那么只需清理并重新构建项目。
当你使用Kotlin时,没有什么能解决我的问题,直到我将生成器参数从“applicationContext”更改为“this”。
这行不通
val dialogDelete = AlertDialog.Builder(applicationContext)
.setTitle("Confirmation")
.setMessage("Delete this photo?")
.setNegativeButton(android.R.string.no){ it, which ->
it.dismiss()
}
dialogDelete.show()
遵循代码工作
val dialogDelete = AlertDialog.Builder(this)
.setTitle("Confirmation")
.setMessage("Delete this photo?")
.setNegativeButton(android.R.string.no){ it, which ->
it.dismiss()
}
dialogDelete.show()
根据我的经验,问题在于我展示对话的环境。
在一个按钮点击中,我以这样的方式实例化了一个AlertDialog:
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();
}
}
}
这就是我的解决方案。