当我尝试从我最近在Android工作室模拟器,nexus 5 api23上找到音乐播放器的教程中运行程序时,我收到错误。 错误是:
05-28 21:01:23.267 2937-2937/com.tunein.bryantyoung.tunein E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tunein.bryantyoung.tunein, PID: 2937
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tunein.bryantyoung.tunein/com.tunein.bryantyoung.tunein.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2937, uid=10066 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2622)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:6066)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2937, uid=10066 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1666)
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:502)
at android.content.ContentResolver.query(ContentResolver.java:444)
at com.tunein.bryantyoung.tunein.MainActivity.getSongList(MainActivity.java:197)
at com.tunein.bryantyoung.tunein.MainActivity.onCreate(MainActivity.java:104)
at android.app.Activity.performCreate(Activity.java:6584)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:6066)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
当我运行导致异常的程序时,不确定到底发生了什么。 main.xml文件是:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:id="@+id/action_shuffle"
android:icon="@drawable/rand"
android:orderInCategory="1"
android:showAsAction="always"
android:title="Shuffle"/>
<item
android:id="@+id/action_end"
android:icon="@drawable/end"
android:orderInCategory="2"
android:showAsAction="always"
android:title="End"/>
</menu>
我认为问题在于android:showAsAction="always"
行,但当我尝试将android
更改为app
并添加xmlns:app="http://schemas.android.com/apk/res-auto">
时,我仍然遇到与主要问题相同的问题
教程是Here 谢谢
答案 0 :(得分:0)
你的问题在这里:
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2937, uid=10066 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
您的应用必须请求访问文件的权限,请尝试将其添加到您的应用清单:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
更多信息:https://developer.android.com/training/permissions/index.html