problem description
using ContentResolver to report an error in service, the error is as follows, how to solve it?
error
FATAL EXCEPTION: main
Process: com.mp.mmedicaplayer, PID: 16990
java.lang.RuntimeException: Unable to instantiate service com.mp.mmedicaplayer.service.MusicService: java.lang.NullPointerException: Attempt to invoke virtual method "android.content.ContentResolver android.content.Context.getContentResolver()" on a null object reference
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3390)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1753)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:159)
at android.app.ActivityThread.main(ActivityThread.java:6385)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1096)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:883)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method "android.content.ContentResolver android.content.Context.getContentResolver()" on a null object reference
at com.mp.mmedicaplayer.service.MusicService.getMusicToArray(MusicService.java:211)
at com.mp.mmedicaplayer.service.MusicService.<init>(MusicService.java:48)
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3387)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1753)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:159)
at android.app.ActivityThread.main(ActivityThread.java:6385)
at java.lang.reflect.Method.invoke(Native Method)
Code
public void getMusicToArray(){
/**
*
*
*/
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
List<MusicListData> mListData = new ArrayList<>();
//Cursor
if(cursor.moveToFirst()){
for(int i = 0; i <cursor.getCount();iPP) {
MusicListData data = new MusicListData();
long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String abum = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
long albulm_id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
int ismusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
Log.d("MMMM",url);
if (ismusic != 0 && duration / (500 * 60) >= 1) {
Log.d("M","2");
musicPath[i] = url;
data.setTitle(title);
data.setUrl(url);
data.setSonger(artist);
mListData.add(data);
}
//
cursor.moveToNext();
}
}
//
cursor.close();
}