提问者:小点点

如何处理数据库异常?


MainActivity

public class MainActivity extends AppCompatActivity {

    RecyclerView mRecyclerView;
    FirebaseAdapter mAdapter;
    private static final String TAG = "MainActivity";

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

        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        FirebaseRecyclerOptions<DataClass> options = new FirebaseRecyclerOptions.Builder<DataClass>()
                .setQuery(FirebaseDatabase.getInstance().getReference().child("Users"), DataClass.class)
                .build();

        mAdapter = new FirebaseAdapter(options);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
    }
    @Override
    protected void onStart() {
        super.onStart();
        mAdapter.startListening();
    }
    @Override
    protected void onStop() {
        super.onStop();
        mAdapter.stopListening();
    }
}

适配器

public class FirebaseAdapter extends FirebaseRecyclerAdapter<DataClass,FirebaseAdapter.MyviewHolder> {
    private static final String TAG = "FirebaseAdapter";
    public FirebaseAdapter(@NonNull FirebaseRecyclerOptions<DataClass> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull MyviewHolder holder, int position, @NonNull DataClass model) {
        holder.name.setText(model.getName());
     holder.Nickname.setText(model.getNickname());
        Log.d(TAG, "onBindViewHolder: Now Bind method executing");
    }

    @NonNull
    @Override
    public MyviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View v=  layoutInflater.inflate(R.layout.row_represent,parent,false);
        MyviewHolder v1 = new MyviewHolder(v);
        Log.d(TAG, "onCreateViewHolder: Now on create method executing");
        return v1;
    }

    public static class MyviewHolder extends RecyclerView.ViewHolder{
        TextView name;
       TextView Nickname;
        public MyviewHolder(@NonNull View itemView) {
            super(itemView);
            name = (TextView)itemView.findViewById(R.id.names);
            Nickname = (TextView) itemView.findViewById(R.id.nick);
        }
    }
}

public class DataClass {
      String name;
      String Nickname;

    public DataClass(){

    }

    public DataClass(String name, String Nickname) {
        this.name = name;
        this.Nickname= Nickname;
    }

    public String getName() {
        return name;
    }

    public String getNickname() {
        return Nickname;
    }
}

错误:

 2020-11-24 14:19:05.520 7304-7304/com.example.firebasesearchrec D/FirebaseAdapter: onCreateViewHolder: Now on create method executing
    2020-11-24 14:19:05.535 7304-7304/com.example.firebasesearchrec D/AndroidRuntime: Shutting down VM
    2020-11-24 14:19:05.547 7304-7304/com.example.firebasesearchrec E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.firebasesearchrec, PID: 7304
        com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.firebasesearchrec.DataClass
            at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
            at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)

我得到数据库异常无法转换java. lang类型的对象,有人能在这个问题上帮我吗……………………………………………………………………………………………………………………………………………………………


共3个答案

匿名用户

问题是您对db的查询返回String的结果。要么您尝试读取该节点的子数据,要么数据与对象DataClass不匹配

匿名用户

尝试调试fire base响应,因为我怀疑您的数据类型不同,应该是字符串

匿名用户

您正在尝试读取String类型的数据,这实际上是DataClass类型,这是因为您收到此错误。您需要根据您的要求更改代码。

有关更多详细信息,您可以查看此链接。

相关问题