0

我有一个 nullpointexeption,这使我的应用程序无法暂停。(也许重要的是要注意我在 SherlockActionBar 片段中工作)。

我认为是因为这行代码:

// SEND THE FILE
String sharePath = Environment.getExternalStorageDirectory().getPath()
            + "/Soundboard/Ringtones/custom_ringtone.ogg";
Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));

这是我的日志:

09-16 19:33:57.030: E/AndroidRuntime(32080): FATAL EXCEPTION: main
09-16 19:33:57.030: E/AndroidRuntime(32080): java.lang.RuntimeException: Unable to pause activity {com.mega.sb/com.mega.sb.MainActivity}: java.lang.NullPointerException
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2879)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2835)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2813)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.app.ActivityThread.access$800(ActivityThread.java:140)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.os.Looper.loop(Looper.java:137)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.app.ActivityThread.main(ActivityThread.java:4898)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at java.lang.reflect.Method.invokeNative(Native Method)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at java.lang.reflect.Method.invoke(Method.java:511)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at dalvik.system.NativeStart.main(Native Method)
09-16 19:33:57.030: E/AndroidRuntime(32080): Caused by: java.lang.NullPointerException
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:126)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.app.Activity.performSaveInstanceState(Activity.java:1181)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1219)
09-16 19:33:57.030: E/AndroidRuntime(32080):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2861)
09-16 19:33:57.030: E/AndroidRuntime(32080):    ... 12 more

提前致谢!

4

1 回答 1

1

检查您可以记录的文件路径以查看是否有任何变量为空并使用getAbsolutePath()而不是getPath()

String sharePath = Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/Soundboard/Ringtones/custom_ringtone.ogg";
File f = new File(sharePath);
if(f!=null && f.exists()) {
Uri uri = Uri.parse(sharePath);
....

}
于 2013-09-16T17:56:30.377 回答