2013-11-15 7 views
0

В settings.xml:переключатель в всплывающем окне NullPointerException

..... 
<Switch 
    android:id="@+id/flmode" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_marginLeft="30dp" 
    android:layout_toRightOf="@+id/textView1" /> 

<Switch 
    android:id="@+id/fmode" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView2" 
    android:layout_marginLeft="30dp" 
    android:layout_toRightOf="@+id/textView2"/> 
..... 

вызова всплывающего:

public void showPopup(View anchorView) { 

    View popupView = getLayoutInflater().inflate(R.layout.settings, null); 

    PopupWindow popupWindow = new PopupWindow(popupView, 
          LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

    Switch flmode = (Switch) findViewById(R.id.flmode); 

    flmode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ 

     } 
    });   

    Switch fmode = (Switch) findViewById(R.id.fmode); 
    fmode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ 
     } 
    }); 

    popupWindow.setFocusable(true); 

    popupWindow.setBackgroundDrawable(new ColorDrawable()); 

    int location[] = new int[2]; 

    anchorView.getLocationOnScreen(location); 

    popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, 
            location[0], location[1] + anchorView.getHeight()); 

} 

после запуска ошибки:

11-15 09:26:08.302: E/AndroidRuntime(11433): FATAL EXCEPTION: main 
11-15 09:26:08.302: E/AndroidRuntime(11433): java.lang.NullPointerException 
11-15 09:26:08.302: E/AndroidRuntime(11433): at com.test.com.MainFirst.showPopup(MainFirst.java:507) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at com.test.com.MainFirst.onClick(MainFirst.java:242) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at android.view.View.performClick(View.java) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at android.view.View$PerformClick.run(View.java) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at android.os.Handler.handleCallback(Handler.java) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at android.os.Handler.dispatchMessage(Handler.java) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at android.os.Looper.loop(Looper.java) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at android.app.ActivityThread.main(ActivityThread.java) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at java.lang.reflect.Method.invokeNative(Native Method) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at java.lang.reflect.Method.invoke(Method.java:511) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 
11-15 09:26:08.302: E/AndroidRuntime(11433): at dalvik.system.NativeStart.main(Native Method) 

, что пошло не так, и как реализовать его? заранее спасибо

ответ

2

Вам нужно сделать

Switch flmode = (Switch) popupView.findViewById(R.id.flmode); 
Switch fmode = (Switch) popupView.findViewById(R.id.fmode); 

вместо

Switch flmode = (Switch) findViewById(R.id.flmode); 
Switch fmode = (Switch) findViewById(R.id.fmode);  
+0

Большое спасибо – Forme

+0

Вы можете. Пожалуйста, примите ответ, если это поможет. – Apoorv

+0

Помогли. Я жду 10 минут, чтобы принять ответ – Forme

Смежные вопросы