Checkbox setOnclicListner导致android崩溃

时间:2013-03-20 10:57:09

标签: android checkbox

我正在实现一个应用程序,在我的应用程序中,我试图显示一个Popwindow。每当点击菜单项时,我都会显示一个带标题和复选框的弹出窗口。在那个我的代码我试图setonclick侦听器到Checkbox,不幸的是这是崩溃应用程序的cuases。以下是我的代码,请看一下并建议我。

在xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
android:id="@+id/popup"
android:layout_height="wrap_content"
android:background="@drawable/music_bg"
android:orientation="vertical" >
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <TextView android:id="@+id/txtTopTitleForSetInfoInput" android:text="Voice Coaching"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:textColor="#FFFFFF" android:textSize="21sp" android:gravity="center"
        android:textStyle="bold" android:layout_centerVertical="true" />

    <Button android:id="@+id/btnCloseControl" 
        android:background="@drawable/music_close"
        android:layout_width="40dip" android:layout_height="40dip"
        android:layout_alignParentRight="true" android:layout_centerVertical="true"
         />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip" >
        <TextView
        android:id="@+id/txtVoiceCoaching"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/cbVoiceCoachingOn"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/cbVoiceCoachingOn"
        android:gravity="center"
        android:text="Voice Coaching"
        android:textColor="#FFFFFF"
        android:textSize="21sp"
        android:textStyle="bold" />
        <CheckBox
            android:id="@+id/cbVoiceCoachingOn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/txtVoiceCoaching" />
</RelativeLayout>

                              

所选菜单项中的代码:

 @Override
 public boolean onMenuItemSelected(int featureId, MenuItem item) 
 {
    super.onMenuItemSelected(featureId, item);

            int[] location = new int[2];

            ll.getLocationOnScreen(location);

                int popupWidth = 300;
               int popupHeight = 160;

               Point p = new Point();
               p.x = location[0];
               p.y = location[1];

               // Inflate the popup_layout.xml
               LinearLayout viewGroup = (LinearLayout) this.findViewById(R.id.popup);
               LayoutInflater layoutInflater = (LayoutInflater) this
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               View layout = layoutInflater.inflate(R.layout.volume_pop_layout, viewGroup);

               // Creating the PopupWindow
               final PopupWindow popup = new PopupWindow(this);
               popup.setContentView(layout);
               popup.setWidth(popupWidth);
               popup.setHeight(popupHeight);
               popup.setFocusable(true);

               // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
               int OFFSET_X = 10;
               int OFFSET_Y = -40;

               // Clear the default translucent background
               popup.setBackgroundDrawable(new BitmapDrawable());

               // Displaying the popup at the specified location, + offsets.
               popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

               Button close = (Button) layout.findViewById(R.id.btnCloseControl);
               close.setOnClickListener(new OnClickListener() {

                 @Override
                 public void onClick(View v) {
                   popup.dismiss();
                 }
               });

               CheckBox cbVoiceCoahcingOn = (CheckBox) findViewById(R.id.cbVoiceCoachingOn);
               if(cbVoiceCoahcingOn==null)
               {
                   System.out.println("cbVoiceCoahcingOn==null");
               }

               cbVoiceCoahcingOn.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                       System.out.println("onclick");
                     }
                   });

    return true;
}

1 个答案:

答案 0 :(得分:0)

你在这里弄错了:

CheckBox cbVoiceCoahcingOn = (CheckBox) findViewById(R.id.cbVoiceCoachingOn);

错误:您正在尝试从主XML布局中找到View,而不是从Dialog的inflate布局中找到它。

正确:

CheckBox cbVoiceCoahcingOn = (CheckBox) layout.findViewById(R.id.cbVoiceCoachingOn);