试图将 facebook sdk 集成到 android app.but 我的应用程序没有启动.suggest我如何找到导致错误的行以及如何修复此错误。谢谢。
public class MainActivity extends Activity {
String APP_ID = "307128189431829"; // Replace your App ID // here
// Instance of Facebook Class
private Facebook facebook;
Button btnFbLogin = (Button) findViewById(R.id.button1);
Button postToWall = (Button) findViewById(R.id.button2);
@SuppressWarnings({ "deprecation", "unused" })
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
@SuppressWarnings("unused")
private SharedPreferences mPrefs;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postToWall.getHandler().post(new Runnable() {
public void run() {
postToWall.setVisibility(View.INVISIBLE);
}
});
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
btnFbLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginToFacebook();
btnFbLogin.getHandler().post(new Runnable() {
public void run() {
btnFbLogin.setVisibility(View.INVISIBLE);
}
});
postToWall.getHandler().post(new Runnable() {
public void run() {
postToWall.setVisibility(View.VISIBLE);
}
});
}
});
}
尝试调试在 LoginToFacebook 方法中发现了该问题
public void loginToFacebook() {
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);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
@Override
public void onCancel() {
// Function to handle cancel event
}
@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
@Override
public void onError(DialogError error) {
// Function to handle error
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
日志详细信息
08-15 19:13:13.354: E/AndroidRuntime(994): FATAL EXCEPTION: main
08-15 19:13:13.354: E/AndroidRuntime(994): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sri.z4globalsoft/com.sri.z4globalsoft.MainActivity}: java.lang.NullPointerException
08-15 19:13:13.354: E/AndroidRuntime(994): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
08-15 19:13:13.354: E/AndroidRuntime(994): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-15 19:13:13.354: E/AndroidRuntime(994): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-15 19:13:13.354: E/AndroidRuntime(994): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-15 19:13:13.354: E/AndroidRuntime(994): at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 19:13:13.354: E/AndroidRuntime(994): at android.os.Looper.loop(Looper.java:130)
08-15 19:13:13.354: E/AndroidRuntime(994): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-15 19:13:13.354: E/AndroidRuntime(994): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 19:13:13.354: E/AndroidRuntime(994): at java.lang.reflect.Method.invoke(Method.java:507)
08-15 19:13:13.354: E/AndroidRuntime(994): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-15 19:13:13.354: E/AndroidRuntime(994): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-15 19:13:13.354: E/AndroidRuntime(994): at dalvik.system.NativeStart.main(Native Method)
08-15 19:13:13.354: E/AndroidRuntime(994): Caused by: java.lang.NullPointerException
08-15 19:13:13.354: E/AndroidRuntime(994): at android.app.Activity.findViewById(Activity.java:1647)
08-15 19:13:13.354: E/AndroidRuntime(994): at com.sri.z4globalsoft.MainActivity.<init>(MainActivity.java:23)
08-15 19:13:13.354: E/AndroidRuntime(994): at java.lang.Class.newInstanceImpl(Native Method)
08-15 19:13:13.354: E/AndroidRuntime(994): at java.lang.Class.newInstance(Class.java:1409)
08-15 19:13:13.354: E/AndroidRuntime(994): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-15 19:13:13.354: E/AndroidRuntime(994): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
08-15 19:13:13.354: E/AndroidRuntime(994): ... 11 more