【问题标题】:NullPointerException in onResume caused by MapActivity?MapActivity引起的onResume中的NullPointerException?
【发布时间】:2012-06-27 10:15:18
【问题描述】:

我只在第一次运行我的应用程序时收到此错误。为什么?有什么线索吗? 这是我的代码:

我在方法init() 中获取用户的Facebook 名称和图片我在启用toogle 按钮时启动服务

public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "[ACTIVITY] onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mUserPic = (ImageView) findViewById(R.id.user_pic);
    mText = (TextView) findViewById(R.id.welcome_text);
    mStepValueView = (TextView) findViewById(R.id.step_value);

    // restore facebook session if exist
    SessionStore.restore(facebook, this);

    // Init facebook session
    init();

    // inizialize pedometer
    mStepValue = 0;
    mPaceValue = 0;

    // set map
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.setEnabled(true);
    mapController = mapView.getController(); // get MapController
    mapController.setZoom(MAP_ZOOM); // zoom in the map

    // register listener for toggleButton
    ToggleButton toggleButton = (ToggleButton) findViewById(R.id.ToggleButton);
    toggleButton.setOnCheckedChangeListener(toggleButtonListener);

}

public void onResume() {
    Log.i(TAG, "[ACTIVITY] onResume");
    super.onResume();
    if (!facebook.isSessionValid()) {
        mText.setText("User offline");
        mUserPic.setImageBitmap(null);
    } else {
        facebook.extendAccessTokenIfNeeded(this, null);
    }

    // resume pedometer
    mSettings = PreferenceManager.getDefaultSharedPreferences(this);
    mPedometerSettings = new PedometerSettings(mSettings);

    // Read from preferences if the service was running on the last onPause
    mIsRunning = mPedometerSettings.isServiceRunning();

    if (mIsRunning) {
        bindStepService();
    }

    mPedometerSettings.clearServiceRunning();

}

@Override
protected void onPause() {
    Log.i(TAG, "[ACTIVITY] onPause");
    // if (mIsRunning) {
    // unbindStepService();
    // }
    if (mQuitting) {
        mPedometerSettings.saveServiceRunningWithNullTimestamp(mIsRunning);
    } else {
        mPedometerSettings.saveServiceRunningWithTimestamp(mIsRunning);
    }

    super.onPause();
}

Logcat

06-27 11:48:54.101: E/AndroidRuntime(20313): FATAL EXCEPTION: main
06-27 11:48:54.101: E/AndroidRuntime(20313): java.lang.RuntimeException: Unable to resume activity {it.univpm.dii.socialfoot/it.univpm.dii.socialfoot.SocialFootActivity}: java.lang.NullPointerException
06-27 11:48:54.101: E/AndroidRuntime(20313):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2455)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2483)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1176)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at android.os.Looper.loop(Looper.java:137)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at android.app.ActivityThread.main(ActivityThread.java:4437)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at java.lang.reflect.Method.invokeNative(Native Method)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at java.lang.reflect.Method.invoke(Method.java:511)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at dalvik.system.NativeStart.main(Native Method)
06-27 11:48:54.101: E/AndroidRuntime(20313): Caused by: java.lang.NullPointerException
06-27 11:48:54.101: E/AndroidRuntime(20313):    at com.google.android.maps.MapActivity.onResume(MapActivity.java:451)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at it.univpm.dii.socialfoot.SocialFootActivity.onResume(SocialFootActivity.java:296)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at android.app.Activity.performResume(Activity.java:4545)
06-27 11:48:54.101: E/AndroidRuntime(20313):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2445)
06-27 11:48:54.101: E/AndroidRuntime(20313):    ... 10 more

【问题讨论】:

  • 哪一行导致异常?
  • it.univpm.dii.socialfoot.SocialFootActivity.onResume(SocialFootActivity.java:296) 对应 super.onResume()

标签: android nullpointerexception android-mapview mapactivity


【解决方案1】:

看起来facebook可能在那里无效...... 试试这个

public void onResume() {
    Log.i(TAG, "[ACTIVITY] onResume");
    super.onResume();
   if(null!=facebook){ ///<------------
    if (!facebook.isSessionValid()) {
        mText.setText("User offline");
        mUserPic.setImageBitmap(null);
    } else {
        facebook.extendAccessTokenIfNeeded(this, null);
    }
}

【讨论】:

  • 第296行对应super.onResume()
  • 看起来有一些与 mapview 相关的内部问题,请参阅那里的 onResume 函数 java2s.com/Open-Source/Android/App/mapsforge/org/mapsforge/…
  • 那我该如何解决呢?但是我查看了 onResume 中的 mapview 是否为空,并且不是
  • 不用找原因 Mapview 怎么会为空呢?你从哪里回来活动?应用程序是否长时间处于后台?
  • 我只有在第一次启动应用程序时才会收到此错误
猜你喜欢
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2012-05-23
相关资源
最近更新 更多