我想显示包含动态添加的单选按钮的对话框,然后当用户选择单选按钮并按下OK按钮时,它应该返回该单选按钮的名称。 我已经制作了应用程序,但是当我尝试在手机上运行时应用程序崩溃了?我正在学习本教程,然后将其用于我的工作。 http://www.codeproject.com/Tips/659766/Android-Custom-DialogBox。
代码导致应用崩溃的错误是什么?
MainActivity.java
public class MainActivity extends ActionBarActivity {
String countryName[] = { "India", "Pakistan", "China", "Nepal", "Bangladesh" };
TextView textDialog;
RadioGroup rg;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textDialog = (TextView)findViewById(R.id.textView1);
showCustomDialog(textDialog);
}
public void showCustomDialog(final TextView _textDialog) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_main);
rg = (RadioGroup)findViewById(R.id.radio_group);
btn = (Button)findViewById(R.id.button);
final RadioButton[] rb = new RadioButton[countryName.length];
rg.setOrientation(RadioGroup.VERTICAL);
for (int i = 0; i < countryName.length; i++) {
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText(countryName[i]);
}
Button button = (Button)dialog.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = "";
if(rg.getCheckedRadioButtonId()==-1){
Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
}
else{
int selectedId = rg.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
text = selectedRadioButton.getText().toString();
Toast.makeText(getApplicationContext(), selectedRadioButton.getText().toString()+" is selected", Toast.LENGTH_SHORT).show();
}
_textDialog.setText(text);
dialog.dismiss();
}
});
dialog.show();
}
}
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:text="TextView" />
</RelativeLayout>
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</RadioGroup>
</ScrollView>
<Button
android:layout_below="@+id/scrollView"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
05-21 05:48:06.291: E/AndroidRuntime(14823): FATAL EXCEPTION: main
05-21 05:48:06.291: E/AndroidRuntime(14823): Process: com.example.dialogbox, PID: 14823
05-21 05:48:06.291: E/AndroidRuntime(14823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dialogbox/com.example.dialogbox.MainActivity}: java.lang.NullPointerException
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.os.Handler.dispatchMessage(Handler.java:102)
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.os.Looper.loop(Looper.java:136)
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.app.ActivityThread.main(ActivityThread.java:5105)
05-21 05:48:06.291: E/AndroidRuntime(14823): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 05:48:06.291: E/AndroidRuntime(14823): at java.lang.reflect.Method.invoke(Method.java:515)
05-21 05:48:06.291: E/AndroidRuntime(14823): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
05-21 05:48:06.291: E/AndroidRuntime(14823): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
05-21 05:48:06.291: E/AndroidRuntime(14823): at dalvik.system.NativeStart.main(Native Method)
05-21 05:48:06.291: E/AndroidRuntime(14823): Caused by: java.lang.NullPointerException
05-21 05:48:06.291: E/AndroidRuntime(14823): at com.example.dialogbox.MainActivity.showCustomDialog(MainActivity.java:42)
05-21 05:48:06.291: E/AndroidRuntime(14823): at com.example.dialogbox.MainActivity.onCreate(MainActivity.java:28)
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.app.Activity.performCreate(Activity.java:5275)
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-21 05:48:06.291: E/AndroidRuntime(14823): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
05-21 05:48:06.291: E/AndroidRuntime(14823): ... 11 more
答案 0 :(得分:0)
rg
在此行中为空:
rg.setOrientation(RadioGroup.VERTICAL);
这意味着findViewById()
无法找到您在此行中设置时传递的ID:
rg = (RadioGroup)findViewById(R.id.radio_group);
如果findViewById()
找不到指定的ID,则会返回null
,因为您可以阅读in the docs。
这是因为R.id.radio_group
位于活动activity_main
中,但您却夸大了main
:
setContentView(R.layout.main);
因此,您的R.id.radio_group
此时无法找到。
答案 1 :(得分:0)
进行这些更改,肯定会有效
我。在onCreate()之前删除 RadioGroup rg;
和Button btn;
II。 将 rg = (RadioGroup)findViewById(R.id.radio_group);
替换为final RadioGroup rg = (RadioGroup)dialog.findViewById(R.id.radio_group);
III。 删除 btn = (Button)findViewById(R.id.button);
IV。 将 Button button = (Button)findViewById(R.id.button);
替换为Button button = (Button)dialog.findViewById(R.id.button);
诉在其他地方,将 RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId);
替换为RadioButton selectedRadioButton = (RadioButton)dialog.findViewById(selectedId);