2015-05-21 128 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

相關問題