0

嗨,我已将 facebook 集成到我的 android 应用程序中。下面是代码:

public class Example extends Activity {


public static final String APP_ID = "My App ID";//i have placed my app id here

private LoginButton mLoginButton;
private TextView mText;
private Button mPostButton;


private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (APP_ID == null) {
        Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " +
                "specified before running this example: see Example.java");
    }

    setContentView(R.layout.main);
    mLoginButton = (LoginButton) findViewById(R.id.login);
    mText = (TextView) Example.this.findViewById(R.id.txt);

    mPostButton = (Button) findViewById(R.id.postButton);

    **mFacebook = new Facebook(APP_ID);**//this is where i get a Null Pointer Exception
    mAsyncRunner = new AsyncFacebookRunner(mFacebook);

    SessionStore.restore(mFacebook, this);
    SessionEvents.addAuthListener(new SampleAuthListener());
    SessionEvents.addLogoutListener(new SampleLogoutListener());
    mLoginButton.init(this, mFacebook);



    mPostButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mFacebook.dialog(Example.this, "feed",
                    new SampleDialogListener());
        }
    });
    mPostButton.setVisibility(mFacebook.isSessionValid() ?
            View.VISIBLE :
            View.INVISIBLE);
 } 

当我运行应用程序时,我在以下行得到一个空指针异常:
mfacebook = new facebook(APP_ID);

这是我的 Logcat

06-27 15:01:29.424: ERROR/AndroidRuntime(646): FATAL EXCEPTION: main 06-27 15:01:29.424: ERROR/AndroidRuntime(646): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. sardarjijokes.ncpl/com.sardarjijokes.ncpl.Example}:java.lang.NullPointerException 06-27 15:01:29.424:错误/AndroidRuntime(646):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)06 -27 15:01:29.424: 错误/AndroidRuntime(646): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06-27 15:01:29.424: 错误/AndroidRuntime(646): 在 android.app .ActivityThread.access$2300(ActivityThread.java:125) 06-27 15:01:29.424: 错误/AndroidRuntime(646): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 06-27 15: 01:29.424:错误/AndroidRuntime(646):在 android.os.Handler。dispatchMessage(Handler.java:99) 06-27 15:01:29.424: ERROR/AndroidRuntime(646): at android.os.Looper.loop(Looper.java:123) 06-27 15:01:29.424: ERROR/ AndroidRuntime(646): 在 android.app.ActivityThread.main(ActivityThread.java:4627) 06-27 15:01:29.424: 错误/AndroidRuntime(646): 在 java.lang.reflect.Method.invokeNative(Native Method) 06-27 15:01:29.424: ERROR/AndroidRuntime(646): at java.lang.reflect.Method.invoke(Method.java:521) 06-27 15:01:29.424: ERROR/AndroidRuntime(646): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 06-27 15:01:29.424: ERROR/AndroidRuntime(646): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:626) 06-27 15:01:29.424: ERROR/AndroidRuntime(646): at dalvik.system.NativeStart.main(Native Method) 06-27 15:01:29.424: ERROR/AndroidRuntime(646):引起:java.lang.NullPointerException 06-27 15:01:29.424: ERROR/AndroidRuntime(646): at com.sardarjijokes.ncpl.Example.onCreate(Example.java:85) 06-27 15:01:29.424:错误/AndroidRuntime(646): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 06-27 15:01:29.424: 错误/AndroidRuntime(646): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java :2627)

请提供建议。

4

2 回答 2

0

尝试以下:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 setContentView(R.layout.main); //set Activity layout here
    if (APP_ID == null) {
        Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " +
                "specified before running this example: see Example.java");
    }


    mLoginButton = (LoginButton) findViewById(R.id.login);
    mText = (TextView)findViewById(R.id.txt);

    mPostButton = (Button) findViewById(R.id.postButton);

    **mFacebook = new Facebook(APP_ID);**//this is where i get a Null Pointer Exception
    mAsyncRunner = new AsyncFacebookRunner(mFacebook);
于 2012-06-27T10:20:23.213 回答
0

我不明白你的代码,如果APP_ID为空,你会显示一条警告消息,但让其余的代码得到执行。

不应该是:

if (APP_ID == null) {
    Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " + "specified before running this example: see Example.java");
    return;
}

?

于 2012-06-27T11:13:22.873 回答