- FragmentでRecycleViewのリストを表示したいのですが、RecyclerView.setAdapter処理の後に、onCreateView処理を抜けるとアプリが落ちてしまいます。
問題個所をコメント化すると、ReciclerViewは表示されませんが、アプリは起動します。
AndroidStudio 4.1.1
API 28
で開発をしています。
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } _parentActivity = getActivity(); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //外部ストレージ パーミッション設定 ActivityCompat.requestPermissions(_parentActivity,new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED); // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_kokyaku_list, container, false); lvKokyaku = view.findViewById(R.id.lvKokyaku); lvKokyaku.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(_parentActivity); lvKokyaku.setLayoutManager(layoutManager); RecyclerView.Adapter mainAdapter = new RecyclerListAdapter(createRowData()); lvKokyaku.setAdapter(mainAdapter); return view; <---------ここを通過後にアプリが落ちる }
private List<ClsRowData> createRowData() { List<ClsRowData> dataSet = new ArrayList<>(); int i = 0; while (i<20){ ClsRowData data = new ClsRowData(); data.kokyakuMei = "テストユーザー1"; data.jyusyo1 = "住所1"; data.jYusyo2 = "住所2"; data.kokyakuCode = "001-001-0001-000"; data.denwa = "0120-22-0023"; dataSet.add(data); i= i+ 1; } return dataSet; }
private static class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.MainViewHolder>{ private List<ClsRowData> rowDataList; RecyclerListAdapter(List<ClsRowData> rowDataList){ this.rowDataList = rowDataList; } static class MainViewHolder extends RecyclerView.ViewHolder { TextView kokyakuCode; TextView kokyakuMei; TextView jyusyo1; TextView jyusyo2; TextView denwa; MainViewHolder(@NonNull View itemView) { super(itemView); kokyakuCode =itemView.findViewById(R.id.tvCode); kokyakuMei = itemView.findViewById(R.id.tvSimei); jyusyo1 = itemView.findViewById(R.id.tvJyusyo1); jyusyo2 = itemView.findViewById(R.id.tvJyusyo2); denwa = itemView.findViewById(R.id.tvDenwa); } } @NonNull @Override public RecyclerListAdapter.MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.low_kokyaku_list,parent,false); return new MainViewHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerListAdapter.MainViewHolder holder, int position) { ClsRowData rowData = this.rowDataList.get(position); holder.kokyakuCode.setText(rowData.kokyakuCode); holder.kokyakuMei.setText(rowData.kokyakuMei); holder.jyusyo1.setText(rowData.jyusyo1); holder.jyusyo2.setText(rowData.jYusyo2); holder.denwa.setText(rowData.denwa); } @Override public int getItemCount() { return rowDataList.size(); } } class ClsRowData { //Long id; String kokyakuCode; String kokyakuMei; String jyusyo1; String jYusyo2; String denwa; }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/tvDenwa" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvCode" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tvSimei" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvCode" /> <TextView android:id="@+id/tvJyusyo1" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvSimei" /> <TextView android:id="@+id/tvJyusyo02" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvJyusyo1" /> <TextView android:id="@+id/textView9" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvJyusyo02" /> </androidx.constraintlayout.widget.ConstraintLayout>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。