1

我正在创建一个简单的应用程序来获取和发布任何 Facebook 帐户上的一些信息!

我无法通过登录和退出。我查看了 facebook 教程网站,但无济于事!我已经讨论这个问题好几天了。

这是我写的代码。我需要知道我在哪里搞砸了。代码构建没有错误,但当我单击“登录”按钮时崩溃。

我还在代码之后复制了 LogCat。

package com.thenewboston.travis;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.android.*;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.Facebook.*;

public class FbExample extends Activity implements OnClickListener {

Facebook facebook = new Facebook("xxxxxxx");
AsyncFacebookRunner mAsyncRunner;
Button login;
TextView logoutResult;
private SharedPreferences mPrefs;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fbexample);
    login = (Button) findViewById(R.id.bLogin);
    logoutResult = (TextView) findViewById(R.id.tvLogoutResult);
    login.setOnClickListener(this);
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);
    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }
    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId() == R.id.bLogin) {
        if (!facebook.isSessionValid()) {
            facebook.authorize(this, new String[] {}, new DialogListener() {
                public void onComplete(Bundle values) {
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token",
                            facebook.getAccessToken());
                    editor.putLong("access_expires",
                            facebook.getAccessExpires());
                    editor.commit();
                    login.setText("Logout");

                }

                public void onFacebookError(FacebookError error) {
                }

                public void onError(DialogError e) {
                }

                public void onCancel() {
                }
            });
        }
        if (facebook.isSessionValid()) {
            mAsyncRunner.logout(this,
                    new RequestListener() {

                        public void onComplete(String response, Object state) {
                            login.setText("Login");
                        }

                        public void onIOException(IOException e,
                                Object state) {
                            String error = e.getMessage();
                            logoutResult.setText(error);

                        }

                        public void onFileNotFoundException(
                                FileNotFoundException e, Object state) {
                            String error = e.getMessage();
                            logoutResult.setText(error);
                        }

                        public void onMalformedURLException(
                                MalformedURLException e, Object state) {
                            String error = e.getMessage();
                            logoutResult.setText(error);
                        }

                        public void onFacebookError(FacebookError e,
                                Object state) {
                            String error = e.getMessage();
                            logoutResult.setText(error);
                        }
                    });

        }
    }

}
}

日志猫

08-02 02:20:26.859: W/dalvikvm(302): threadid=1: thread exiting with uncaught
exception (group=0x4001d800)
08-02 02:20:26.878: E/AndroidRuntime(302): FATAL EXCEPTION: main
08-02 02:20:26.878: E/AndroidRuntime(302): java.lang.NullPointerException
08-02 02:20:26.878: E/AndroidRuntime(302):  at     com.thenewboston.travis.FbExample.onClick(FbExample.java:81)
08-02 02:20:26.878: E/AndroidRuntime(302):  at android.view.View.performClick(View.java:2408)
08-02 02:20:26.878: E/AndroidRuntime(302):  at android.view.View$PerformClick.run(View.java:8816)
08-02 02:20:26.878: E/AndroidRuntime(302):  at android.os.Handler.handleCallback(Handler.java:587)
08-02 02:20:26.878: E/AndroidRuntime(302):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-02 02:20:26.878: E/AndroidRuntime(302):  at android.os.Looper.loop(Looper.java:123)
08-02 02:20:26.878: E/AndroidRuntime(302):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-02 02:20:26.878: E/AndroidRuntime(302):  at java.lang.reflect.Method.invokeNative(Native Method)
08-02 02:20:26.878: E/AndroidRuntime(302):  at java.lang.reflect.Method.invoke(Method.java:521)
08-02 02:20:26.878: E/AndroidRuntime(302):  at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-02 02:20:26.878: E/AndroidRuntime(302):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-02 02:20:26.878: E/AndroidRuntime(302):  at dalvik.system.NativeStart.main(Native Method)
08-02 02:20:29.368: I/Process(302): Sending signal. PID: 302 SIG: 9
4

2 回答 2

0

您没有在调用 mAsyncRunner.logout() 之前实例化 mAsyncRunner

您可以更改以下行:

 AsyncFacebookRunner mAsyncRunner;

 AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);

请让我知道这是否有帮助。

于 2012-08-02T06:30:48.363 回答
0

在第 81 行有一个空指针异常。放置一个断点,看看它为什么为空。

于 2012-08-01T21:52:22.813 回答