0

我已经从 Facebook 的 android 教程中复制了注销代码。每次我尝试注销时,都会出现空指针异常。

这是代码...

private AsyncFacebookRunner mAsyncRunner;

...

              mAsyncRunner.logout(home.this, new RequestListener() {
              @Override
              public void onComplete(String response, Object state) 
              {
                 /* mPrefs.edit().putBoolean("firstTime", true).commit();
                  userName.setText("Logged out!");*/
              }

              @Override
              public void onIOException(IOException e, Object state) {
                  Log.d("IOExcenption", e.toString());
              }

              @Override
              public void onFileNotFoundException(FileNotFoundException e,
                    Object state) {
                  Log.d("FileNotFoundException", e.toString());
              }

              @Override
              public void onMalformedURLException(MalformedURLException e,
                    Object state) {
                  Log.d("MalformedUrlExpection", e.toString());
              }

              @Override
              public void onFacebookError(FacebookError e, Object state) {
                  Log.d("FacebookError", e.toString());
              }
            });

日志

 04-29 23:41:35.523: E/AndroidRuntime(1011): java.lang.NullPointerException
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at rageup.android.home.onClick(home.java:177)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.view.View.performClick(View.java:2364)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.view.View.onTouchEvent(View.java:4179)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.widget.TextView.onTouchEvent(TextView.java:6541)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.view.View.dispatchTouchEvent(View.java:3709)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.os.Handler.dispatchMessage(Handler.java:99)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.os.Looper.loop(Looper.java:123)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at android.app.ActivityThread.main(ActivityThread.java:4363)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at java.lang.reflect.Method.invokeNative(Native Method)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at java.lang.reflect.Method.invoke(Method.java:521)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 04-29 23:41:35.523: E/AndroidRuntime(1011):    at dalvik.system.NativeStart.main(Native Method)
 04-29 23:41:35.534: I/dalvikvm(1011): threadid=7: reacting to signal 3
 04-29 23:41:35.534: E/dalvikvm(1011): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
 04-29 23:41:37.283: I/Process(1011): Sending signal. PID: 1011 SIG: 9

关于为什么会发生这种情况的任何建议?空指针异常是指第二个代码块的第一行。

4

3 回答 3

1

你的mAsyncRunner似乎是null

你初始化了吗?

像这样初始化它:

mAsyncRunner = new AsyncFacebookRunner(mFacebook);  //mFacebook is your facebook object.
于 2012-04-30T07:10:16.810 回答
0

试试下面的代码,

public void logout(final Context context,
                   final RequestListener listener,
                   final Object state) {
    new Thread() {
        @Override public void run() {
            try {
                String response = fb.logout(context);
                if (response.length() == 0 || response.equals("false")){
                    listener.onFacebookError(new FacebookError(
                            "auth.expireSession failed"), state);
                    return;
                }
                listener.onComplete(response, state);
            } catch (FileNotFoundException e) {
                listener.onFileNotFoundException(e, state);
            } catch (MalformedURLException e) {
                listener.onMalformedURLException(e, state);
            } catch (IOException e) {
                listener.onIOException(e, state);
            }
        }
    }.start();
}

public void logout(final Context context, final RequestListener listener) {
    logout(context, listener, /* state */ null);
}
于 2012-04-30T07:06:52.410 回答
0

检查上下文是否为空并尝试使用

 facebook.logout(this);
于 2012-04-30T07:18:08.090 回答