对话框不显示和应用程序崩溃

Dialogbox not showing and app crashes

我想显示包含动态添加的单选按钮的对话框,然后当用户选择单选按钮并按确定按钮时,它应该 return 该单选按钮的名称。 我已经制作了该应用程序,但是当我尝试在我的手机上 运行 时该应用程序崩溃了?我正在学习本教程,然后将其用于我的工作。 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.access0(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

rg 在此行中为空:

rg.setOrientation(RadioGroup.VERTICAL);

这意味着findViewById()找不到您在这一行设置时传递的id:

rg = (RadioGroup)findViewById(R.id.radio_group);

如果findViewById()没有找到指定的id,它returnsnull如你所见in the docs.

这是因为 R.id.radio_group 位于 activity activity_main,但是你夸大了 main:

setContentView(R.layout.main);

因此,目前无法找到您的 R.id.radio_group

进行这些更改,它肯定会起作用

我。 在 onCreate()

之前删除 RadioGroup rg;Button btn;

二。 rg = (RadioGroup)findViewById(R.id.radio_group);替换为final RadioGroup rg = (RadioGroup)dialog.findViewById(R.id.radio_group);

三。 移除 btn = (Button)findViewById(R.id.button);

四。 Button button = (Button)dialog.findViewById(R.id.button);

替换Button button = (Button)findViewById(R.id.button);

v。在其他情况下, RadioButton selectedRadioButton = (RadioButton)findViewById(selectedId); 替换为 RadioButton selectedRadioButton = (RadioButton)dialog.findViewById(selectedId);