2015-05-21 92 views
0

我想显示包含动态添加的单选按钮的对话框,然后当用户选择单选按钮并按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 
+1

那是什么导致崩溃的例外呢? – Timo

+0

@TimoSta这是logcat快照 http://www.mediafire.com/convkey/59c7/9t4c5mycmyojt3qzg.jpg – aqarain

+0

请将'NullPointerException'的完整堆栈跟踪复制到您的问题中。 – Timo

回答

0

进行变更,它将肯定

工作我。 删除RadioGroup rg;Button btn; onCreate()

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);

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

+1

真棒,谢谢你,完美的工作:) – aqarain

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不可能发现在这一点上。

+0

,所以如何解决这个错误? – aqarain

+0

通过将所有屏幕元素移动到一个活动XML文件并对此活动进行膨胀。您无法使用您在当前活动以外的其他活动中定义的屏幕元素。 – Timo

+0

当我在一个activity.xml中定义所有元素时,仍然不起作用 – aqarain

相关问题