在运行应用程序时,应用程序突然停止,并且在调试应用程序时出现以下错误

时间:2016-10-09 09:27:26

标签: java android android-studio-2.2 android-music-player

我在运行应用程序时停止了应用程序,并且在调试应用程序时出现以下错误。以下是logcat日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.srishti.myapplication, PID: 13312
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.srishti.myapplication/com.example.srishti.myapplication.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=13312, uid=10146 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
                  at android.app.ActivityThread.access$900(ActivityThread.java:153)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5438)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
               Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=13312, uid=10146 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                  at android.os.Parcel.readException(Parcel.java:1620)
                  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                  at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                  at android.content.ContentResolver.query(ContentResolver.java:493)
                  at android.content.ContentResolver.query(ContentResolver.java:435)
                  at com.example.srishti.myapplication.MainActivity.getSongList(MainActivity.java:142)
                  at com.example.srishti.myapplication.MainActivity.onCreate(MainActivity.java:58)
                  at android.app.Activity.performCreate(Activity.java:6303)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:153) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5438) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 
I/Process: Sending signal. PID: 13312 SIG: 9
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'

3 个答案:

答案 0 :(得分:1)

需要权限READ_EXTERNAL_STORAGE

答案 1 :(得分:1)

在清单中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如果您使用的是API 23+,那么也可以在您的活动中添加以下代码,

if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }
}

它将返回结果,

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.v("", "Permission: " + permissions[0] + "was " + grantResults[0]);
        }

    }

答案 2 :(得分:0)

请参阅您的例外日志有解决方案

requires android.permission.READ_EXTERNAL_STORAGE, or there is no runtime-permission model implementation if you are using API 23 or above

您访问存储空间,但清单中没有定义使用权限。

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="..." >

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

// or if you wants to write to then add only below 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

但更可能的情况是,您使用的是API 23(棉花糖或以上),因此为API 23或更高版本添加runtime-permission模型follow the official docs for implementation