应用程序因NullPointerException而崩溃

时间:2013-08-15 13:52:35

标签: android facebook nullpointerexception

尝试将facebook sdk集成到Android app.but我的应用程序没有启动。不幸的是应用程序停止错误我显示。从日志我明白这是由于空指针异常。但没有找到确切的问题。我在哪里放置logs.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

2 个答案:

答案 0 :(得分: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分配一个值,在实例级别声明它们,这在setContentView()被调用之前将导致它们为空指针。

答案 1 :(得分: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对象时也可能会发生这种情况,因为这是您不会检查空的唯一其他地方。此外,您发布的日志是否与loginToFacebook()中出现的错误相关?

希望这有帮助!