1

找不到我得到“空指针异常”的原因。我可以明确检查“getStorageState 是否为空”,但这并不能解释我收到此错误的原因。错误是:

java.lang.NullPointerException
at android.os.Environment.getStorageState(Environment.java:719)
at android.os.Environment.getExternalStorageState(Environment.java:694)
at com.ciqual.android.insight.sessionService.RemoveFiles(SessionService.java:664)
com.vyshas.android.sessionService.onEndSession(SessionService.java:460)

在:(错误指向的行是这个:)(在 jellybean 4.3 和 kitkat 中看到)

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //
        }
4

2 回答 2

1

Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState) 不会导致 NPE,而 Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) 确实会导致 NPE 并使应用程序崩溃。

即使我设置了权限,我仍然不知道为什么 getExternalStorageState 为空,但解决方案至少不会使应用程序崩溃。

于 2014-04-21T08:41:36.247 回答
0

我有来自 Android 4.3 的类似报告。

源代码是:

    /**
     * Gets the current state of the primary "external" storage device.
     *
     * @see #getExternalStorageDirectory()
     */
    public static String getExternalStorageState() {
        try {
            IMountService mountService = IMountService.Stub.asInterface(ServiceManager
                    .getService("mount"));
            final StorageVolume primary = getPrimaryVolume();
            return mountService.getVolumeState(primary.getPath());
        } catch (RemoteException rex) {
            Log.w(TAG, "Failed to read external storage state; assuming REMOVED: " + rex);
            return Environment.MEDIA_REMOVED;
        }
    }

我无事可做,但试着抓住它。

于 2017-06-24T08:44:14.230 回答