質問編集履歴

3

メインクラスの下部にHandlerで呼び出されるメソッドを追記しました。HttpClientのonResponse内にHandlerを使用する一文を追記しました。

2019/03/04 14:03

投稿

Mariydi
Mariydi

スコア9

test CHANGED
File without changes
test CHANGED
@@ -206,6 +206,10 @@
206
206
 
207
207
  public void CompAnswer(Looper toLooper,final String Answer) {
208
208
 
209
+ setContentView(R.layout.activity_main);
210
+
211
+ textView = (TextView) findViewById(R.id.formula);
212
+
209
213
  new Handler(toLooper).post(new Runnable() {
210
214
 
211
215
  @Override
@@ -364,11 +368,25 @@
364
368
 
365
369
  ```ErorrMessage
366
370
 
367
- Process: com.example.hogehoge.calculater, PID: 21187
368
-
369
- java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
371
+ java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
372
+
370
-
373
+ at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
374
+
375
+ at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
376
+
377
+ at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
378
+
379
+ at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
380
+
381
+ at com.example.hogehoge.calculater.MainActivity.CompAnswer(MainActivity.java:79)
382
+
383
+ at com.example.hogehoge.calculater.HttpClient.okhttp3Client(HttpClient.java:24)
384
+
371
- at com.example.hogehoge.calculater.MainActivity$2.run(MainActivity.java:83)
385
+ at com.example.hogehoge.calculater.MainActivity$1.onClick(MainActivity.java:70)
386
+
387
+ at android.view.View.performClick(View.java:6294)
388
+
389
+ at android.view.View$PerformClick.run(View.java:24770)
372
390
 
373
391
  at android.os.Handler.handleCallback(Handler.java:790)
374
392
 
@@ -384,4 +402,6 @@
384
402
 
385
403
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
386
404
 
405
+
406
+
387
407
  ```

2

Handlerで呼び出されるtextView.setTextが実行されたときに表示されるエラー文を追記しました。

2019/03/04 14:03

投稿

Mariydi
Mariydi

スコア9

test CHANGED
File without changes
test CHANGED
@@ -357,3 +357,31 @@
357
357
 
358
358
 
359
359
  ```
360
+
361
+
362
+
363
+ ### エラー文
364
+
365
+ ```ErorrMessage
366
+
367
+ Process: com.example.hogehoge.calculater, PID: 21187
368
+
369
+ java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
370
+
371
+ at com.example.hogehoge.calculater.MainActivity$2.run(MainActivity.java:83)
372
+
373
+ at android.os.Handler.handleCallback(Handler.java:790)
374
+
375
+ at android.os.Handler.dispatchMessage(Handler.java:99)
376
+
377
+ at android.os.Looper.loop(Looper.java:164)
378
+
379
+ at android.app.ActivityThread.main(ActivityThread.java:6494)
380
+
381
+ at java.lang.reflect.Method.invoke(Native Method)
382
+
383
+ at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
384
+
385
+ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
386
+
387
+ ```

1

メインクラスの下部にHandlerで呼び出されるメソッドを追記しました。HttpClientのonResponse内にHandlerを使用する一文を追記しました。

2019/03/04 13:57

投稿

Mariydi
Mariydi

スコア9

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
  ```Java
50
50
 
51
- package com.example.sahus.calculater;
51
+ package com.example.hogehoge.calculater;
52
52
 
53
53
 
54
54
 
@@ -82,8 +82,6 @@
82
82
 
83
83
  Button button;
84
84
 
85
-   //たぶんここでHanlder宣言する?
86
-
87
85
  Handler handler = new Handler();
88
86
 
89
87
 
@@ -202,6 +200,36 @@
202
200
 
203
201
  };
204
202
 
203
+
204
+
205
+ //以下のメソッドを新しく追加しました。
206
+
207
+ public void CompAnswer(Looper toLooper,final String Answer) {
208
+
209
+ new Handler(toLooper).post(new Runnable() {
210
+
211
+ @Override
212
+
213
+ public void run() {
214
+
215
+
216
+
217
+ //このテキストビューまでは到達できました。
218
+
219
+ //実行しようとするとアプリ自体が落ちることは確認できました。
220
+
221
+ textView.setText(Answer);
222
+
223
+
224
+
225
+ return;
226
+
227
+ }
228
+
229
+ });
230
+
231
+ }
232
+
205
233
  }
206
234
 
207
235
  ```
@@ -210,7 +238,7 @@
210
238
 
211
239
 
212
240
 
213
- ### okhttp3スレッドのソースコード
241
+ ### HttpClientのソースコード
214
242
 
215
243
 
216
244
 
@@ -220,6 +248,10 @@
220
248
 
221
249
 
222
250
 
251
+ import android.os.Looper;
252
+
253
+
254
+
223
255
  import java.io.IOException;
224
256
 
225
257
 
@@ -242,7 +274,7 @@
242
274
 
243
275
 
244
276
 
245
- public class HttpClient implements Runnable {
277
+ public class HttpClient {
246
278
 
247
279
 
248
280
 
@@ -276,7 +308,7 @@
276
308
 
277
309
  .build();
278
310
 
279
-
311
+
280
312
 
281
313
  Call call = client.newCall(request);
282
314
 
@@ -300,9 +332,9 @@
300
332
 
301
333
  {
302
334
 
303
- //い処理を描く
335
+ //この1行を追加ました。
304
-
336
+
305
- //ここにvoid run()を書けば良い…?
337
+ new MainActivity().CompAnswer(Looper.getMainLooper(),response.body().string());
306
338
 
307
339
  }
308
340
 
@@ -314,7 +346,7 @@
314
346
 
315
347
  }catch(Exception e) {
316
348
 
317
-       //ここもエラー処理?
349
+
318
350
 
319
351
  }
320
352
 
@@ -322,4 +354,6 @@
322
354
 
323
355
  }
324
356
 
357
+
358
+
325
359
  ```