看板 AndroidDev 關於我們 聯絡資訊
大致列出比較可能出問題的地方 Manifest: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <service android:name=".MediaPlayService" android:enabled="true"/> MainActivity.java: Intent it = new Intent(MainActivity.this, MediaPlayService.class); startService(it); MediaPlayerService.java: public class MediaPlayService extends Service { private MediaPlayer player; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { super.onDestroy(); player.stop(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Uri uri=Uri.fromFile( new File(Environment.getExternalStorageDirectory().getPath()+"/fate.mp3")); player=MediaPlayer.create(this,uri); player.start(); return super.onStartCommand(intent, flags, startId); } } Error Log: java.lang.RuntimeException: Unable to start service com.example.mrcat.lesson_052.MediaPlayService@a48b8eb with Intent { cmp=com.example.mrcat.lesson_052/.MediaPlayService }: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference 是指找不到嗎?路徑不對? 使用 startActivity(it) 路徑一樣可以直接撥放 (只是會跳去音樂播放器) 這次想在背景直接launch 照著書打也是失敗...(書的sample project 也一樣會crash) 音樂是放在內存的 (無SD card) Android 6.0.1 (Sony) 還請大神解惑 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.100.99 ※ 文章網址: https://www.ptt.cc/bbs/AndroidDev/M.1488018190.A.72F.html
aids61517: READ_EXTERNAL_STORAGE 好像需要要權限 02/25 21:10
gary771016: 是指root嗎? 它也沒有跳出是否同意權限... 02/25 21:23
ssccg: 跳出是要自己做的,你有做動態要權限? 02/25 23:40
ssccg: 去App設定裡面的權限看那個權限有沒有開,沒有的話開起來試 02/25 23:41
gary771016: 大大又救了我一次 需要去APP選項手動開啟權限 02/26 02:23
aids61517: android 6.0以上某些權限需要動態跟使用者要 02/26 15:22
aids61517: https://goo.gl/nRTvU8 02/26 15:23
aids61517: 基本上有出現在裡面的權限都要額外要 02/26 15:23
WiLLSTW: 6.0之後危險權限都被要求要動態確認 03/01 15:14
ray650128: Android 6.0以上存取外部裝置需要權限喔! 05/18 22:57