質問編集履歴
4
不要な情報の削除
title
CHANGED
File without changes
|
body
CHANGED
@@ -198,7 +198,3 @@
|
|
198
198
|
Registry: external.system.auto.import.disabled=true
|
199
199
|
Non-Bundled Plugins: dev.polek.adbwifi (1.2.4), org.jetbrains.kotlin (211-1.6.10-release-923-AS7442.40)
|
200
200
|
|
201
|
-
実行デバイス
|
202
|
-
AQUOS sense3(SH-02M)
|
203
|
-
Android バージョン: 11
|
204
|
-
|
3
書式の改善
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,9 +1,42 @@
|
|
1
|
+
### 前提・実現したいこと
|
2
|
+
|
1
3
|
Android StudioでJavaを使ってメモ帳アプリを作っています。メモのデータをSQLiteで保存し、それをRecyclerViewを使って表示する仕様です。メモデータを追加したときにRecyclerViewを更新する必要があるのですが、そこでエラーが出てしまいます。
|
2
4
|
|
3
5
|
具体的に言うと、ダイアログの保存ボタンを押したときの処理(.setPositiveButton/onCreateDialog/CreatesNotesDialogFragment.javaの第二引数内)でMainActivity.javaのnotifyAboutrecyclerViewメソッドを呼び出し、そのメソッド中でnotifyItemInsertedを使って更新処理をしています。notifiItemInsertedメソッドを実行しているadapterがnullになっているのがエラーの原因だと思うのですが、MainActivityのonCreate内でadapterインスタンスの生成を行っているのでなぜnullなのか良く分かりません。もしわかる方がいらっしゃれば、ご教授いただければ幸いです。
|
4
6
|
|
5
|
-
|
7
|
+
### エラーメッセージ(コメントに該当箇所を記載しておきます)
|
6
8
|
|
9
|
+
```
|
10
|
+
E/AndroidRuntime: FATAL EXCEPTION: main
|
11
|
+
Process: jp.halo_sf.haloint, PID: 18663
|
12
|
+
java.lang.NullPointerException: Attempt to invoke virtual method 'void jp.halo_sf.haloint.MainActivity$RecyclerViewAdapter.notifyItemInserted(int)' on a null object reference
|
13
|
+
at jp.halo_sf.haloint.MainActivity.notifyAboutRecyclerView(MainActivity.java:61)
|
14
|
+
//public class MainActivity extends AppCompatActivity {
|
15
|
+
public void notifyAboutRecyclerView(){
|
16
|
+
adapter.notifyItemInserted(listSize);
|
17
|
+
}
|
18
|
+
} のところです。
|
19
|
+
at jp.halo_sf.haloint.CreatesNotesDialogFragment.lambda$onCreateDialog$0$jp-halo_sf-haloint-CreatesNotesDialogFragment(CreatesNotesDialogFragment.java:28)
|
20
|
+
//public class CreatesNotesDialogFragment extends DialogFragment {
|
21
|
+
public Dialog onCreateDialog(Bundle savedInstance) {
|
22
|
+
builder.setPositiveButton(R.string.btn_keep, (dialogInterface, i) -> {
|
23
|
+
new MainActivity().notifyAboutRecyclerView();
|
24
|
+
})
|
25
|
+
}
|
26
|
+
} のところです。
|
27
|
+
at jp.halo_sf.haloint.CreatesNotesDialogFragment$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
|
28
|
+
//CreateNotesDialogFragment の import android.app.Dialog; のところです。
|
29
|
+
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:174)
|
30
|
+
at android.os.Handler.dispatchMessage(Handler.java:106)
|
31
|
+
at android.os.Looper.loop(Looper.java:223)
|
32
|
+
at android.app.ActivityThread.main(ActivityThread.java:8240)
|
33
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
34
|
+
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
|
35
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:979)
|
36
|
+
```
|
37
|
+
|
38
|
+
### 該当のソースコード
|
39
|
+
|
7
40
|
```MainActivity.java
|
8
41
|
import android.database.Cursor;
|
9
42
|
import android.os.Bundle;
|
@@ -146,32 +179,26 @@
|
|
146
179
|
}
|
147
180
|
}
|
148
181
|
```
|
149
|
-
|
182
|
+
|
150
|
-
```
|
151
|
-
E/AndroidRuntime: FATAL EXCEPTION: main
|
152
|
-
Process: jp.halo_sf.haloint, PID: 18663
|
153
|
-
java.lang.NullPointerException: Attempt to invoke virtual method 'void jp.halo_sf.haloint.MainActivity$RecyclerViewAdapter.notifyItemInserted(int)' on a null object reference
|
154
|
-
at jp.halo_sf.haloint.MainActivity.notifyAboutRecyclerView(MainActivity.java:61)
|
155
|
-
//public class MainActivity extends AppCompatActivity {
|
156
|
-
public void notifyAboutRecyclerView(){
|
157
|
-
adapter.notifyItemInserted(listSize);
|
158
|
-
}
|
159
|
-
|
183
|
+
### 試したこと
|
160
|
-
|
184
|
+
|
161
|
-
//public class CreatesNotesDialogFragment extends DialogFragment {
|
162
|
-
public Dialog onCreateDialog(Bundle savedInstance) {
|
163
|
-
builder.setPositiveButton(R.string.btn_keep, (dialogInterface, i) -> {
|
164
|
-
new MainActivity().notifyAboutRecyclerView();
|
165
|
-
})
|
166
|
-
}
|
167
|
-
} のところです。
|
168
|
-
at jp.halo_sf.haloint.CreatesNotesDialogFragment$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
|
169
|
-
|
185
|
+
原因が全く分からないため特にありません。インターネットでいろいろ調べてみましたが思うような答えが得られなかったので質問しました。
|
170
|
-
|
186
|
+
|
171
|
-
|
187
|
+
### 補足情報
|
188
|
+
|
189
|
+
使用ツールの環境
|
172
|
-
|
190
|
+
Android Studio Bumblebee | 2021.1.1 Patch 2
|
173
|
-
|
191
|
+
Build #AI-211.7628.21.2111.8193401, built on February 17, 2022
|
192
|
+
ランタイム・バージョン: 11.0.11+9-b60-7590822 amd64
|
193
|
+
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
|
194
|
+
Windows 10 10.0
|
174
|
-
|
195
|
+
GC: G1 Young Generation, G1 Old Generation
|
175
|
-
|
196
|
+
Memory: 1280M
|
197
|
+
Cores: 8
|
176
|
-
|
198
|
+
Registry: external.system.auto.import.disabled=true
|
199
|
+
Non-Bundled Plugins: dev.polek.adbwifi (1.2.4), org.jetbrains.kotlin (211-1.6.10-release-923-AS7442.40)
|
200
|
+
|
177
|
-
|
201
|
+
実行デバイス
|
202
|
+
AQUOS sense3(SH-02M)
|
203
|
+
Android バージョン: 11
|
204
|
+
|
2
ソースコードの可読性を改善
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
-
Android StudioでJavaを使ってメモ帳アプリを作っています。メモのデータをSQLiteで保存し、それをRecyclerViewを使って表示する仕様です。メモデータを追加したときにRecyclerViewを更新する必要があるのですが、そこでエラーが出てしまいます。
|
1
|
+
Android StudioでJavaを使ってメモ帳アプリを作っています。メモのデータをSQLiteで保存し、それをRecyclerViewを使って表示する仕様です。メモデータを追加したときにRecyclerViewを更新する必要があるのですが、そこでエラーが出てしまいます。
|
2
2
|
|
3
|
+
具体的に言うと、ダイアログの保存ボタンを押したときの処理(.setPositiveButton/onCreateDialog/CreatesNotesDialogFragment.javaの第二引数内)でMainActivity.javaのnotifyAboutrecyclerViewメソッドを呼び出し、そのメソッド中でnotifyItemInsertedを使って更新処理をしています。notifiItemInsertedメソッドを実行しているadapterがnullになっているのがエラーの原因だと思うのですが、MainActivityのonCreate内でadapterインスタンスの生成を行っているのでなぜnullなのか良く分かりません。もしわかる方がいらっしゃれば、ご教授いただければ幸いです。
|
4
|
+
|
5
|
+
以下ソースコードとエラー文です。
|
6
|
+
|
3
7
|
```MainActivity.java
|
4
8
|
import android.database.Cursor;
|
5
9
|
import android.os.Bundle;
|
@@ -57,7 +61,7 @@
|
|
57
61
|
super.onDestroy();
|
58
62
|
}
|
59
63
|
|
60
|
-
//エラー
|
64
|
+
//エラー文に記載されている箇所。
|
61
65
|
public void notifyAboutRecyclerView(){
|
62
66
|
adapter.notifyItemInserted(listSize);
|
63
67
|
}
|
@@ -108,6 +112,7 @@
|
|
108
112
|
|
109
113
|
```CreatesNotesDialogFragment.java
|
110
114
|
import android.app.AlertDialog;
|
115
|
+
//エラー文に記載されている箇所。
|
111
116
|
import android.app.Dialog;
|
112
117
|
import android.os.Bundle;
|
113
118
|
import android.view.LayoutInflater;
|
@@ -132,6 +137,7 @@
|
|
132
137
|
EditText etNoteText = customView.findViewById(R.id.etSetNoteText);
|
133
138
|
String noteText = etNoteText.getText().toString();
|
134
139
|
new DatabaseHelper(getContext()).addData(noteTitle, noteText);
|
140
|
+
//エラー文に記載されている箇所。
|
135
141
|
new MainActivity().notifyAboutRecyclerView();
|
136
142
|
})
|
137
143
|
.setNegativeButton(R.string.btn_cxl, (dialogInterface, i) -> {
|
@@ -140,14 +146,27 @@
|
|
140
146
|
}
|
141
147
|
}
|
142
148
|
```
|
143
|
-
エラー文
|
149
|
+
エラー文(コメントに該当箇所を記載しておきます)
|
144
150
|
```
|
145
151
|
E/AndroidRuntime: FATAL EXCEPTION: main
|
146
152
|
Process: jp.halo_sf.haloint, PID: 18663
|
147
153
|
java.lang.NullPointerException: Attempt to invoke virtual method 'void jp.halo_sf.haloint.MainActivity$RecyclerViewAdapter.notifyItemInserted(int)' on a null object reference
|
148
154
|
at jp.halo_sf.haloint.MainActivity.notifyAboutRecyclerView(MainActivity.java:61)
|
155
|
+
//public class MainActivity extends AppCompatActivity {
|
156
|
+
public void notifyAboutRecyclerView(){
|
157
|
+
adapter.notifyItemInserted(listSize);
|
158
|
+
}
|
159
|
+
} のところです。
|
149
160
|
at jp.halo_sf.haloint.CreatesNotesDialogFragment.lambda$onCreateDialog$0$jp-halo_sf-haloint-CreatesNotesDialogFragment(CreatesNotesDialogFragment.java:28)
|
161
|
+
//public class CreatesNotesDialogFragment extends DialogFragment {
|
162
|
+
public Dialog onCreateDialog(Bundle savedInstance) {
|
163
|
+
builder.setPositiveButton(R.string.btn_keep, (dialogInterface, i) -> {
|
164
|
+
new MainActivity().notifyAboutRecyclerView();
|
165
|
+
})
|
166
|
+
}
|
167
|
+
} のところです。
|
150
168
|
at jp.halo_sf.haloint.CreatesNotesDialogFragment$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
|
169
|
+
//CreateNotesDialogFragment の import android.app.Dialog; のところです。
|
151
170
|
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:174)
|
152
171
|
at android.os.Handler.dispatchMessage(Handler.java:106)
|
153
172
|
at android.os.Looper.loop(Looper.java:223)
|
1
記載するソースコードの変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
Android StudioでJavaを使ってメモ帳アプリを作っています。メモのデータをSQLiteで保存し、それをRecyclerViewを使って表示する仕様です。メモデータを追加したときにRecyclerViewを更新する必要があるのですが、そこでエラーが出てしまいます。
|
1
|
+
Android StudioでJavaを使ってメモ帳アプリを作っています。メモのデータをSQLiteで保存し、それをRecyclerViewを使って表示する仕様です。メモデータを追加したときにRecyclerViewを更新する必要があるのですが、そこでエラーが出てしまいます。具体的に言うと、ダイアログの保存ボタンを押したときの処理(.setPositiveButton/onCreateDialog/CreatesNotesDialogFragment.javaの第二引数内)でMainActivity.javaのnotifyAboutrecyclerViewメソッドを呼び出してその中でnotifyItemInsertedを使って更新処理をしています。以下ソースコードとエラー文です。
|
2
2
|
|
3
|
-
`java`
|
4
3
|
```MainActivity.java
|
5
|
-
//import文。
|
6
4
|
import android.database.Cursor;
|
7
5
|
import android.os.Bundle;
|
8
6
|
import android.view.LayoutInflater;
|
@@ -28,7 +26,29 @@
|
|
28
26
|
protected void onCreate(Bundle savedInstanceState) {
|
29
27
|
super.onCreate(savedInstanceState);
|
30
28
|
setContentView(R.layout.activity_main);
|
29
|
+
|
31
|
-
//DatabaseHelperの生成処理
|
30
|
+
//DatabaseHelperの生成処理。
|
31
|
+
_helper = new DatabaseHelper(MainActivity.this);
|
32
|
+
|
33
|
+
//RecyclerViewの設定。
|
34
|
+
RecyclerView rvNote = findViewById(R.id.rvNote);
|
35
|
+
rvNote.setLayoutManager(new LinearLayoutManager(MainActivity.this));
|
36
|
+
ArrayList<String> title = new ArrayList<>();
|
37
|
+
ArrayList<String> text = new ArrayList<>();
|
38
|
+
Cursor cursor = _helper.readData();
|
39
|
+
while (cursor.moveToNext()) {
|
40
|
+
title.add(cursor.getString(1));
|
41
|
+
text.add(cursor.getString(2));
|
42
|
+
}
|
43
|
+
adapter = new RecyclerViewAdapter(title, text);
|
44
|
+
rvNote.setAdapter(adapter);
|
45
|
+
|
46
|
+
//fabAddを押したときの処理。
|
47
|
+
FloatingActionButton fabAdd = findViewById(R.id.fabAdd);
|
48
|
+
fabAdd.setOnClickListener(view -> {
|
49
|
+
CreatesNotesDialogFragment fragment = new CreatesNotesDialogFragment();
|
50
|
+
fragment.show(getSupportFragmentManager(),"CreatesNotesDialogFragment");
|
51
|
+
});
|
32
52
|
}
|
33
53
|
|
34
54
|
@Override
|
@@ -37,7 +57,7 @@
|
|
37
57
|
super.onDestroy();
|
38
58
|
}
|
39
59
|
|
40
|
-
|
60
|
+
//エラーの出るところ。
|
41
61
|
public void notifyAboutRecyclerView(){
|
42
62
|
adapter.notifyItemInserted(listSize);
|
43
63
|
}
|
@@ -83,4 +103,56 @@
|
|
83
103
|
}
|
84
104
|
}
|
85
105
|
}
|
86
|
-
}
|
106
|
+
}
|
107
|
+
```
|
108
|
+
|
109
|
+
```CreatesNotesDialogFragment.java
|
110
|
+
import android.app.AlertDialog;
|
111
|
+
import android.app.Dialog;
|
112
|
+
import android.os.Bundle;
|
113
|
+
import android.view.LayoutInflater;
|
114
|
+
import android.view.View;
|
115
|
+
import android.widget.EditText;
|
116
|
+
|
117
|
+
import androidx.annotation.NonNull;
|
118
|
+
import androidx.fragment.app.DialogFragment;
|
119
|
+
|
120
|
+
public class CreatesNotesDialogFragment extends DialogFragment {
|
121
|
+
|
122
|
+
@NonNull
|
123
|
+
@Override
|
124
|
+
public Dialog onCreateDialog(Bundle savedInstance) {
|
125
|
+
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
126
|
+
LayoutInflater inflater = requireActivity().getLayoutInflater();
|
127
|
+
View customView = inflater.inflate(R.layout.dialog_layout, null);
|
128
|
+
builder.setView(customView)
|
129
|
+
.setPositiveButton(R.string.btn_keep, (dialogInterface, i) -> {
|
130
|
+
EditText etNoteTitle = customView.findViewById(R.id.etSetNoteTitle);
|
131
|
+
String noteTitle = etNoteTitle.getText().toString();
|
132
|
+
EditText etNoteText = customView.findViewById(R.id.etSetNoteText);
|
133
|
+
String noteText = etNoteText.getText().toString();
|
134
|
+
new DatabaseHelper(getContext()).addData(noteTitle, noteText);
|
135
|
+
new MainActivity().notifyAboutRecyclerView();
|
136
|
+
})
|
137
|
+
.setNegativeButton(R.string.btn_cxl, (dialogInterface, i) -> {
|
138
|
+
});
|
139
|
+
return builder.create();
|
140
|
+
}
|
141
|
+
}
|
142
|
+
```
|
143
|
+
エラー文
|
144
|
+
```
|
145
|
+
E/AndroidRuntime: FATAL EXCEPTION: main
|
146
|
+
Process: jp.halo_sf.haloint, PID: 18663
|
147
|
+
java.lang.NullPointerException: Attempt to invoke virtual method 'void jp.halo_sf.haloint.MainActivity$RecyclerViewAdapter.notifyItemInserted(int)' on a null object reference
|
148
|
+
at jp.halo_sf.haloint.MainActivity.notifyAboutRecyclerView(MainActivity.java:61)
|
149
|
+
at jp.halo_sf.haloint.CreatesNotesDialogFragment.lambda$onCreateDialog$0$jp-halo_sf-haloint-CreatesNotesDialogFragment(CreatesNotesDialogFragment.java:28)
|
150
|
+
at jp.halo_sf.haloint.CreatesNotesDialogFragment$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
|
151
|
+
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:174)
|
152
|
+
at android.os.Handler.dispatchMessage(Handler.java:106)
|
153
|
+
at android.os.Looper.loop(Looper.java:223)
|
154
|
+
at android.app.ActivityThread.main(ActivityThread.java:8240)
|
155
|
+
at java.lang.reflect.Method.invoke(Native Method)
|
156
|
+
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
|
157
|
+
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:979)
|
158
|
+
```
|