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

質問編集履歴

3

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

2019/03/04 14:03

投稿

Mariydi
Mariydi

スコア9

title CHANGED
File without changes
body CHANGED
@@ -102,6 +102,8 @@
102
102
 
103
103
  //以下のメソッドを新しく追加しました。
104
104
  public void CompAnswer(Looper toLooper,final String Answer) {
105
+ setContentView(R.layout.activity_main);
106
+ textView = (TextView) findViewById(R.id.formula);
105
107
  new Handler(toLooper).post(new Runnable() {
106
108
  @Override
107
109
  public void run() {
@@ -181,9 +183,16 @@
181
183
 
182
184
  ### エラー文
183
185
  ```ErorrMessage
184
- Process: com.example.hogehoge.calculater, PID: 21187
185
- java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
186
+ java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
187
+ at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
188
+ at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
189
+ at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
190
+ at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
191
+ at com.example.hogehoge.calculater.MainActivity.CompAnswer(MainActivity.java:79)
192
+ at com.example.hogehoge.calculater.HttpClient.okhttp3Client(HttpClient.java:24)
186
- at com.example.hogehoge.calculater.MainActivity$2.run(MainActivity.java:83)
193
+ at com.example.hogehoge.calculater.MainActivity$1.onClick(MainActivity.java:70)
194
+ at android.view.View.performClick(View.java:6294)
195
+ at android.view.View$PerformClick.run(View.java:24770)
187
196
  at android.os.Handler.handleCallback(Handler.java:790)
188
197
  at android.os.Handler.dispatchMessage(Handler.java:99)
189
198
  at android.os.Looper.loop(Looper.java:164)
@@ -191,4 +200,5 @@
191
200
  at java.lang.reflect.Method.invoke(Native Method)
192
201
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
193
202
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
203
+
194
204
  ```

2

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

2019/03/04 14:03

投稿

Mariydi
Mariydi

スコア9

title CHANGED
File without changes
body CHANGED
@@ -177,4 +177,18 @@
177
177
  }
178
178
  }
179
179
 
180
+ ```
181
+
182
+ ### エラー文
183
+ ```ErorrMessage
184
+ Process: com.example.hogehoge.calculater, PID: 21187
185
+ java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
186
+ at com.example.hogehoge.calculater.MainActivity$2.run(MainActivity.java:83)
187
+ at android.os.Handler.handleCallback(Handler.java:790)
188
+ at android.os.Handler.dispatchMessage(Handler.java:99)
189
+ at android.os.Looper.loop(Looper.java:164)
190
+ at android.app.ActivityThread.main(ActivityThread.java:6494)
191
+ at java.lang.reflect.Method.invoke(Native Method)
192
+ at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
193
+ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
180
194
  ```

1

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

2019/03/04 13:57

投稿

Mariydi
Mariydi

スコア9

title CHANGED
File without changes
body CHANGED
@@ -23,7 +23,7 @@
23
23
  ### メインスレッドのソースコード
24
24
 
25
25
  ```Java
26
- package com.example.sahus.calculater;
26
+ package com.example.hogehoge.calculater;
27
27
 
28
28
  import android.os.Handler;
29
29
  import android.os.HandlerThread;
@@ -40,7 +40,6 @@
40
40
 
41
41
  TextView textView;
42
42
  Button button;
43
-   //たぶんここでHanlder宣言する?
44
43
  Handler handler = new Handler();
45
44
 
46
45
  @Override
@@ -100,15 +99,32 @@
100
99
  }
101
100
  }
102
101
  };
102
+
103
+ //以下のメソッドを新しく追加しました。
104
+ public void CompAnswer(Looper toLooper,final String Answer) {
105
+ new Handler(toLooper).post(new Runnable() {
106
+ @Override
107
+ public void run() {
108
+
109
+ //このテキストビューまでは到達できました。
110
+ //実行しようとするとアプリ自体が落ちることは確認できました。
111
+ textView.setText(Answer);
112
+
113
+ return;
114
+ }
115
+ });
116
+ }
103
117
  }
104
118
  ```
105
119
 
106
120
 
107
- ### okhttp3スレッドのソースコード
121
+ ### HttpClientのソースコード
108
122
 
109
123
  ```Java
110
124
  package com.example.hogehoge.calculater;
111
125
 
126
+ import android.os.Looper;
127
+
112
128
  import java.io.IOException;
113
129
 
114
130
  import okhttp3.Callback;
@@ -120,7 +136,7 @@
120
136
  import okhttp3.Response;
121
137
 
122
138
 
123
- public class HttpClient implements Runnable {
139
+ public class HttpClient {
124
140
 
125
141
  MainActivity test1;
126
142
 
@@ -137,7 +153,7 @@
137
153
  .url(url)
138
154
  .post(RequestBody.create(MediaType.parse("text/plain;charset=utf-8"),strNum))
139
155
  .build();
140
-
156
+
141
157
  Call call = client.newCall(request);
142
158
  call.enqueue(new Callback() {
143
159
  @Override
@@ -149,15 +165,16 @@
149
165
  @Override
150
166
  public void onResponse(Call call, Response response) throws IOException
151
167
  {
152
- //正しい処理描く
168
+ //この1行追加しました。
153
- //ここにvoid run()を書けば良い…?
169
+ new MainActivity().CompAnswer(Looper.getMainLooper(),response.body().string());
154
170
  }
155
171
  });
156
172
 
157
173
 
158
174
  }catch(Exception e) {
159
-       //ここもエラー処理?
175
+
160
176
  }
161
177
  }
162
178
  }
179
+
163
180
  ```