无法将java.lang.String类型的对象转换为类型 - Firebase错误

其firebase错误我无法获得它请帮助我解决并告诉我为什么发生它。 对不起,英语,并尝试解决它无法将java.lang.String类型的对象转换为类型 - Firebase错误

E/AndroidRuntime: FATAL EXCEPTION: main 

Process: com.example.kunalgharate.weedon, PID: 16793

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.kunalgharate.weedon.Friends

at com.google.android.gms.internal.zg.zzb(Unknown Source)

at com.google.android.gms.internal.zg.zza(Unknown Source)

at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)

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.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)

at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)

at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)

at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)

at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)

at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)

at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)

at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)

at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1710)

at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:346)

at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)

at android.view.Choreographer.doCallbacks(Choreographer.java:683)

at android.view.Choreographer.doFrame(Choreographer.java:616)

at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)

at android.os.Handler.handleCallback(Handler.java:751)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6123)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

申请终止。

活动主要

import android.content.Context; 

import android.content.Intent;

import android.os.Build;

import android.support.design.widget.TabLayout;

import android.support.v4.view.ViewPager;

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.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;

import com.google.firebase.auth.FirebaseAuth;

import com.google.firebase.database.DatabaseReference;

import com.google.firebase.database.FirebaseDatabase;

import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;

RecyclerView mSubsList;

DatabaseReference mFriendsDatabase;

DatabaseReference mUsersDatabase;

private String mCurrent_user_id;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = findViewById(R.id.main_page_toolbar);

setSupportActionBar(toolbar);

getSupportActionBar().setTitle("WeedON");

mAuth = FirebaseAuth.getInstance();

mCurrent_user_id = mAuth.getCurrentUser().getUid();

mSubsList =findViewById(R.id.subscribes_list);

mSubsList.setHasFixedSize(true);

mSubsList.setLayoutManager(new LinearLayoutManager(this));

mFriendsDatabase = FirebaseDatabase.getInstance().getReference().child("subscribers").child(mCurrent_user_id);

mFriendsDatabase.keepSynced(true);

mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

mUsersDatabase.keepSynced(true);

}

private void signOut() {

mAuth.signOut();

}

@Override

protected void onStart() {

super.onStart();

FirebaseRecyclerAdapter<Friends,MainViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Friends,MainViewHolder>(

Friends.class,

R.layout.single_service_layout,

MainViewHolder.class,

mFriendsDatabase

)

{

@Override

protected void populateViewHolder(MainViewHolder viewHolder, Friends model, int position) {

}

};

mSubsList.setAdapter(firebaseRecyclerAdapter);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

super.onCreateOptionsMenu(menu);

getMenuInflater().inflate(R.menu.main_menu, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

super.onOptionsItemSelected(item);

if(item.getItemId() == R.id.main_logout_btn){

// mUserRef.child("online").setValue(ServerValue.TIMESTAMP);

signOut();

Intent intent = new Intent(this, LoginActivity.class);

startActivity(intent);

finish();

// sendToStart();

}

if(item.getItemId() == R.id.main_settings_btn){

Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);

startActivity(settingsIntent);

}

if(item.getItemId() == R.id.main_all_btn){

Intent settingsIntent = new Intent(MainActivity.this, AllServicesActivity.class);

startActivity(settingsIntent);

}

return true;

}

public static class MainViewHolder extends RecyclerView.ViewHolder{

View mView;

public MainViewHolder(View itemView) {

super(itemView);

mView =itemView;

}

}

}

数据库截图

enter image description here

它不显示布局错误或代码错误我该怎么办?

回答:

要显示这些消息,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); 

DatabaseReference uidRef = rootRef.child("subscribers").child(mCurrent_user_id);

ValueEventListener eventListener = new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

for(DataSnapshot ds : dataSnapshot.getChildren()) {

String message = ds.getValue(String.class);

Log.d("TAG", message);

}

}

@Override

public void onCancelled(DatabaseError databaseError) {}

};

uidRef.addListenerForSingleValueEvent(eventListener);

回答:

通过看你的数据库中,我可以看到你的mFriendsDatabase DatabseReference指出了一些“串”的价值观和你告诉你的firebaseRecyclerAdapter那些是类型为“朋友”,这就是抛出异常的原因。大多数情况下,您可能会在将值保存到数据库时犯了错误,而不是保存Friends对象,而是保存一些字符串值。但如果情况并非如此,则可以更改firebaseRecyclerAdapter到此...

FirebaseRecyclerAdapter<String,MainViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<String,MainViewHolder>(

String.class,

R.layout.single_service_layout,

MainViewHolder.class,

mFriendsDatabase

)

{

@Override

protected void populateViewHolder(MainViewHolder viewHolder, String mString, int position) {

Log.d("TAG",mString);

}

};

mSubsList.setAdapter(firebaseRecyclerAdapter);

以上是 无法将java.lang.String类型的对象转换为类型 - Firebase错误 的全部内容, 来源链接: utcz.com/qa/263362.html

回到顶部