我正在制作一项活动,一旦开始,就必须将注册表格显示为对话框。但是,活动总是崩溃。 以下是活动的代码:
public class CheckInActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_in);
getDialogue();
}
public void getDialogue(){
//Pop dialogue
AlertDialog.Builder mBuilder= new AlertDialog.Builder(getApplicationContext());
View mView= getLayoutInflater().inflate(R.layout.dialog_register, null);
final EditText mPhone= (EditText) mView.findViewById(R.id.etPhone);
final EditText mPass= (EditText) mView.findViewById(R.id.etPass);
final EditText mRtPass= (EditText) mView.findViewById(R.id.etRtPass);
Button mRegisterButton = (Button) mView.findViewById(R.id.btnRegister);
mBuilder.setView(mView);
AlertDialog dialog= mBuilder.create();
dialog.show();
}
}
这是activity_check_in.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.shashank_pc.trial.CheckInActivity">
</android.support.constraint.ConstraintLayout>
这是android_manifest.xml文件中的活动
<activity android:name=".CheckInActivity"
android:theme="@style/Theme.AppCompat">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
错误消息如下:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
请帮助!!
答案 0 :(得分:0)
尝试AlertDialog.Builder mBuilder= new AlertDialog.Builder(this);
答案 1 :(得分:0)
只需替换此行
AlertDialog.Builder mBuilder= new AlertDialog.Builder(getApplicationContext());
<强>与强>
android.support.v7.app.AlertDialog.Builder mBuilder= new android.support.v7.app.AlertDialog.Builder(getApplicationContext());
或者只是将android:theme="@style/Theme.AppCompat.Light"
添加到AndroidManifest.xml文件中的应用程序代码中。