teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

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

2021/08/22 12:44

投稿

Jhon_McClane
Jhon_McClane

スコア48

title CHANGED
File without changes
body CHANGED
@@ -16,21 +16,44 @@
16
16
  receiveTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
17
17
  }
18
18
 
19
-
19
+ // リスナー解放 レシーバを中断する処理は,onClick()
20
20
  protected void onPause() {
21
+ super.onPause();
22
+ getMembers = false;
23
+ Log.d("onPause()", "位置情報と方角取得リスナをリリース");
24
+ mSensorManager.unregisterListener(this);
25
+ //stop location updates when Activity is no longer active
21
- if (receiveTask != null) {
26
+ if (fusedLocationClient != null) {
22
-         //receiveTask.requestTermination();
27
+ fusedLocationClient.removeLocationUpdates(mLocationCallback);
23
- try {
24
- Thread.sleep((int) (0.5 * 1000));
25
- } catch (InterruptedException e) {
26
- e.printStackTrace();
27
- }
28
- if (receiveTask.getStatus() != AsyncTask.Status.FINISHED) {
29
- receiveTask.cancel(true);
30
- }
31
- receiveTask = null;
32
28
  }
33
29
  }
30
+
31
+ @Override
32
+ public void onClick(View v) {
33
+ if (R.id.exitGroup == v.getId()) {
34
+ receiveTask.cancel(false);
35
+ /***********追記**************************************************/
36
+ UtilCommon utilCommon = (UtilCommon) UtilCommon.getAppContext();
37
+ DatagramSocket socket = utilCommon.getDatagramSocket();
38
+ /*****************************************************************/
39
+ new Thread(()->{
40
+ try(DatagramSocket s = new DatagramSocket();) {
41
+ // 宛先ポートが「s.getLocalPort()」としていた
42
+ DatagramPacket p = new DatagramPacket(new byte[]{0}, 1, InetAddress.getLocalHost(), socket.getLocalPort());
43
+ s.send(p);
44
+ Log.d("onClick()", "ダミーパケット送信");
45
+ } catch(IOException e) {
46
+ e.printStackTrace();
47
+ }
48
+ }).start();
49
+ exitAndCloseProcess();
50
+ finish();
51
+ } else if (R.id.send == v.getId()) {
52
+        //...
53
+ } else if (R.id.mute == v.getId()) {
54
+ //...
55
+ }
56
+ }
34
57
  ```
35
58
 
36
59
 
@@ -44,7 +67,10 @@
44
67
 
45
68
  @Override
46
69
  protected Void doInBackground(Void... strings) {
70
+ Log.d("ReceiveTask", "レシーバの処理を開始");
47
- DatagramPacket receivePacket = new DatagramPacket(new byte[2048], 2048);
71
+ DatagramPacket receivePacket = new DatagramPacket(new byte[2048], 2048);
72
+ UtilCommon utilCommon = (UtilCommon) UtilCommon.getAppContext();
73
+ DatagramSocket socket = utilCommon.getDatagramSocket();
48
74
  while(running) {
49
75
  if (isCancelled()){
50
76
  Log.d("ReceiveTask", "isCancelled:レシーバの処理を中断します");
@@ -55,6 +81,16 @@
55
81
  } catch (IOException e) {
56
82
  Log.d("ReceiveTask", "受信エラー:" + e);
57
83
  }
84
+ String receivedResult = new String(receivePacket.getData(), 0, receivePacket.getLength());
85
+ try {
86
+ // ↓のコードで Warning
87
+ JSONObject jsonObject = new JSONObject(receivedResult);
88
+ //受け取ったデータを基に処理
89
+          //...
90
+ } catch (JSONException e) {
91
+ e.printStackTrace();
92
+ Log.d("Receiver(シグナリングとPeer)", "エラー:" + e);
93
+ }
58
94
  }
59
95
  return null;
60
96
  }
@@ -66,4 +102,26 @@
66
102
  }
67
103
  }
68
104
 
105
+ ```
106
+
107
+ ### 発生している警告
108
+ ReceiveTask.java:70がJSONObject jsonObject = new JSONObject(receivedResult); です.
109
+ この警告は,無視して問題ないでしょうか。
110
+ ```ここに言語を入力
111
+ D/onClick(): ダミーパケット送信
112
+ W/System.err: org.json.JSONException: Value �� of type java.lang.String cannot be converted to JSONObject
113
+ W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
114
+ at org.json.JSONObject.<init>(JSONObject.java:163)
115
+ at org.json.JSONObject.<init>(JSONObject.java:176)
116
+ at com.example.g_lockonbyaw.signaling.receiver.ReceiveTask.doInBackground(ReceiveTask.java:70)
117
+ at com.example.g_lockonbyaw.signaling.receiver.ReceiveTask.doInBackground(ReceiveTask.java:27)
118
+ at android.os.AsyncTask$2.call(AsyncTask.java:333)
119
+ at java.util.concurrent.FutureTask.run(FutureTask.java:266)
120
+ W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
121
+ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
122
+ at java.lang.Thread.run(Thread.java:764)
123
+ D/Receiver(シグナリングとPeer): エラー:org.json.JSONException: Value �� of type java.lang.String cannot be converted to JSONObject
124
+ D/ReceiveTask: isCancelled:レシーバの処理を中断します
125
+ D/ReceiveTask: onCancelled:レシーバの処理を中断します
126
+
69
127
  ```