我正在使用一個名爲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;
}
});
你初始化你的'Facebook的FB;'對象? – user370305