当单击警报对话框的按钮以获取在警报对话框中弹出的单选按钮文本时,应用程序将停止



当我单击布局时,会弹出一个警报对话框,上面有两个单选按钮。我想要的是获取单选按钮文本,并在单击"确定"按钮时将该文本显示在 Toast 中。但是单击"确定"按钮后,应用程序停止工作。为什么?

Methodlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(Add_AlarmActivity.this);
View v=getLayoutInflater().inflate(R.layout.method_pop,null);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
int selectedId=radioGroup.getCheckedRadioButtonId();
radioButton=(RadioButton) findViewById(selectedId);
String s=radioButton.getText().toString();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.setView(v);
AlertDialog dialog=builder.create();
dialog.show();
}
});

这是 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/method_radio"
android:gravity="center_horizontal"
android:layout_margin="10dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Solve math"
android:id="@+id/solve_math"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shake"
android:id="@+id/shake"
android:checked="false"
android:layout_marginRight="15dp"/>
</RadioGroup>
</RelativeLayout>
</RelativeLayout>

[这是日志][1]:https://i.stack.imgur.com/4vgG6.png

01-24 17:40:27.466 24560-24560/com.example.application.alarta E/Android运行时:致命异常:主要 进程: com.example.application.alarta, PID: 24560 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'int android.widget.RadioGroup.getCheckedRadioButtonId()' 在 com.example.application.alarta.Add_AlarmActivity$3$1.onClick(Add_AlarmActivity.java:175) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

好的,您没有初始化RadioGroup

它应该在充气后初始化View v

Methodlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(Add_AlarmActivity.this);
View v=getLayoutInflater().inflate(R.layout.method_pop,null);
final RadioGroup radioGroup = (RadioGroup) v.findViewById(R.id.method_radio);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
int selectedId=radioGroup.getCheckedRadioButtonId();
RadioButton radioButton=(RadioButton) v.findViewById(selectedId);
String s=radioButton.getText().toString();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.setView(v);
AlertDialog dialog=builder.create();
dialog.show();
}
});

我还没有运行代码,所以可能需要进行更改。

最新更新