2017-03-26 9 views
0

필터 기능을 만들려고합니다. 검색 활동에 팝업 창이있어서 사용자가 스위치를 켜고 끌 수있는 스위치 목록을 표시합니다. 나는 팝업 표시 괜찮아요하지만 언제든지 스위치를 틱 때마다 가치가 검색 활동에 전달되고 아무것도 안하고있다. 여기 내 코드입니다 : 나는 "토스트"를 사용하고팝업 내부 스위치 사용

filter.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      initiatePopupWindow(); 
     } 
    }); 

팝업 기능 (SearchActivity 내부)

 private void initiatePopupWindow() { 
     try { 
// We need to get the instance of the LayoutInflater 
      LayoutInflater inflater = (LayoutInflater)SearchActivity.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.activity_filter, 
        (ViewGroup) findViewById(R.id.popup_element)); 
      pwindo = new PopupWindow(layout ,ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT,true); 

      pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0); 

      switch1 = (Switch) findViewById(R.id.parking); 
      switch2 = (Switch) findViewById(R.id.delivery); 
      switch3 = (Switch) findViewById(R.id.reservation); 
      switch4 = (Switch) findViewById(R.id.bar); 
      switch5 = (Switch) findViewById(R.id.card); 
      switch6 = (Switch) findViewById(R.id.wifi); 
      switch7 = (Switch) findViewById(R.id.terrace); 

      btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup); 
      btnClosePopup.setOnClickListener(cancel_button_click_listener); 

      switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) { 
        if (bChecked) { 
         parking_filter=1; 
         Toast.makeText(getApplicationContext(), 
           "Searching Database using : " + parking_filter, 
           Toast.LENGTH_SHORT).show(); 

        } else { 
         parking_filter=0; 
         Toast.makeText(getApplicationContext(), 
           "Searching Database using : " + parking_filter, 
           Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 

activity_filter.xml (스위치)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/popup_element" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@color/cardview_light_background" 
android:padding="10sp" > 
    <Switch 
     android:id="@+id/delivery" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:text="Has Delivery" 
     android:layout_below="@+id/reservation" 
     android:layout_alignParentStart="true" /> 

    <Switch 
     android:id="@+id/parking" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Has Parking" 
     android:layout_marginTop="20dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 
    <Switch 
     android:id="@+id/reservation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Takes Reservations" 
     android:layout_marginTop="20dp" 
     android:layout_below="@+id/terrace" 
     android:layout_alignParentStart="true" /> 
    <Switch 
     android:id="@+id/terrace" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:text="outdoor seating" 
     android:layout_below="@+id/bar" 
     android:layout_alignParentStart="true" /> 
    <Switch 
     android:id="@+id/wifi" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:text="Has Wifi" 
     android:layout_below="@+id/parking" 
     android:layout_alignParentStart="true" /> 

    <Switch 
     android:id="@+id/bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:text="Has Bar" 
     android:layout_below="@+id/wifi" 
     android:layout_alignParentStart="true" /> 

    <Switch 
     android:id="@+id/card" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Accept Cards" 
     android:layout_marginTop="20dp" 
     android:layout_below="@+id/delivery" 
     android:layout_alignParentStart="true" /> 

<Button 
    android:id="@+id/btn_close_popup" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Close" 
    android:layout_below="@+id/card" /> 


</RelativeLayout> 

: 검색 활동 미리보기 "주차 필터"값을 추적하지만 아무 소용이 없습니다. 로그에 있음

java.lang.NullPointerException: Attempt to invoke virtual method 'void  android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference 

답변

0

잘못된 위치에서 찾고 있기 때문에 스위치가 null입니다. 정확한 레이아웃으로 아이디별로 뷰를 찾아야합니다.

switch1 = (Switch) findViewById(R.id.parking); 

해야 할 것 :이

switch1 = (Switch) findViewById(R.id.parking); 

대신이

switch1 = (Switch) layout.findViewById(R.id.parking); 
1

로 변경

switch1 = (Switch) layout.findViewById(R.id.parking);