RecycleView使Android 5.0.2应用崩溃

时间:2018-10-18 21:39:45

标签: android recycler-adapter android-room

我有必须显示的UsersDB(带有年龄,体重,姓名)。但是,当我在数据库中添加一些人并打开“设置”布局时,该应用程序将崩溃。

Android 5.0.2

Settings.java:

public class Settings extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        List<UserDB> users = MainActivity.MyDB.myDAO().getUsers();

        initRecycleViewAdapter(users);
    }

    private void initRecycleViewAdapter(List<UserDB> users) {
        RecyclerView rvMain = (RecyclerView) findViewById(R.id.rvMain);
        RecycleViewAdapter adapter = new RecycleViewAdapter(users,this);
        rvMain.setAdapter(adapter);
        rvMain.setLayoutManager(new LinearLayoutManager(this));
    }
}

RecycleViewAdapter.java:

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

    List<UserDB> musers;
    Context mContext;

    public RecycleViewAdapter(List<UserDB> users, Context context) {
        this.musers = users;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_listitem, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.firstName.setText(musers.get(i).getName());
        viewHolder.Age.setText(musers.get(i).getAge());
        viewHolder.Weight.setText(musers.get(i).getWeight());
    }

    @Override
    public int getItemCount() {
        return musers.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        CircleImageView circleImageView;
        TextView firstName;
        TextView Age;
        TextView Weight;
        RelativeLayout parentLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            circleImageView = itemView.findViewById(R.id.image);
            parentLayout = itemView.findViewById(R.id.parent_layout);
            firstName = itemView.findViewById(R.id.first_name);
            Age = itemView.findViewById(R.id.Age);
            Weight = itemView.findViewById(R.id.Weight);
        }
    }
}

activity_settings.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.measurepressure.Settings">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

layout_listitem.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="15dp">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/image"
        android:orientation="vertical">

        <TextView
            android:id="@+id/first_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/Age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/Weight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp" />
    </LinearLayout>
</RelativeLayout>

0 个答案:

没有答案