質問編集履歴

1

onClick()の記述ミスを発見,レシーバの警告ログの提示

2021/08/22 12:44

投稿

Jhon_McClane
Jhon_McClane

スコア48

test CHANGED
File without changes
test CHANGED
@@ -34,103 +34,219 @@
34
34
 
35
35
 
36
36
 
37
-
37
+ // リスナー解放 レシーバを中断する処理は,onClick()
38
38
 
39
39
  protected void onPause() {
40
40
 
41
+ super.onPause();
42
+
43
+ getMembers = false;
44
+
45
+ Log.d("onPause()", "位置情報と方角取得リスナをリリース");
46
+
47
+ mSensorManager.unregisterListener(this);
48
+
49
+ //stop location updates when Activity is no longer active
50
+
41
- if (receiveTask != null) {
51
+ if (fusedLocationClient != null) {
52
+
42
-
53
+ fusedLocationClient.removeLocationUpdates(mLocationCallback);
54
+
55
+ }
56
+
57
+ }
58
+
59
+
60
+
61
+ @Override
62
+
63
+ public void onClick(View v) {
64
+
65
+ if (R.id.exitGroup == v.getId()) {
66
+
67
+ receiveTask.cancel(false);
68
+
69
+ /***********追記**************************************************/
70
+
71
+ UtilCommon utilCommon = (UtilCommon) UtilCommon.getAppContext();
72
+
73
+ DatagramSocket socket = utilCommon.getDatagramSocket();
74
+
75
+ /*****************************************************************/
76
+
77
+ new Thread(()->{
78
+
79
+ try(DatagramSocket s = new DatagramSocket();) {
80
+
43
-         //receiveTask.requestTermination();
81
+ // 宛先ポートが「s.getLocalPort()」としていた
82
+
83
+ DatagramPacket p = new DatagramPacket(new byte[]{0}, 1, InetAddress.getLocalHost(), socket.getLocalPort());
84
+
85
+ s.send(p);
86
+
87
+ Log.d("onClick()", "ダミーパケット送信");
88
+
89
+ } catch(IOException e) {
90
+
91
+ e.printStackTrace();
92
+
93
+ }
94
+
95
+ }).start();
96
+
97
+ exitAndCloseProcess();
98
+
99
+ finish();
100
+
101
+ } else if (R.id.send == v.getId()) {
102
+
103
+        //...
104
+
105
+ } else if (R.id.mute == v.getId()) {
106
+
107
+ //...
108
+
109
+ }
110
+
111
+ }
112
+
113
+ ```
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+ ```ここに言語を入力
122
+
123
+
124
+
125
+ public class ReceiveTask extends AsyncTask<Void, Void, Void> {
126
+
127
+ private boolean running = true;
128
+
129
+
130
+
131
+   // ...
132
+
133
+
134
+
135
+ @Override
136
+
137
+ protected Void doInBackground(Void... strings) {
138
+
139
+ Log.d("ReceiveTask", "レシーバの処理を開始");
140
+
141
+ DatagramPacket receivePacket = new DatagramPacket(new byte[2048], 2048);
142
+
143
+ UtilCommon utilCommon = (UtilCommon) UtilCommon.getAppContext();
144
+
145
+ DatagramSocket socket = utilCommon.getDatagramSocket();
146
+
147
+ while(running) {
148
+
149
+ if (isCancelled()){
150
+
151
+ Log.d("ReceiveTask", "isCancelled:レシーバの処理を中断します");
152
+
153
+ break;
154
+
155
+ }
44
156
 
45
157
  try {
46
158
 
47
- Thread.sleep((int) (0.5 * 1000));
159
+ socket.receive(receivePacket); //
48
-
160
+
49
- } catch (InterruptedException e) {
161
+ } catch (IOException e) {
162
+
163
+ Log.d("ReceiveTask", "受信エラー:" + e);
164
+
165
+ }
166
+
167
+ String receivedResult = new String(receivePacket.getData(), 0, receivePacket.getLength());
168
+
169
+ try {
170
+
171
+ // ↓のコードで Warning
172
+
173
+ JSONObject jsonObject = new JSONObject(receivedResult);
174
+
175
+ //受け取ったデータを基に処理
176
+
177
+          //...
178
+
179
+ } catch (JSONException e) {
50
180
 
51
181
  e.printStackTrace();
52
182
 
183
+ Log.d("Receiver(シグナリングとPeer)", "エラー:" + e);
184
+
53
185
  }
54
186
 
55
- if (receiveTask.getStatus() != AsyncTask.Status.FINISHED) {
56
-
57
- receiveTask.cancel(true);
58
-
59
- }
60
-
61
- receiveTask = null;
62
-
63
187
  }
64
188
 
189
+ return null;
190
+
65
- }
191
+ }
192
+
193
+
194
+
195
+ @Override
196
+
197
+ protected void onCancelled() {
198
+
199
+ Log.d("ReceiveTask", "onCancelled:レシーバの処理を中断します");
200
+
201
+ running = false;
202
+
203
+ }
204
+
205
+ }
206
+
207
+
66
208
 
67
209
  ```
68
210
 
69
211
 
70
212
 
71
-
213
+ ### 発生している警告
214
+
72
-
215
+ ReceiveTask.java:70がJSONObject jsonObject = new JSONObject(receivedResult); です.
216
+
73
-
217
+ この警告は,無視して問題ないでしょうか。
74
218
 
75
219
  ```ここに言語を入力
76
220
 
77
-
78
-
79
- public class ReceiveTask extends AsyncTask<Void, Void, Void> {
80
-
81
- private boolean running = true;
82
-
83
-
84
-
85
-   // ...
221
+ D/onClick(): ダミーパケット送信
222
+
86
-
223
+ W/System.err: org.json.JSONException: Value �� of type java.lang.String cannot be converted to JSONObject
224
+
87
-
225
+ W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
88
-
226
+
89
- @Override
227
+ at org.json.JSONObject.<init>(JSONObject.java:163)
228
+
90
-
229
+ at org.json.JSONObject.<init>(JSONObject.java:176)
230
+
231
+ at com.example.g_lockonbyaw.signaling.receiver.ReceiveTask.doInBackground(ReceiveTask.java:70)
232
+
233
+ at com.example.g_lockonbyaw.signaling.receiver.ReceiveTask.doInBackground(ReceiveTask.java:27)
234
+
91
- protected Void doInBackground(Void... strings) {
235
+ at android.os.AsyncTask$2.call(AsyncTask.java:333)
92
-
236
+
93
- DatagramPacket receivePacket = new DatagramPacket(new byte[2048], 2048);
237
+ at java.util.concurrent.FutureTask.run(FutureTask.java:266)
238
+
94
-
239
+ W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
240
+
95
- while(running) {
241
+ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
96
-
242
+
97
- if (isCancelled()){
243
+ at java.lang.Thread.run(Thread.java:764)
244
+
98
-
245
+ D/Receiver(シグナリングとPeer): エラー:org.json.JSONException: Value �� of type java.lang.String cannot be converted to JSONObject
246
+
99
- Log.d("ReceiveTask", "isCancelled:レシーバの処理を中断します");
247
+ D/ReceiveTask: isCancelled:レシーバの処理を中断します
100
-
101
- break;
248
+
102
-
103
- }
104
-
105
- try {
106
-
107
- socket.receive(receivePacket); //
108
-
109
- } catch (IOException e) {
110
-
111
- Log.d("ReceiveTask", "受信エラー:" + e);
112
-
113
- }
114
-
115
- }
116
-
117
- return null;
118
-
119
- }
120
-
121
-
122
-
123
- @Override
124
-
125
- protected void onCancelled() {
126
-
127
- Log.d("ReceiveTask", "onCancelled:レシーバの処理を中断します");
249
+ D/ReceiveTask: onCancelled:レシーバの処理を中断します
128
-
129
- running = false;
130
-
131
- }
132
-
133
- }
134
250
 
135
251
 
136
252