English 中文(简体)
MyResume () 抛出一个 NullPointer 例外
原标题:My onResume() throws a NullPointerException

我使用加速计读数来捕捉摇动手机的用户。 当用户摇动手机时, 我想设计它来做一些事情。 它很好, 但我在Pause () 方法上安装了一种加速计, 因为当用户不在应用程序内时, 我不需要加速计来检测任何摇动动作, 因为那样会产生不理想的结果 。 在 Pause () 上, 我从传感器管理器中解开加速计 。

我希望在Resume()中,我可以重新注册感官管理者,并开始我的工作。 显然,这并非完全正确。 我读过文件,而且说实话,我一直无法弄清楚究竟发生了什么事情。

守则:

我宣布全球变异感官Mgr:

private SensorManager sensorMgr;

然后我将它登记在加速计上:

sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);

    // Detect if device has accelerometer
    boolean accelSupported = sensorMgr.registerListener(this,
            SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_GAME);

    if (!accelSupported) {
        // No accelerometer on this device
        sensorMgr.unregisterListener(this,
                SensorManager.SENSOR_ACCELEROMETER);
    }
    sensorMgr.registerListener(this, SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_GAME);

暂停( ) :

protected void onPause() {
    if (sensorMgr != null) {
        sensorMgr.unregisterListener(this,
                SensorManager.SENSOR_ACCELEROMETER);
        sensorMgr = null;
    }
    super.onPause();
}

恢复( ) :

protected void onResume() {
    sensorMgr.registerListener(this, SensorManager.SENSOR_ACCELEROMETER,
        SensorManager.SENSOR_DELAY_GAME);
    super.onResume();
}

错误 :

05-24 14:35:54.058: E/AndroidRuntime(16783): java.lang.RuntimeException: Unable to resume activity : java.lang.NullPointerException

我真的很感激你们能帮上忙 并提前感谢!

最佳回答

sensorMgr 在您到达 onResume () () 时是无效的, 因为您强行设置它在您的 on Pause () 方法中为无效 。 在拨打 sensorMgr ( getSystemService (...) 之前, 重新重新启用 sensorMgr ( getSystemService (...) ) 。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签