在FirebaseRecyclerAdapter中使用Picasso设置CircleImageView时,应用程序崩溃

时间:2019-05-24 21:23:47

标签: java android firebase firebase-realtime-database picasso

我正在尝试显示一个Subject,其中包含来自Firebase的数据。

我不知道为什么会收到此错误:

RecyclerView

那是在我的Process: com.example.lapitchat, PID: 29868 java.lang.IllegalArgumentException: Target must not be null. at com.squareup.picasso.RequestCreator.into(RequestCreator.java:682) at com.squareup.picasso.RequestCreator.into(RequestCreator.java:665) at com.example.lapitchat.UsersActivity$1.onBindViewHolder(UsersActivity.java:65) at com.example.lapitchat.UsersActivity$1.onBindViewHolder(UsersActivity.java:61) at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823) at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641) at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888) at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:407) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949) at android.view.Choreographer.doCallbacks(Choreographer.java:761) at android.view.Choreographer.doFrame(Choreographer.java:693) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 中,我想在那里显示UsersActivity。我也有RecyclerViewViewHolder提供了图片下载网址,但我不知道为什么不能将图片存储在model.getImage()中。

holder.singleImage

这是我的public class UsersActivity extends AppCompatActivity { private Toolbar mToolbar; private RecyclerView mUsersList; private DatabaseReference mUsersDatabase; private FirebaseRecyclerOptions<Users> options; private FirebaseRecyclerAdapter<Users, UsersViewHolder> adapter; private static final String TAG = "UsersActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_users); mToolbar = findViewById(R.id.users_appBar); setSupportActionBar(mToolbar); getSupportActionBar().setTitle("All Users"); getSupportActionBar().setDisplayHomeAsUpEnabled(true); mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users"); mUsersList = findViewById(R.id.users_list); mUsersList.setHasFixedSize(true); mUsersList.setLayoutManager(new LinearLayoutManager(this)); options = new FirebaseRecyclerOptions.Builder<Users>() .setQuery(mUsersDatabase, Users.class).build(); adapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) { @Override protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) { Picasso.get().load(model.getImage()).into(holder.singleImage); holder.singleName.setText(model.getName()); holder.singleStatus.setText(model.getStatus()); } @NonNull @Override public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.users_single_layout, parent, false); return new UsersViewHolder(view); } }; } @Override protected void onStart() { super.onStart(); adapter.startListening(); mUsersList.setAdapter(adapter); } @Override protected void onPause() { super.onPause(); adapter.stopListening(); mUsersList.setAdapter(adapter); } public class UsersViewHolder extends RecyclerView.ViewHolder { TextView singleName; TextView singleStatus; CircleImageView singleImage; UsersViewHolder(@NonNull View itemView) { super(itemView); singleName = findViewById(R.id.user_single_name); singleStatus = findViewById(R.id.user_single_status); singleImage = findViewById(R.id.user_single_image); } } } 代码:

Users.java

其他可能有用的文件:

该活动的布局为package com.example.lapitchat; public class Users { public String image; public String name; public String status; public Users() { //needed } public Users(String name, String image, String status) { this.image = image; this.name = name; this.status = status; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } }

activity_users.xml

以下是列表项布局<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".UsersActivity"> <include android:id="@+id/users_appBar" layout="@layout/app_bar_layout" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <android.support.v7.widget.RecyclerView android:id="@+id/users_list" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/users_appBar"> </android.support.v7.widget.RecyclerView> </android.support.constraint.ConstraintLayout>

users_single_layout.xml

此外,我的数据库外观如下:

1 个答案:

答案 0 :(得分:0)

有些事情对我来说还不是很清楚。但是,我认为这不是您在Firebase数据库中拥有的完整数据。我想您正在吸引大量用户。因此,您需要修改Firebase适配器的初始化,如下所示。

options = new FirebaseRecyclerOptions.Builder<Users>()
    .setQuery(mUsersDatabase, Users[].class).build(); // Use the array

您认为Users类还需要具有另一个我认为也缺少的属性。

public class Users {
    public String image;
    public String name;
    public String status;
    public String thumb_image; // This is missing
}

其他人对我很好。让我知道这是否可以解决您的问题。