我的项目中有简单的radioGroup。当我设置监听器,然后按菜单显示包含该RadioGroup的对话框时,它将显示“不幸的是,已停止”。当我删除代码“... setOnCheckedChangeListener”时,它将显示该对话框,并且没有错误。我已经阅读了很多来源将侦听器设置为radioGroup,我不知道为什么我的应用程序仍然有问题。请帮我。这是我的代码:
private void drawRectangle()
{
currentDialog = new Dialog(this);
currentDialog.setContentView(R.layout.draw_rectangle_dialog);
currentDialog.setTitle(R.string.title_draw_rectangle_dialog);
currentDialog.setCancelable(true);
// get the color SeekBars and set their onChange listeners
final SeekBar alphaSeekBar =
(SeekBar) currentDialog.findViewById(R.id.alphaSeekBar);
final SeekBar redSeekBar =
(SeekBar) currentDialog.findViewById(R.id.redSeekBar);
final SeekBar greenSeekBar =
(SeekBar) currentDialog.findViewById(R.id.greenSeekBar);
final SeekBar blueSeekBar =
(SeekBar) currentDialog.findViewById(R.id.blueSeekBar);
// register SeekBar event listeners
alphaSeekBar.setOnSeekBarChangeListener(drawRectangleSeekBarChanged);
redSeekBar.setOnSeekBarChangeListener(drawRectangleSeekBarChanged);
greenSeekBar.setOnSeekBarChangeListener(drawRectangleSeekBarChanged);
blueSeekBar.setOnSeekBarChangeListener(drawRectangleSeekBarChanged);
// use current drawing color to set SeekBar values
alphaSeekBar.setProgress(255);
redSeekBar.setProgress(255);
greenSeekBar.setProgress(255);
blueSeekBar.setProgress(255);
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.fill_rectangle);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
{};
});
// set the Set Color Button's onClickListener
Button setColorButton = (Button) currentDialog.findViewById(
R.id.drawRectangleButton);
setColorButton.setOnClickListener(drawRectangleButtonListener);
dialogIsVisible.set(true); // dialog is on the screen
currentDialog.show(); // show the dialog
}
这是我的XML代码的一部分:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_fill" android:gravity="right"
android:layout_gravity="center_vertical"/>
<RadioGroup
android:id="@+id/fill_rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/fillYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_yes"/>
<RadioButton
android:id="@+id/fillNo"
android:layout_width="wrap_content"
android:la![enter image description here][1]yout_height="wrap_content"
android:text="@string/label_no"/>
</RadioGroup>
</LinearLayout>
这是logcat:
06-29 22:47:37.572: E/AndroidRuntime(11472): FATAL EXCEPTION: main
06-29 22:47:37.572: E/AndroidRuntime(11472): Process: com.deitel.doodlz, PID: 11472
06-29 22:47:37.572: E/AndroidRuntime(11472): java.lang.NullPointerException
06-29 22:47:37.572: E/AndroidRuntime(11472): at com.deitel.doodlz.Doodlz.drawRectangle(Doodlz.java:569)
06-29 22:47:37.572: E/AndroidRuntime(11472): at com.deitel.doodlz.Doodlz.onOptionsItemSelected(Doodlz.java:230)
06-29 22:47:37.572: E/AndroidRuntime(11472): at android.app.Activity.onMenuItemSelected(Activity.java:2650)
06-29 22:47:37.572: E/AndroidRuntime(11472): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1084)
06-29 22:47:37.572: E/AndroidRuntime(11472): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
06-29 22:47:37.572: E/AndroidRuntime(11472): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
06-29 22:47:37.572: E/AndroidRuntime(11472): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
06-29 22:47:37.572: E/AndroidRuntime(11472): at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:177)
06-29 22:47:37.572: E/AndroidRuntime(11472): at android.widget.AdapterView.performItemClick(AdapterView.java:308)
06-29 22:47:37.572: E/AndroidRuntime(11472): at android.widget.AbsListView.performItemClick(AbsListView.java:1476)
06-29 22:47:37.572: E/AndroidRuntime(11472): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3497)
06-29 22:47:37.572: E/AndroidRuntime(11472): at android.widget.AbsListView$3.run(AbsListView.java:4814)
06-29 22:47:37.572: E/AndroidRuntime(11472): at android.os.Handler.handleCallback(Handler.java:733)
06-29 22:47:37.572: E/AndroidRuntime(11472): at android.os.Handler.dispatchMessage(Handler.java:95)
06-29 22:47:37.572: E/AndroidRuntime(11472): at android.os.Looper.loop(Looper.java:157)
06-29 22:47:37.572: E/AndroidRuntime(11472): at android.app.ActivityThread.main(ActivityThread.java:5293)
06-29 22:47:37.572: E/AndroidRuntime(11472): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 22:47:37.572: E/AndroidRuntime(11472): at java.lang.reflect.Method.invoke(Method.java:515)
06-29 22:47:37.572: E/AndroidRuntime(11472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-29 22:47:37.572: E/AndroidRuntime(11472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-29 22:47:37.572: E/AndroidRuntime(11472): at dalvik.system.NativeStart.main(Native Method)