无法转换 java.lang.String 类型的对象 [Android]

发布于 2025-01-14 04:27:09 字数 8482 浏览 0 评论 0原文

输入图片此处描述

当我尝试检索应用程序关闭的子路径时,我只想能够在 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)

enter image description here

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.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2025-01-21 04:27:10

FirebaseUI 中的 FirebaseRecyclerAdapter 假设您要显示数据库中的子节点列表。因此,您传递给 setQuery 的查询必须是选择一个或多个专辑的查询,或者是您要显示的整个专辑列表。

您将在此处加载单个相册:

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

因此,当您将其传递给 FirebaseRecyclerAdapter 时,它会尝试在适配器的视图容器中显示该相册的每个子属性。这不是你想要的。

如果您只想显示一个专辑,那么 RecyclerView 通常不是最好的小部件。但如果您确实想要使用它,则必须创建一个查询来选择该专辑:

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

当您对 Firebase 数据库执行查询时,可能会出现多个结果。因此快照包含这些结果的列表。即使只有一个结果,快照也将包含一个结果的列表。

现在您可以将查询传递给Firebase:

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

The FirebaseRecyclerAdapter from FirebaseUI assumes that you want to show a list of child nodes from the database. 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.

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. 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. But if you do really want to use that, you'll have to create a query to select that one album:

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. 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:

FirebaseRecyclerOptions<Recover> options =
    new FirebaseRecyclerOptions.Builder<Recover>()
            .setQuery(query, Recover.class)
            .build();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文