我正在关注音频播放器的this教程。我可以在服务中播放歌曲。但是当我尝试使用gson保存歌曲并将arraylist发送到服务但是当我点击列表时项目应用程序崩溃错误
FATAL EXCEPTION: main
Process: com.example.neelay.level95, PID: 15133
java.lang.RuntimeException: Unable to start service com.example.neelay.level95.MediaPlayerService@79fde5a with Intent { cmp=com.example.neelay.level95/.MediaPlayerService }: java.lang.RuntimeException: Failed to invoke private android.net.Uri() with no args
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3034)
at android.app.ActivityThread.access$2200(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.RuntimeException: Failed to invoke private android.net.Uri() with no args
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:111)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at com.example.neelay.level95.StorageUtil.loadAudio(StorageUtil.java:42)
at com.example.neelay.level95.MediaPlayerService.onStartCommand(MediaPlayerService.java:106)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3017)
at android.app.ActivityThread.access$2200(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.InstantiationException: Can't instantiate abstract class android.net.Uri
at java.lang.reflect.Constructor.newInstance(Native Method)
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:108)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at com.example.neelay.level95.StorageUtil.loadAudio(StorageUtil.java:42)
at com.example.neelay.level95.MediaPlayerService.onStartCommand(MediaPlayerService.java:106)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3017)
at android.app.ActivityThread.access$2200(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
我收到错误的服务错误
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
//Load data from SharedPreferences
StorageUtil storage = new StorageUtil(getApplicationContext());
audioList = storage.loadAudio();
audioIndex = storage.loadAudioIndex();
if (audioIndex != -1 && audioIndex < audioList.size()) {
//index is in a valid range
activeAudio = audioList.get(audioIndex);
} else {
stopSelf();
}
} catch (NullPointerException e) {
stopSelf();
}
//Request audio focus
if (requestAudioFocus() == false) {
//Could not gain focus
stopSelf();
}
if (mediaSessionManager == null) {
try {
initMediaSession();
initMediaPlayer();
} catch (RemoteException e) {
e.printStackTrace();
stopSelf();
}
buildNotification(PlaybackStatus.PLAYING);
}
//Handle Intent action from MediaSession.TransportControls
handleIncomingActions(intent);
return super.onStartCommand(intent, flags, startId);
}
在songutil类中显示错误的错误
public ArrayList<Songfileinfo> loadAudio() {
preferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = preferences.getString("audioArrayList", null);
Type type = new TypeToken<ArrayList<Songfileinfo>>() {
}.getType();
return gson.fromJson(json, type);
}
我只能知道我在第106行的服务类中遇到错误
audioList = storage.loadAudio();
和第41行的songutil课程
return gson.fromJson(json, type);
但我怎么能解决这个问题,任何帮助都会有所帮助。现在当我从songfileinfo中删除一些代码时,我能够播放这首歌是代码
public class Songfileinfo implements Serializable {
private String title,album,artist,file_uri;
// private Uri album_art; //私人长期;
public Songfileinfo(String data, String title, String album, String artist) {
this.file_uri = data;
this.title = title;
this.album = album;
this.artist = artist;
}
public Songfileinfo() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getFile_uri() {
return file_uri;
}
public void setFile_uri(String file_uri) {
this.file_uri = file_uri;
}
}
但是当我添加代码时,它仍然会崩溃
private Uri album_art;
private long duration;
public Uri getAlbum_art() {
return album_art;
}
public void setAlbum_art(Uri album_art) {
this.album_art = album_art;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
任何原因如果它被崩溃我也会在构造函数中添加它。
答案 0 :(得分:1)
基本上Gson使用java反射从json构建数据对象。它需要一个空构造函数。您正在数据对象中使用Uri。所以只需将Uri中的地址保存为字符串。这将解决问题。
答案 1 :(得分:0)
我认为问题出在这里;
String json = preferences.getString("audioArrayList", null);
如果prefs中没有audioArrayList,则默认返回null。
gson.fromJson(json, type);
变得混乱。顺便说一下,我通常会为类型的json String设置我所期望的任何类。使用
的任何具体原因Type type = new TypeToken<ArrayList<Songfileinfo>>()
...