質問編集履歴

1

文字数内で修正をさせていただきました。現状この修正でエラー自体は出なくなりました。追ってコメントさせていただきます。

2024/01/25 09:05

投稿

tetejiro
tetejiro

スコア15

test CHANGED
File without changes
test CHANGED
@@ -2,37 +2,12 @@
2
2
  カレンダーを基にしたアプリを作成しています。
3
3
  現在は、初期表示として1月のカレンダーが表示されており(12月31日・2月1~10日も一緒に表示)
4
4
  1月から2月もしくは12月に移動できる仕様です。
5
- 今回は2月に移動しようとしたところ(MainActivity.java の plus() を呼び出し)でエラーをはきます。
6
-
7
- ### 発生している問題・分からないこと
8
- 最初の表示は、現在上手くいっているようですが(今月のカレンダー表示)
9
- 次の月へ移動するときにエラーをはき、シャットダウンしている状況です。
10
-
11
- ### エラーメッセージ
12
- ```error
13
- FATAL EXCEPTION: main
14
- Process: com.example.RecordTime, PID: 13571
15
- java.lang.IllegalStateException: Could not execute method for android:onClick
16
- at android.view.View$DeclaredOnClickListener.onClick(View.java:6474)
17
- ~~省略~~
18
- Caused by: java.lang.reflect.InvocationTargetException
19
- at java.lang.reflect.Method.invoke(Native Method)
20
- at android.view.View$DeclaredOnClickListener.onClick(View.java:6469)
21
- ~~省略~~
22
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' on a null object reference
23
- ~~省略~~
24
- ```
25
5
 
26
6
  ### 該当のソースコード
27
-
28
7
  ```MonthFragment.java
29
- package com.example.RecordTime;
30
8
  ~~省略~~
31
9
  public class MonthFragment extends Fragment {
32
- TextView year_tx;
10
+ View view;
33
- TextView month_tx;
34
- RecyclerView recyclerView;
35
- FragmentActivity activity;
36
11
  Calendar this_month_instance;
37
12
  int this_first_day_of_week;
38
13
  Integer this_last_day_of_week;
@@ -48,7 +23,6 @@
48
23
  int idx = 0;
49
24
 
50
25
  @Override
51
- //container: fragment が挿入される親
52
26
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
53
27
  Bundle savedInstanceState) {
54
28
  return inflater.inflate(R.layout.month_fragment, container, false);
@@ -57,24 +31,28 @@
57
31
  @Override
58
32
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
59
33
  super.onStart();
60
-
61
- this.activity = getActivity();
34
+ this.view = view;
62
-
35
+ Button post = view.findViewById(R.id.post);
36
+ post.setOnClickListener(new View.OnClickListener() {
37
+ @Override
38
+ public void onClick(View view) {
39
+ this_month++;
40
+ viewCalendar();
41
+ }
42
+ });
63
43
  viewCalendar();
64
44
  }
65
45
 
66
46
  public void viewCalendar() {
67
47
  dataInitialize();
68
48
 
69
- // 〇年を今年にする
70
- year_tx = activity.findViewById(R.id.year);
49
+ TextView yearText = this.view.findViewById(R.id.year);
71
- year_tx.setText(this_year + "年");
50
+ yearText.setText(this_year + "年");
72
- // 〇月を今月にする
51
+
73
- month_tx = activity.findViewById(R.id.month);
52
+ TextView monthText = this.view.findViewById(R.id.month);
74
- month_tx.setText(this_month + 1 + "月");
53
+ monthText.setText(this_month + 1 + "月");
75
-
76
- // RecyclerView を生成して各日にちを生成
54
+
77
- recyclerView = activity.findViewById(R.id.date_recycler_view_container);
55
+ RecyclerView recyclerView = this.view.findViewById(R.id.date_recycler_view_container);
78
56
  recyclerView.setHasFixedSize(true);
79
57
  // ①recycleView に layoutManager をセット
80
58
  recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 7));
@@ -84,9 +62,7 @@
84
62
 
85
63
  // dateList = [DateList, , ...] を作成
86
64
  private void dataInitialize() {
87
-
88
65
  this.dateList = new ArrayList<>(); // push する対象
89
-
90
66
  // 今月1日のインスタンス
91
67
  this.this_month_instance = new GregorianCalendar(this.this_year, this.this_month, 1);
92
68
  // 今月1日の曜日
@@ -107,7 +83,6 @@
107
83
  }
108
84
  this.idx++;
109
85
  }
110
-
111
86
  //★② その月の日付生成
112
87
  while(this.idx <= this.this_maxDate) {
113
88
  // 当日は当日フラグ = true
@@ -119,8 +94,6 @@
119
94
  }
120
95
  this.idx++;
121
96
  }
122
-
123
-
124
97
  //★③ 来月の日付生成
125
98
  this.rest = 7 - this.this_last_day_of_week;
126
99
  this.idx = 1;
@@ -131,36 +104,23 @@
131
104
  this.rest--;
132
105
  }
133
106
  }
134
-
135
- public void plus() {
136
- this.this_month++;
137
- viewCalendar();
138
- }
139
-
140
- public void minus() {
141
- }
142
-
143
107
  public class DateList {
144
108
  private int year; // 年
145
109
  private int month;// 月
146
110
  private int date; // 日にち
147
- private boolean todayFlg = false;
111
+ private boolean todayFlg; // 今日かどうかのフラグ
148
-
149
112
  DateList(int year,int month, int date, boolean todayFlg) {
150
113
  this.year = year;
151
114
  this.month = month;
152
115
  this.date = date;
153
116
  this.todayFlg = todayFlg;
154
117
  }
155
-
156
118
  public int getYear() {
157
119
  return this.year;
158
120
  }
159
-
160
121
  public int getMonth() {
161
122
  return this.month;
162
123
  }
163
-
164
124
  public int getDate() {
165
125
  return this.date;
166
126
  }
@@ -168,115 +128,104 @@
168
128
  }
169
129
  ```
170
130
 
171
- ```month_fragment.xml
172
- <?xml version="1.0" encoding="utf-8"?>
173
- <androidx.constraintlayout.widget.ConstraintLayout
174
- xmlns:android="http://schemas.android.com/apk/res/android"
175
- xmlns:app="http://schemas.android.com/apk/res-auto"
176
- xmlns:tools="http://schemas.android.com/tools"
177
- android:layout_width="match_parent"
178
- android:layout_height="match_parent">
179
-
180
- <TextView
181
- android:id="@+id/year"
182
- android:layout_width="wrap_content"
183
- android:layout_height="wrap_content"
184
- android:text="@string/fuu"
185
- app:layout_constraintBottom_toTopOf="@+id/linerLayout"
186
- app:layout_constraintEnd_toEndOf="parent"
187
- app:layout_constraintStart_toStartOf="parent"
188
- app:layout_constraintTop_toTopOf="parent" />
189
-
190
- <LinearLayout
191
- android:id="@+id/linerLayout"
192
- android:layout_width="match_parent"
193
- android:layout_height="wrap_content"
194
- android:layout_marginTop="32dp"
195
- android:gravity="center"
196
- android:orientation="horizontal"
197
- app:layout_constraintEnd_toEndOf="parent"
198
- app:layout_constraintStart_toStartOf="parent"
199
- app:layout_constraintTop_toTopOf="parent">
200
-
201
- <Button
202
- android:id="@+id/pre"
203
- style="?android:attr/borderlessButtonStyle"
204
- android:layout_width="wrap_content"
205
- android:layout_height="wrap_content"
206
- android:backgroundTint="@color/white"
207
- android:onClick="minus"
208
- android:text="@string/back"
209
- android:textColor="@color/black"
210
- android:textSize="24sp" />
211
-
212
- <TextView
213
- android:id="@+id/month"
214
- android:layout_width="100dp"
215
- android:layout_height="wrap_content"
216
- android:gravity="center"
217
- android:text="@string/month"
218
- android:textSize="24sp" />
219
-
220
- <Button
221
- android:id="@+id/post"
222
- style="?android:attr/borderlessButtonStyle"
223
- android:layout_width="wrap_content"
224
- android:layout_height="wrap_content"
225
- android:backgroundTint="@color/white"
226
- android:onClick="plus"
227
- android:text="@string/post"
228
- android:textColor="@color/black"
229
- android:textSize="24sp" />
230
- </LinearLayout>
231
-
232
- <androidx.recyclerview.widget.RecyclerView
233
- android:id="@+id/date_recycler_view_container"
234
- android:layout_width="match_parent"
235
- android:layout_height="wrap_content"
236
- app:layout_constraintBottom_toBottomOf="parent"
237
- app:layout_constraintTop_toBottomOf="@+id/linerLayout" />
238
-
239
- </androidx.constraintlayout.widget.ConstraintLayout>
131
+ ```Monthly_RecyclerView_Adapter.java
132
+ ~~省略~~
133
+ public class Monthly_RecyclerView_Adapter extends RecyclerView.Adapter<Monthly_RecyclerView_Adapter.ViewHolder>{
134
+
135
+ Context content;
136
+ ArrayList<MonthFragment.DateList> DateList;
137
+
138
+ Monthly_RecyclerView_Adapter(Context content, ArrayList<MonthFragment.DateList> DateList) {
139
+ this.content = content;
140
+ this.DateList = DateList;
141
+ }
142
+
143
+ @Override
144
+ public int getItemCount() {
145
+ return this.DateList.size();
146
+ }
147
+
148
+ @NonNull
149
+ @Override
150
+ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
151
+ // content は MainActivity
152
+ View view = LayoutInflater.from(content).inflate(R.layout.date_viewholder, parent, false);
153
+ return new ViewHolder(view);
154
+ }
155
+
156
+ @Override
157
+ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
158
+
159
+ // 各日付の曜日を取得
160
+ int year = this.DateList.get(position).getYear();
161
+ int month = this.DateList.get(position).getMonth();
162
+ int date = this.DateList.get(position).getDate();
163
+
164
+ Calendar cl = new GregorianCalendar(year, month, date);
165
+ int dayOfWeekNum = cl.get(Calendar.DAY_OF_WEEK);
166
+
167
+ String[] weekDays = {"", "(日)", "(月)", "(火)", "(水)", "(木)", "(金)", "(土)"};
168
+ String dayOfWeek = weekDays[dayOfWeekNum];
169
+
170
+ // 日付 + 曜日を出力
171
+ holder.dateBox.setText(date + dayOfWeek);
172
+
173
+ holder.dateBox.setClickable(true);
174
+ holder.dateBox.setOnClickListener(new View.OnClickListener() {
175
+ @Override
176
+ public void onClick(View view) {
177
+ TextView textView = (TextView)view;
178
+ FragmentActivity fragmentActivity = (FragmentActivity)view.getContext();
179
+ DateFragment dateFragment = new DateFragment();
180
+ DailyCalendarFragment dailyCalendarFragment = new DailyCalendarFragment();
181
+
182
+ // 日付画面フラグメントにパラメータを付与する(〇月・〇日)
183
+ Bundle bundle = new Bundle();
184
+ TextView monthView = fragmentActivity.findViewById(R.id.month);
185
+ bundle.putString("month", monthView.getText().toString());
186
+ bundle.putString("date", textView.getText().toString());
187
+ dateFragment.setArguments(bundle);
188
+
189
+ // 日付画面フラグメントを起動する
190
+ fragmentActivity.getSupportFragmentManager()
191
+ .beginTransaction()
192
+ .replace(R.id.month_fragment_container, dateFragment)
193
+ .addToBackStack("date")
194
+ .commit();
195
+
196
+ fragmentActivity.getSupportFragmentManager()
197
+ .beginTransaction()
198
+ .add(R.id.fragmentContainerView, dailyCalendarFragment)
199
+ .addToBackStack("dailyCalender")
200
+ .commit();
201
+ }
202
+ });
203
+ }
204
+
205
+ public static class ViewHolder extends RecyclerView.ViewHolder {
206
+ TextView dateBox;
207
+
208
+ ViewHolder(@NonNull View itemView) {
209
+ super(itemView);
210
+ dateBox = itemView.findViewById(R.id.every_date);
211
+ }
212
+ }
213
+ }
240
214
  ```
241
215
 
242
216
  ```MainActivity.java
243
217
  package com.example.RecordTime;
244
218
  ~~省略~~
245
219
  public class MainActivity extends FragmentActivity {
246
- MonthFragment monthFragment = new MonthFragment();
247
-
248
220
  @Override
249
221
  protected void onCreate(Bundle savedInstanceState) {
250
222
  super.onCreate(savedInstanceState);
251
223
  setContentView(R.layout.activity_main);
252
224
 
253
- // 月画面フラグメントを表示
254
225
  getSupportFragmentManager().beginTransaction()
255
- .setReorderingAllowed(true)//トランザクションに関与するフラグメントの状態変更を最適化
226
+ .setReorderingAllowed(true)
256
227
  .add(R.id.month_fragment_container, new MonthFragment())
257
228
  .commit();
258
229
  }
259
-
260
- public void plus(View view) {
261
- monthFragment.plus();
262
- }
263
-
264
- public void minus(View view) {
265
- monthFragment.minus();
266
- }
267
230
  }
268
231
  ```
269
-
270
- ### 試したこと・調べたこと
271
- - [ ] teratailやGoogle等で検索した
272
- - [x] ソースコードを自分なりに変更した
273
- - [ ] 知人に聞いた
274
- - [ ] その他
275
-
276
- ##### 上記の詳細・結果
277
- dataInitialize()の中身をthis.dateList = new ArrayList<>();以外コメントアウトしてみましたが
278
- エラー内容は変わらずでした。
279
-
280
- ### 補足
281
- 文字数制限のためエラー全文は載せられていません。
282
- 必要であれば載せますので、おっしゃっていただけますでしょうか。