2012-09-11 90 views
0

我正在使用一個名爲logout的按鈕。此註銷用於從應用程序中爲用戶提供的Facebook註銷選項。當我單擊此按鈕時,會出現錯誤。錯誤:Android中按鈕單擊的NullPointerException

我在下面的代碼中註釋了哪裏出現此錯誤。

09-11 15:25:30.124: E/AndroidRuntime(6866): FATAL EXCEPTION: main 
> 09-11 15:25:30.124: E/AndroidRuntime(6866): 
> java.lang.NullPointerException 09-11 15:25:30.124: 
> E/AndroidRuntime(6866): at 
> com.example.settings.Settings$1.logout(Settings.java:46) 09-11 
> 15:25:30.124: E/AndroidRuntime(6866):  at 
> com.example.settings.Settings$1.onClick(Settings.java:38) 09-11 
> 15:25:30.124: E/AndroidRuntime(6866):  at 
> android.view.View.performClick(View.java:2485) 09-11 15:25:30.124: 
> E/AndroidRuntime(6866): at 
> android.view.View$PerformClick.run(View.java:9080) 09-11 15:25:30.124: 
> E/AndroidRuntime(6866): at 
> android.os.Handler.handleCallback(Handler.java:587) 09-11 
> 15:25:30.124: E/AndroidRuntime(6866):  at 
> android.os.Handler.dispatchMessage(Handler.java:92) 09-11 
> 15:25:30.124: E/AndroidRuntime(6866):  at 
> android.os.Looper.loop(Looper.java:123) 09-11 15:25:30.124: 
> E/AndroidRuntime(6866): at 
> android.app.ActivityThread.main(ActivityThread.java:3683) 09-11 
> 15:25:30.124: E/AndroidRuntime(6866):  at 
> java.lang.reflect.Method.invokeNative(Native Method) 09-11 
> 15:25:30.124: E/AndroidRuntime(6866):  at 
> java.lang.reflect.Method.invoke(Method.java:507) 09-11 15:25:30.124: 
> E/AndroidRuntime(6866): at 
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
> 09-11 15:25:30.124: E/AndroidRuntime(6866): at 
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-11 
> 15:25:30.124: E/AndroidRuntime(6866):  at 
> dalvik.system.NativeStart.main(Native Method) 

我的代碼:

Facebook的FB;

Button btn_logout = (Button)findViewById(R.id.logout); 
       btn_logout.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String response = logout();//error 
      Toast.makeText(getApplicationContext(), "Logout"+ response , 2).show(); 
     } 

     public String logout() { 
      // TODO Auto-generated method stub 
      String res = null; 
      try { 
        res = fb.logout(getApplicationContext());//error 

      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return res; 
     } 


    }); 
+1

你初始化你的'Facebook的FB;'對象? – user370305

回答

-1

你可能忘了初始化的Facebook FB;

Facebook fb= new Facebook("APP_ID"); 
+1

你確定這可能是原因嗎?順便說一句,我不這麼認爲。 –

+0

請參閱我編輯的ans – MAC

+0

類型new View.OnClickListener(){}中的方法logout()不適用於當我將V放入代碼中時參數(View) –

0

確保Facebook對象(fb)初始化...

Facebook fb= new ....