AppCompatImageView 无法转换为 com.squareup.picasso.Target
AppCompatImageView cannot be cast to com.squareup.picasso.Target
我正在尝试将 Firebase 数据库中的图像上传到 post 布局中,但每当我上传图像时,应用程序就会崩溃,并且出现此错误:
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to com.squareup.picasso.Target
at com.example.app.MainActivity.onBindViewHolder(MainActivity.java:164)
at com.example.app.MainActivity.onBindViewHolder(MainActivity.java:159)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
at androidx.recyclerview.widget.RecyclerView.run(RecyclerView.java:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:721)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我不知道这是什么意思,我还没有发现有类似问题的人。这是我的 styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="color">#0F2926</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
最后,这里是我认为发生错误的涉及毕加索的代码:
private void DisplayAllUsersPosts()
{
FirebaseRecyclerOptions<Posts> options =
new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(PostsRef, Posts.class)
.build();
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Posts, PostsViewHolder> (options)
{
@Override
protected void onBindViewHolder(@NonNull PostsViewHolder holder, int position, @NonNull Posts model)
{
holder.Description.setText(model.getDescription());
Picasso.get().load(model.getImage()).into((Target) holder.Image);
}
@NonNull
@Override
public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.all_posts_layout, viewGroup, false);
PostsViewHolder viewHolder = new PostsViewHolder(view);
return viewHolder;
}
};
firebaseRecyclerAdapter.startListening();
postList.setAdapter(firebaseRecyclerAdapter);
}
您收到以下错误:
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to com.squareup.picasso.Target
在以下代码行:
Picasso.get().load(model.getImage()).into((Target) holder.Image);
因为你试图将AppCompatImageView
类型的对象转换为Target
类型的对象,这在Java中实际上是不可能的。要解决这个问题,只需删除不必要的转换:
Picasso.get().load(model.getImage()).into(holder.Image);
我正在尝试将 Firebase 数据库中的图像上传到 post 布局中,但每当我上传图像时,应用程序就会崩溃,并且出现此错误:
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to com.squareup.picasso.Target
at com.example.app.MainActivity.onBindViewHolder(MainActivity.java:164)
at com.example.app.MainActivity.onBindViewHolder(MainActivity.java:159)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
at androidx.recyclerview.widget.RecyclerView.run(RecyclerView.java:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:721)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我不知道这是什么意思,我还没有发现有类似问题的人。这是我的 styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="color">#0F2926</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
最后,这里是我认为发生错误的涉及毕加索的代码:
private void DisplayAllUsersPosts()
{
FirebaseRecyclerOptions<Posts> options =
new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(PostsRef, Posts.class)
.build();
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Posts, PostsViewHolder> (options)
{
@Override
protected void onBindViewHolder(@NonNull PostsViewHolder holder, int position, @NonNull Posts model)
{
holder.Description.setText(model.getDescription());
Picasso.get().load(model.getImage()).into((Target) holder.Image);
}
@NonNull
@Override
public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.all_posts_layout, viewGroup, false);
PostsViewHolder viewHolder = new PostsViewHolder(view);
return viewHolder;
}
};
firebaseRecyclerAdapter.startListening();
postList.setAdapter(firebaseRecyclerAdapter);
}
您收到以下错误:
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to com.squareup.picasso.Target
在以下代码行:
Picasso.get().load(model.getImage()).into((Target) holder.Image);
因为你试图将AppCompatImageView
类型的对象转换为Target
类型的对象,这在Java中实际上是不可能的。要解决这个问题,只需删除不必要的转换:
Picasso.get().load(model.getImage()).into(holder.Image);