您好我已将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 =新facebook(APP_ID);
这是我的Logcat
06-27 15:01:29.424:ERROR / AndroidRuntime(646):致命异常:主要 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:ERROR / AndroidRuntime(646):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 06-27 15:01:29.424:ERROR / AndroidRuntime(646):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06-27 15:01:29.424:ERROR / AndroidRuntime(646):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 06-27 15:01:29.424:ERROR / AndroidRuntime(646):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 06-27 15:01:29.424:ERROR / AndroidRuntime(646):在android.os.Handler.dispatchMessage(Handler.java:99) 06-27 15:01:29.424:ERROR / AndroidRuntime(646):在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:ERROR / AndroidRuntime(646):at 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:ERROR / AndroidRuntime(646):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 06-27 15:01:29.424:ERROR / AndroidRuntime(646):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
请提供建议。
答案 0 :(得分: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);
答案 1 :(得分:0)
我不理解你的代码,如果APP_ID
为null,你会显示一条警告信息,但让其余代码执行。
不应该是:
if (APP_ID == null) {
Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " + "specified before running this example: see Example.java");
return;
}