-1

试图将 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
4

2 回答 2

2

尝试:

String APP_ID = "307128189431829"; // Replace your App ID
                                                    // here

// Instance of Facebook Class
private Facebook facebook;
Button btnFbLogin;
Button postToWall;

@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);
    btnFbLogin = (Button) findViewById(R.id.button1);
    postToWall = (Button) findViewById(R.id.button2);
    postToWall.getHandler().post(new Runnable() {
        public void run() {
            postToWall.setVisibility(View.INVISIBLE);
        }
    });
    //Rest is the same

尝试Button在实例级别声明它们的位置分配 sa 值,在setContentView()调用之前将导致它们的空指针。

于 2013-08-15T13:53:46.637 回答
0

我无法摆脱这条线的感觉,

postToWall.getHandler().post(new Runnable() {
    public void run() {
        postToWall.setVisibility(View.INVISIBLE);
    }
});

是你问题的根源。为什么不简单地写(除了已经建议的更改):

postToWall.setVisibility(View.INVISIBLE);

更新:

不幸的是,我目前无法访问 Facebook API 文档。该loginToFacebook()方法中的代码非常简单。如果它在那里抛出一个 NPE,它可能与你设置 mPrefs 的时间有关,mPrefs = getPreferences(MODE_PRIVATE);. 否则,当您尝试获取SharedPreferences.Editor对象时也可能会发生这种情况,因为这是您不检查 null 的唯一其他地方。此外,您发布的日志是否与发生的错误相关loginToFacebook()

希望这可以帮助!

于 2013-08-15T14:22:04.403 回答