我创建了AllUsersActivity,它必须获取并显示所有注册用户。但是它崩溃并返回MainActivity。此行中可能存在错误:Picasso.get().load(user_image).into(image);
请在需要查看更多代码的地方写上注释。
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
public class AllUsersActivity extends AppCompatActivity {
private Toolbar mToolbar;
private RecyclerView allUsersList;
private DatabaseReference allDatabaseUserReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_users);
mToolbar = (Toolbar) findViewById(R.id.all_users_app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("All Users");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
allUsersList = (RecyclerView) findViewById(R.id.all_users_list);
allUsersList.setHasFixedSize(true);
allUsersList.setLayoutManager(new LinearLayoutManager(this));
allDatabaseUserReference = FirebaseDatabase.getInstance().getReference().child("Users");
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>
(AllUsers.class, R.layout.all_users_display_layout, AllUsersViewHolder.class, allDatabaseUserReference) {
@Override
protected void populateViewHolder(AllUsersViewHolder viewHolder, AllUsers model, int position) {
viewHolder.setUser_name(model.getUser_name());
viewHolder.setUser_status(model.getUser_status());
viewHolder.setUser_image(getApplicationContext(), model.getUser_image());
}
};
allUsersList.setAdapter(firebaseRecyclerAdapter);
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder {
View mView;
public AllUsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setUser_name (String user_name){
TextView name = (TextView) mView.findViewById(R.id.all_users_username);
name.setText(user_name);
}
public void setUser_status (String user_status){
TextView status = (TextView) mView.findViewById(R.id.all_users_status);
status.setText(user_status);
}
public void setUser_image (Context ctx, String user_image){
CircleImageView image = (CircleImageView) mView.findViewById(R.id.all_users_profile_image);
Picasso.get().load(user_image).into(image);
}
}
}
调试器
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chatting, PID: 21652
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
at com.example.chatting.AllUsersActivity$AllUsersViewHolder.setUser_status(AllUsersActivity.java:83)
at com.example.chatting.AllUsersActivity$1.populateViewHolder(AllUsersActivity.java:54)
at com.example.chatting.AllUsersActivity$1.populateViewHolder(AllUsersActivity.java:48)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:196)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1767)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5345)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
Logcat
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
at com.airliner.chatting.AllUsersActivity$AllUsersViewHolder.setUser_status(AllUsersActivity.java:83)
at com.airliner.chatting.AllUsersActivity$1.populateViewHolder(AllUsersActivity.java:54)
at com.airliner.chatting.AllUsersActivity$1.populateViewHolder(AllUsersActivity.java:48)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:196)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1767)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5345)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
答案 0 :(得分:0)
解决了。我将TextView id错误地添加到LinearLayout。