-1

我正在实现一个应用程序,在我的应用程序中我试图显示一个弹出窗口。每当点击菜单项时,我都会显示一个带有标题和复选框的弹出窗口。在我的代码中,我试图将 setonclick 侦听器设置为 Checkbox,不幸的是,这是导致应用程序崩溃的原因。

以下是我的代码。

在 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;
}
4

1 回答 1

0

你在这里犯了一个错误:

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

错误:您试图从主要 XML 布局中找到 View,而不是从 Dialog 的膨胀布局中找到它。

正确的:

CheckBox cbVoiceCoahcingOn = (CheckBox) layout.findViewById(R.id.cbVoiceCoachingOn);
于 2013-03-20T11:04:45.723 回答