单选按钮和警报对话框中的textview

时间:2012-07-15 11:18:59

标签: android

我正在尝试使用3个单选按钮和textview创建一个对话框警告框。但是当我尝试使用3个单选按钮运行它时,它运行正常,因为我添加了textview,应用程序显示异常。

private void Dialog(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    //  tv1=new TextView(this);
    //  rb1.setText("these are the time limit options for you");
        rb1=new RadioButton(this);
        rb1.setText("10 minutes");
        rb2=new RadioButton(this);
        rb2.setText("20 minutes");
        rb3=new RadioButton(this);
        rb3.setText("30 minutes");
        rb1.setOnClickListener(RadioOptionOnClickListener);
        rb2.setOnClickListener(RadioOptionOnClickListener);
        rb3.setOnClickListener(RadioOptionOnClickListener);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams( new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.FILL_PARENT));
        linearLayout.setOrientation(1); 
private void Dialog(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    //  tv1=new TextView(this);
    //  rb1.setText("these are the time limit options for you");
        rb1=new RadioButton(this);
        rb1.setText("10 minutes");
        rb2=new RadioButton(this);
        rb2.setText("20 minutes");
        rb3=new RadioButton(this);
        rb3.setText("30 minutes");
        rb1.setOnClickListener(RadioOptionOnClickListener);
        rb2.setOnClickListener(RadioOptionOnClickListener);
        rb3.setOnClickListener(RadioOptionOnClickListener);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams( new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
        LinearLayout.LayoutParams.FILL_PARENT));
        linearLayout.setOrientation(1);  
    //  linearLayout.addView(tv1);
        linearLayout.addView(rb1);
        linearLayout.addView(rb2);
        linearLayout.addView(rb3);
        alertDialog.setView(linearLayout);
        alertDialog.show();
    }

这是例外先生

07-15 16:23:30.081: W/dalvikvm(1129): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-15 16:23:30.248: E/AndroidRuntime(1129): FATAL EXCEPTION: main
07-15 16:23:30.248: E/AndroidRuntime(1129): java.lang.NullPointerException
07-15 16:23:30.248: E/AndroidRuntime(1129):     at test.test.AlertdialogActivity.Dialog(AlertdialogActivity.java:32)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at test.test.AlertdialogActivity.access$0(AlertdialogActivity.java:29)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at test.test.AlertdialogActivity$1.onClick(AlertdialogActivity.java:59)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at android.view.View.performClick(View.java:2485)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at android.view.View$PerformClick.run(View.java:9080)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at android.os.Handler.handleCallback(Handler.java:587)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at android.os.Looper.loop(Looper.java:123)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at java.lang.reflect.Method.invoke(Method.java:507)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-15 16:23:30.248: E/AndroidRuntime(1129):     at dalvik.system.NativeStart.main(Native Method)

请告诉我如何解决这个问题。谢谢。

2 个答案:

答案 0 :(得分:1)

我认为这是一个错字,你写道:

  tv1=new TextView(this);
  rb1.setText("these are the time limit options for you"); 

虽然它应该是

 tv1=new TextView(this);
 tv1.setText("these are the time limit options for you"); 

你所做的是一个Null异常,因为rb1将在下一行创建。

答案 1 :(得分:0)

//tv1=new TextView(this); 
//rb1.setText("these are the time limit options for you");

此处您初始化tv1,但设置rb1的值。

尝试

tv1=new TextView(this); 
tv1.setText("these are the time limit options for you");