无法转换 java.lang.String [Android] 类型的对象
Can't convert object of type java.lang.String [Android]
当我尝试检索应用程序关闭的子路径时,我只想能够在 texview 中设置子路径的值,但应用程序总是关闭并且在 logcat 它告诉我: 无法将 java.lang.String 类型的对象转换为 com.musicapp.owls.ClassesFolder.Recover 类型的对象。
貌似我的问题已经在平台上创建了,但我看到的答案确实对我没有帮助,希望你能帮助我,如果你帮助我,我会把答案标记为已回答
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
recyclerView_album_songs.setLayoutManager(mLayoutManager);
mDatabase = FirebaseDatabase.getInstance().getReference("Artistas/"+artist_name+"/Albumes/"+album_name);
FirebaseRecyclerOptions<Recover> options =
new FirebaseRecyclerOptions.Builder<Recover>()
.setQuery(mDatabase,Recover.class)
.build();
adapter = new FirebaseRecyclerAdapter<Recover, RecyclerView.ViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull Recover model) {
TextView textView = holder.itemView.findViewById(R.id.song_name_txt);
textView_album.setText(model.getName());
textView.setText(model.getPath());
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_songs_inflated, parent, false);
return new RecyclerView.ViewHolder(view) {
};
}
};
adapter.startListening();
recyclerView_album_songs.setAdapter(adapter);
}
}
恢复构造函数
public class Recover {
public String Nombre;
public String ImageView;
public String ImageAlbum;
public String NameAlbum;
public String path;
// Default constructor required for calls to
// DataSnapshot.getValue(User.class)
public Recover() {
}
public Recover(String Nombre, String ImageView, String imageView, String nameAlbum, String path) {
this.Nombre = Nombre;
this.ImageView= ImageView;
this.ImageAlbum = imageView;
this.NameAlbum = nameAlbum;
this.path = path;
}
public String getArtist() {
return Nombre;
}
public String getUrl() {
return ImageView;
}
public String getImage() {
return ImageAlbum;
}
public String getName() {
return NameAlbum;
}
public String getPath() {
return path;
}
}
Logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.musicapp.buhos, PID: 30375
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.musicapp.buhos.ClassesFolder.Recover
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:36)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:109)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
E/AndroidRuntime: at com.android.internal.policy.DecorView.onLayout(DecorView.java:804)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3599)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2070)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8400)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1058)
at android.view.Choreographer.doCallbacks(Choreographer.java:880)
at android.view.Choreographer.doFrame(Choreographer.java:813)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1043)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8037)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
FirebaseUI 中的 FirebaseRecyclerAdapter
假定您要显示数据库中的子节点列表。因此,您传递给 setQuery
的查询需要是查询 select 的一个或多个专辑,或者您要显示的整个专辑列表。
您正在此处加载一张专辑:
mDatabase = FirebaseDatabase.getInstance().getReference("Artistas/"+artist_name+"/Albumes/"+album_name);
因此,当您将其传递给 FirebaseRecyclerAdapter
时,它会尝试在适配器的视图持有者中显示该相册的每个子 属性。这不是你想要的。
如果您只想显示一张专辑,RecyclerView
通常不是最好的小部件。但是,如果您 真的想使用它,则必须创建一个查询以 select 那一张专辑:
Query query = FirebaseDatabase.getInstance()
.getReference("Artistas/"+artist_name+"/Albumes")
.orderByKey()
.equaTo(album_name);
当您对 Firebase 数据库执行查询时,可能会有多个结果。所以快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。
现在您可以将 query
传递给 Firebase:
FirebaseRecyclerOptions<Recover> options =
new FirebaseRecyclerOptions.Builder<Recover>()
.setQuery(query, Recover.class)
.build();
当我尝试检索应用程序关闭的子路径时,我只想能够在 texview 中设置子路径的值,但应用程序总是关闭并且在 logcat 它告诉我: 无法将 java.lang.String 类型的对象转换为 com.musicapp.owls.ClassesFolder.Recover 类型的对象。
貌似我的问题已经在平台上创建了,但我看到的答案确实对我没有帮助,希望你能帮助我,如果你帮助我,我会把答案标记为已回答
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
recyclerView_album_songs.setLayoutManager(mLayoutManager);
mDatabase = FirebaseDatabase.getInstance().getReference("Artistas/"+artist_name+"/Albumes/"+album_name);
FirebaseRecyclerOptions<Recover> options =
new FirebaseRecyclerOptions.Builder<Recover>()
.setQuery(mDatabase,Recover.class)
.build();
adapter = new FirebaseRecyclerAdapter<Recover, RecyclerView.ViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull Recover model) {
TextView textView = holder.itemView.findViewById(R.id.song_name_txt);
textView_album.setText(model.getName());
textView.setText(model.getPath());
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_songs_inflated, parent, false);
return new RecyclerView.ViewHolder(view) {
};
}
};
adapter.startListening();
recyclerView_album_songs.setAdapter(adapter);
}
}
恢复构造函数
public class Recover {
public String Nombre;
public String ImageView;
public String ImageAlbum;
public String NameAlbum;
public String path;
// Default constructor required for calls to
// DataSnapshot.getValue(User.class)
public Recover() {
}
public Recover(String Nombre, String ImageView, String imageView, String nameAlbum, String path) {
this.Nombre = Nombre;
this.ImageView= ImageView;
this.ImageAlbum = imageView;
this.NameAlbum = nameAlbum;
this.path = path;
}
public String getArtist() {
return Nombre;
}
public String getUrl() {
return ImageView;
}
public String getImage() {
return ImageAlbum;
}
public String getName() {
return NameAlbum;
}
public String getPath() {
return path;
}
}
Logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.musicapp.buhos, PID: 30375
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.musicapp.buhos.ClassesFolder.Recover
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:36)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:109)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
E/AndroidRuntime: at com.android.internal.policy.DecorView.onLayout(DecorView.java:804)
at android.view.View.layout(View.java:23033)
at android.view.ViewGroup.layout(ViewGroup.java:6468)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3599)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2070)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8400)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1058)
at android.view.Choreographer.doCallbacks(Choreographer.java:880)
at android.view.Choreographer.doFrame(Choreographer.java:813)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1043)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8037)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
FirebaseUI 中的 FirebaseRecyclerAdapter
假定您要显示数据库中的子节点列表。因此,您传递给 setQuery
的查询需要是查询 select 的一个或多个专辑,或者您要显示的整个专辑列表。
您正在此处加载一张专辑:
mDatabase = FirebaseDatabase.getInstance().getReference("Artistas/"+artist_name+"/Albumes/"+album_name);
因此,当您将其传递给 FirebaseRecyclerAdapter
时,它会尝试在适配器的视图持有者中显示该相册的每个子 属性。这不是你想要的。
如果您只想显示一张专辑,RecyclerView
通常不是最好的小部件。但是,如果您 真的想使用它,则必须创建一个查询以 select 那一张专辑:
Query query = FirebaseDatabase.getInstance()
.getReference("Artistas/"+artist_name+"/Albumes")
.orderByKey()
.equaTo(album_name);
当您对 Firebase 数据库执行查询时,可能会有多个结果。所以快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。
现在您可以将 query
传递给 Firebase:
FirebaseRecyclerOptions<Recover> options =
new FirebaseRecyclerOptions.Builder<Recover>()
.setQuery(query, Recover.class)
.build();