-1

我正在尝试使用 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)

请告诉我如何解决这个问题。在此先感谢。

4

2 回答 2

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 将在下一行创建。

于 2012-07-15T11:32:07.690 回答
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");
于 2012-07-15T11:37:23.453 回答