简体   繁体   English

无法转换类型为 java.lang.String 的 object [Android]

[英]Can't convert object of type java.lang.String [Android]

在此处输入图像描述

when I try to retrieve the child path the application closes, I just want to be able to set the value of the child path in the texview but the app always closes and in the logcat it tells me: Can't convert object of type java.lang.String to type com.musicapp.owls.ClassesFolder.Recover.当我尝试检索应用程序关闭的子路径时,我只想能够在 texview 中设置子路径的值,但该应用程序始终关闭并且在 logcat 中它告诉我:无法转换 java 类型的 object .lang.String 键入 com.musicapp.owls.ClassesFolder.Recover。

Apparently my question has already been created on the platform but really the answers I see do not help me, I hope you can help me, I will mark the answer as answered if you help me显然我的问题已经在平台上创建了,但实际上我看到的答案对我没有帮助,我希望你能帮助我,如果你帮助我,我会把答案标记为已回答

        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);


    }
}

Recover constructor恢复构造函数

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)

The FirebaseRecyclerAdapter from FirebaseUI assumes that you want to show a list of child nodes from the database. FirebaseRecyclerAdapter中的 FirebaseRecyclerAdapter 假定您要显示数据库中的子节点列表。 So the query you pass to setQuery needs to be either query that selects one or more albums, or the entire list of albums you want to show.因此,您传递给setQuery的查询需要是选择一个或多个专辑的查询,或者您要显示的整个专辑列表。

You're loading a single album here:您正在此处加载一张专辑:

mDatabase = FirebaseDatabase.getInstance().getReference("Artistas/"+artist_name+"/Albumes/"+album_name);

So when you pass that to the FirebaseRecyclerAdapter , it tries to display each child property of that album in a view holder in the adapter.因此,当您将其传递给FirebaseRecyclerAdapter时,它会尝试在适配器的视图持有者中显示该相册的每个子属性。 And that's not what you want.那不是你想要的。

If you only want to show a single album, a RecyclerView is typically not the best widget to use.如果您只想显示一个相册, RecyclerView通常不是最好的小部件。 But if you do really want to use that, you'll have to create a query to select that one album:但是,如果您真的想使用它,则必须创建一个查询 select 那个专辑:

Query query = FirebaseDatabase.getInstance() 
    .getReference("Artistas/"+artist_name+"/Albumes")
    .orderByKey()
    .equaTo(album_name);

When you execute a query against the Firebase Database, there will potentially be multiple results.当您对 Firebase 数据库执行查询时,可能会有多个结果。 So the snapshot contains a list of those results.所以快照包含这些结果的列表。 Even if there is only a single result, the snapshot will contain a list of one result.即使只有一个结果,快照也会包含一个结果的列表。

So now you can pass the query to Firebase:所以现在您可以将query传递给 Firebase:

FirebaseRecyclerOptions<Recover> options =
    new FirebaseRecyclerOptions.Builder<Recover>()
            .setQuery(query, Recover.class)
            .build();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 无法将 java.lang.String 类型的 object 转换为类型 - Firebase Can't convert object of type java.lang.String to type 无法将类型 java.lang.String 的 object 转换为类型 com.example.myapplication.MessageClass - can't convert object of type java.lang.String to type com.example.myapplication.MessageClass 修复错误代码:“无法将类型为 java.lang.String 的 object 转换为类型” - Fixing the error code: "Can't convert object of type java.lang.String to type" DataBaseException:无法将 java.lang.String 类型的 object 转换为 com.example.testingeverything.ShippingDetail 类型,我尝试了一切来修复它 - DataBaseException: Can't convert object of type java.lang.String to type com.example.testingeverything.ShippingDetail, I tried everything to fix it 如何解决此错误:com.google.firebase.database.DatabaseException:无法转换 java.lang.String 类型的 object - how to solve this error: com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String com.google.firebase.database.DatabaseException:无法将类型 java.lang.String 的 object 转换为类型 com.example.chocolate.ModelChatlist - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.chocolate.ModelChatlist com.google.firebase.database.DatabaseException:无法将类型 java.lang.String 的 object 转换为类型 com.example.gatewayrestaurant.model1 - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.gatewayrestaurant.model1 com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.projects.inputData - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.projects.inputData 在 java.lang.String 类型的对象的 JSON 反序列化期间出现问题 - Issue during JSON deserialization of an object of type java.lang.String 无法转换类型为 java.lang.Boolean Android Studio 的 object - Can't convert object of type java.lang.Boolean Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM