質問編集履歴

4

LableDetectionTaskクラスの追記

2019/06/21 04:14

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -111,3 +111,79 @@
111
111
 
112
112
 
113
113
  やりたいことは、とにかく、グローバル変数として、クラス内の別の箇所でも利用できるようにしたいです。
114
+
115
+
116
+
117
+ 追記:onPostExecuteメソッドが入ってるLableDetectionTask extends AsyncTask<Object, Void, String>クラスを載せます
118
+
119
+ ```java
120
+
121
+ private static class LableDetectionTask extends AsyncTask<Object, Void, String> {
122
+
123
+ private final WeakReference<MainActivity> mActivityWeakReference;
124
+
125
+ private Vision.Images.Annotate mRequest;
126
+
127
+
128
+
129
+ LableDetectionTask(MainActivity activity, Vision.Images.Annotate annotate) {
130
+
131
+ mActivityWeakReference = new WeakReference<>(activity);
132
+
133
+ mRequest = annotate;
134
+
135
+ }
136
+
137
+
138
+
139
+ @Override
140
+
141
+ protected String doInBackground(Object... params) {
142
+
143
+ try {
144
+
145
+ Log.d(TAG, "created Cloud Vision request object, sending request");
146
+
147
+ BatchAnnotateImagesResponse response = mRequest.execute();
148
+
149
+ return convertResponseToString(response);
150
+
151
+
152
+
153
+ } catch (GoogleJsonResponseException e) {
154
+
155
+ Log.d(TAG, "failed to make API request because " + e.getContent());
156
+
157
+ } catch (IOException e) {
158
+
159
+ Log.d(TAG, "failed to make API request because of other IOException " + e.getMessage());
160
+
161
+ }
162
+
163
+ return "Cloud Vision API request failed. Check logs for details.";
164
+
165
+ }
166
+
167
+
168
+
169
+
170
+
171
+ protected void onPostExecute(String result) {
172
+
173
+ MainActivity activity = mActivityWeakReference.get();
174
+
175
+ if (activity != null && !activity.isFinishing()) {
176
+
177
+ imageDetail = activity.findViewById(R.id.image_details);
178
+
179
+ imageDetail.setText(result);
180
+
181
+
182
+
183
+ }
184
+
185
+ }
186
+
187
+ }
188
+
189
+ ```

3

グローバル変数の宣言の際の問題点だけは解決

2019/06/21 04:14

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -94,7 +94,13 @@
94
94
 
95
95
 
96
96
 
97
- また、2つ目のソースコードで、グローバル変数として宣言した際に、"Variable 'imageDetail' is already defined in the scope"(変数 'imageDetail'はすでにスコープ内で定義されています)と表示され、文字の色が灰色になってしまっています..
97
+ ~~また、2つ目のソースコードで、グローバル変数として宣言した際に、"Variable 'imageDetail' is already defined in the scope"(変数 'imageDetail'はすでにスコープ内で定義されています)と表示され、文字の色が灰色になってしまっています..
98
+
99
+ ~~
100
+
101
+ ↑単に同じものを2回宣言してしまっていただけなので、これは解決。
102
+
103
+
98
104
 
99
105
 
100
106
 

2

グローバル変数の宣言の際の問題点の追記

2019/06/21 02:57

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -94,6 +94,10 @@
94
94
 
95
95
 
96
96
 
97
+ また、2つ目のソースコードで、グローバル変数として宣言した際に、"Variable 'imageDetail' is already defined in the scope"(変数 'imageDetail'はすでにスコープ内で定義されています)と表示され、文字の色が灰色になってしまっています..
98
+
99
+
100
+
97
101
  色々調べてみましたが、解決できずにいます。
98
102
 
99
103
  これを解決するには、どのような方法があるのでしょうか。

1

質問内容修正

2019/06/21 02:51

投稿

sekaikan_ozaki
sekaikan_ozaki

スコア65

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  グローバル変数を先に宣言しておいて、クラス内の数か所でその変数を使用したいのですが、
18
18
 
19
- Non-static field 'resultText' cannot be referenced from a static context
19
+ Non-static field 'imageDetail' cannot be referenced from a static context
20
20
 
21
21
  という赤文でエラーメッセージが出てしまい、困っています。
22
22
 
@@ -90,7 +90,7 @@
90
90
 
91
91
  ```
92
92
 
93
- しかし、このように変えた結果、"Non-static field 'resultText' cannot be referenced from a static context"(非静的フィールド 'resultText'は静的コンテキストから参照できません)というエラーとなってしまいました。
93
+ しかし、このように変えた結果、"Non-static field 'imageDetail' cannot be referenced from a static context"(非静的フィールド 'imageDetail'は静的コンテキストから参照できません)というエラーとなってしまいました。
94
94
 
95
95
 
96
96