我想顯示包含動態添加的單選按鈕的對話框,然後當用戶選擇單選按鈕並按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>
logcat的
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
那是什麼導致崩潰的例外呢? – Timo
@TimoSta這是logcat快照 http://www.mediafire.com/convkey/59c7/9t4c5mycmyojt3qzg.jpg – aqarain
請將'NullPointerException'的完整堆棧跟蹤複製到您的問題中。 – Timo