这已被问了很多次,并且已经回答了,我之前已经解决了这个问题,但是这次停留了,这是在一个地方工作但在另一个活动中它给我以下错误信息。
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
以下是我的注册类的代码,为登录活动编写了相同的类,并且它的工作完美。
public class RegistrationActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
if(sql_code.equalsIgnoreCase("0")){
String resultCode= command1.getString("result");
if(resultCode.equalsIgnoreCase("0")){
AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
alertDialog.setTitle("Account Created");
alertDialog.setMessage("Account Created Successfully.");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i= new Intent(RegistrationActivity.this, LoginActivity.class);
startActivity(i);
}
});
alertDialog.show();
清单文件
<activity
android:name=".ticketing.activities.checkout.RegistrationActivity"
android:screenOrientation="portrait"
android:label="@string/title_activity_registration"
android:theme="@style/AppTheme" />
Style.xml
<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>
<item name="android:fontFamily">Roboto</item>
</style>
一切都已到位,但仍然给我这个错误信息,请指导我在这里做错了什么。
答案 0 :(得分:1)
//instead of using it
AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
//use It
AlertDialog alertDialog = new AlertDialog.Builder(RegistrationActivity.this).create();
答案 1 :(得分:1)
使用此构建器创建对话框:
new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
解决此问题的另一种方法是创建自定义样式:
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">_your_color_</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">_your_color_</item>
<!-- Used for the background -->
<item name="android:background">_your_color_</item>
然后在构建对话框时使用它:
new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.MyAlertDialogStyle))
答案 2 :(得分:1)
使用此代码中的第一行替换对话框创建
AlertDialog.Builder alertDialog= new AlertDialog.Builder(context);
alertDialog.setTitle("Account Created");
alertDialog.setMessage("Account Created Successfully.");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i= new Intent(RegistrationActivity.this, LoginActivity.class);
startActivity(i);
}
});
alertDialog.show();
答案 3 :(得分:0)
将extends AppCompatActivity
更改为extends Activity
。
正如您在此处所见 - https://stackoverflow.com/a/21815015/4425195
答案 4 :(得分:-1)
使用DialogFragment
进行对话