我正在尝试使用 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)
请告诉我如何解决这个问题。在此先感谢。