我正在为android开发一个博客应用程序,我正在使用firebase作为后端。该应用程序工作正常,但当我向下滚动到最后一篇文章时,应用程序崩溃了。我一直在努力寻找解决方案,但却找不到任何解决方案。
以下是面临问题的活动
public class UserActivity extends AppCompatActivity {
private RecyclerView mStatusView;
private DatabaseReference mDatabase;
private DatabaseReference mDatabaseUsers;
private DatabaseReference mDatabaseLike;
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
String userID = "";
private Boolean exit = false;
private Boolean mProcessLike = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
mFirebaseAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null) {
Intent loginIntent = new Intent(UserActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}
}
};
mDatabase = FirebaseDatabase.getInstance().getReference().child("Status");
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
mDatabaseUsers.keepSynced(true);
mDatabaseLike.keepSynced(true);
mDatabase.keepSynced(true);
mStatusView = findViewById(R.id.status_list);
mStatusView.setHasFixedSize(true);
mStatusView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
checkUserExists();
mFirebaseAuth.addAuthStateListener(mAuthListener);
// Creating a Firebase recycle Adapter
FirebaseRecyclerAdapter<Status, StatusViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Status, StatusViewHolder>(
Status.class,
R.layout.status_row,
StatusViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(StatusViewHolder viewHolder, Status model, int position) {
final String postKey = getRef(position).getKey();
viewHolder.setTitle(model.getTitle());
viewHolder.setDescription((model.getDescription()));
viewHolder.setImage(getApplicationContext(), model.getImage());
viewHolder.mNumberOfLikesTextView.setVisibility(View.GONE);
viewHolder.setLikeButton(postKey);
viewHolder.mLikeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mProcessLike = true;
mDatabaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessLike) {
if (dataSnapshot.child(postKey).hasChild(mFirebaseAuth.getCurrentUser().getUid())) {
mDatabaseLike.child(postKey).child(mFirebaseAuth.getCurrentUser().getUid()).removeValue();
mProcessLike = false;
Toast.makeText(UserActivity.this, "Like removed", Toast.LENGTH_SHORT).show();
} else {
mDatabaseLike.child(postKey).child(mFirebaseAuth.getCurrentUser().getUid()).setValue("RandomValue");
mProcessLike = false;
Toast.makeText(UserActivity.this, "Liked", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
mStatusView.setAdapter(firebaseRecyclerAdapter);
}
private void checkUserExists() {
try {
userID = mFirebaseAuth.getCurrentUser().getUid();
} catch (NullPointerException e) {
e.printStackTrace();
}
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(userID)) {
Intent registerIntent = new Intent(UserActivity.this,
RegisterActivity.class);
registerIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(registerIntent);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
// Setting up Recycler View
public static class StatusViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageButton mLikeButton;
TextView mNumberOfLikesTextView;
DatabaseReference mDataBaseLike;
FirebaseAuth mAuth;
public StatusViewHolder(View itemView) {
super(itemView);
mView = itemView;
mLikeButton = itemView.findViewById(R.id.like_button);
mNumberOfLikesTextView = itemView.findViewById(R.id.number_of_likes_text_view);
mDataBaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
mAuth = FirebaseAuth.getInstance();
mDataBaseLike.keepSynced(true);
}
public void setLikeButton(final String postKey) {
mDataBaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(postKey).hasChild(mAuth.getCurrentUser().getUid())) {
mLikeButton.setImageResource(R.drawable.ic_thumb_up_blue_24dp);
} else {
mLikeButton.setImageResource(R.drawable.ic_thumb_up_black_24dp);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
// Setting the title
public void setTitle(String title) {
TextView statusTitle = mView.findViewById(R.id.status_title_textView);
statusTitle.setText(title);
}
public void setDescription(String desc) {
TextView statusDescription = mView.findViewById(R.id.status_description_textView);
statusDescription.setText(desc);
}
public void setImage(Context context, String image) {
ImageView statusImage = mView.findViewById(R.id.status_image);
Picasso.with(context).load(image).into(statusImage);
}
}
}
以下是我遇到问题的日志
FATAL EXCEPTION: main
Process: com.example.akash.iametderick, PID: 12384
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type com.example.akash.iametderick.Status
at com.google.android.gms.internal.zzdzr.zzb(Unknown Source:842)
at com.google.android.gms.internal.zzdzr.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:22)
at com.firebase.ui.database.ObservableSnapshotArray.getObject(ObservableSnapshotArray.java:160)
at com.firebase.ui.database.CachingObservableSnapshotArray.getObject(CachingObservableSnapshotArray.java:40)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:180)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:217)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:285)
at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:342)
at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:358)
at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:365)
at android.support.v7.widget.GapWorker.run(GapWorker.java:396)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我一直试图解决这个问题很长时间但却找不到任何解决方案。
这是我的状态节点:
Status {
-L10rFvy6N-t7RiF7FJ6 {
date: ...;
description: ...;
image: ...;
title: ...''
}
date: ...;
description: ...;
image: ...;
title: ...''
}
答案 0 :(得分:4)
问题在于你的数据保存在firebase上。
您的节点--version
是.csproj
,您可能已将其值保存为数据库中该特定节点的Status
。
因此请检查数据库并将其与其他节点进行比较。特别是遇到问题的节点将在你说Object
时最后一次,所以检查你的最后一个节点