简体   繁体   English

致命异常:DatabaseException:无法将 java.lang.String 类型的 object 转换为 com.accentra.retrivedatabase 类型。

[英]FATAL EXCEPTION: DatabaseException: Can't convert object of type java.lang.String to type com.accentra.retrivedatabase.User

I have created a firebase realtime database I am trying to retrieve data using recycler view but I am getting error as below我创建了一个 firebase 实时数据库我正在尝试使用回收器视图检索数据,但出现如下错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.accentra.retrivedatabase, PID: 14196
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.accentra.retrivedatabase.User
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.1.0:418)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.1.0:214)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.1.0:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.1.0:203)
        at com.accentra.retrivedatabase.UserDetailActivity$1.onDataChange(UserDetailActivity.java:77)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.1.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.1.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.1.0:55)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6662)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
I/Process: Sending signal. PID: 14196 SIG: 9
Process 14196 terminated.

我的 Firebase 数据库的屏幕截图

This is my code for Main activity这是我的主要活动代码

public class MainActivity extends AppCompatActivity {

Button b1;

public static final String Firebase_Server_URL = "https://retrive-database-8f64a.firebaseio.com/";

Firebase firebase;

DatabaseReference databaseReference;

// Root Database Name for Firebase Database.
public static final String Database_Path = "User";

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Firebase.setAndroidContext(MainActivity.this);
    firebase = new Firebase(Firebase_Server_URL);

    databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);


    b1=(Button)findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent it=new Intent(MainActivity.this,UserDetailActivity.class);
            startActivity(it);
        }
    });
}
}

This is the code for User detail activity这是用户详细信息活动的代码

public class UserDetailActivity extends AppCompatActivity {

DatabaseReference databaseReference;

ProgressDialog progressDialog;

List<User> list = new ArrayList<>();

RecyclerView recyclerView;
Firebase firebase;
RecyclerView.Adapter adapter ;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_detail);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

    recyclerView.setHasFixedSize(true);

    recyclerView.setLayoutManager(new LinearLayoutManager(UserDetailActivity.this));

    progressDialog = new ProgressDialog(UserDetailActivity.this);

    progressDialog.setMessage("Loading Data from Firebase Database");

    progressDialog.show();

    databaseReference = FirebaseDatabase.getInstance().getReference(MainActivity.Database_Path);

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

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

                User user = dataSnapshot.getValue(User.class);

                list.add(user);
            }

            adapter = new RecyclerViewAdapter(UserDetailActivity.this,list);

            recyclerView.setAdapter(adapter);

            progressDialog.dismiss();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            progressDialog.dismiss();

        }
    });

}
}

This is the code for Recycler View Adapter这是 Recycler View Adapter 的代码

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {


Context context;
List<User> MainImageUploadInfoList;

public RecyclerViewAdapter(Context context, List<User> TempList) {

    this.MainImageUploadInfoList = TempList;

    this.context = context;



}

@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);

    return viewHolder;
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    User user = MainImageUploadInfoList.get(position);

    holder.FirstNameTextView.setText(user.getFirst_name());

    holder.LastNameTextView.setText(user.getLast_name());

}

@Override
public int getItemCount() {

    return MainImageUploadInfoList.size();

}

public class ViewHolder extends RecyclerView.ViewHolder {

   public TextView FirstNameTextView;
   public TextView LastNameTextView;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        FirstNameTextView = (TextView) itemView.findViewById(R.id.textView1);
        LastNameTextView = (TextView) itemView.findViewById(R.id.textView2);


    }
}
}

And this is my User class这是我的用户 class

public class User {

private String first_name;
private String last_name;

public User() {
}

public String getFirst_name() {
    return first_name;
}

public void setFirst_name(String first_name) {
    this.first_name = first_name;
}

public String getLast_name() {
    return last_name;
}

public void setLast_name(String last_name) {
    this.last_name = last_name;
}
}

Please help me to solve this problem请帮我解决这个问题

Can you try:你能试一下吗:

databaseReference = FirebaseDatabase.getInstance().getReference().child(Database_Path);

instead of:代替:

databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.google.firebase.database.DatabaseException:无法将类型为java.lang.String的对象转换为类型 - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type DatabaseException:无法将java.Lang.String对象转换为类型 - DatabaseException: can't convert object of java.Lang.String to type com.google.firebase.database.DatabaseException:无法将类型为java.lang.String的对象转换为类型3 - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type 3 DatabaseException:无法将类型为java.lang.String的对象转换为类型为java.lang.String的类型 - DatabaseException: Can't convert object of type java.lang.String to type java.lang.String to type com.google.firebase.database.DatabaseException:无法转换java.lang.String类型的对象 - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String 错误-com.google.firebase.database.DatabaseException:无法转换java.lang.String类型的对象 - Error - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String com.google.firebase.database.DatabaseException:无法将类型为java.lang.String的对象转换为Recensione - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to Recensione com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.prj.User - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.prj.User com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type Data class object KOTLIN - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type Data class object KOTLIN com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为 models.firebase.FirebaseTripDataHolder 类型 - com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type models.firebase.FirebaseTripDataHolder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM