PCスペック
Windows10 HOME 64bit
メモリ 32GB
プロセッサ Intel Core i7-8700CPU 3.20GHz
Android Studio 3.4.1(最新ver)
デモ用端末(XPERIA OS:7.0 API 24)
グローバル変数を先に宣言しておいて、クラス内の数か所でその変数を使用したいのですが、
Non-static field 'imageDetail' cannot be referenced from a static context
という赤文でエラーメッセージが出てしまい、困っています。
具体的には、GoogleのCloud Vision API(https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/android/)のコードの中で起こった出来事です。
java
1protected void onPostExecute(String result) { 2 MainActivity activity = mActivityWeakReference.get(); 3 if (activity != null && !activity.isFinishing()) { 4 TextView imageDetail = activity.findViewById(R.id.image_details);//imageDetailsを上書きする形になる 5 imageDetail.setText(result);//画面に表示 6 7 } 8 }
この部分のコードで、画像から認識できたすべての「文字」をimageDetail.setText(result);で表示しています。
ここで、imageDetailはonPostExecute内のローカル変数であったため、これをグローバル変数として宣言しなおそうと、下記のように変更しました。
java
1 2public class MainActivity extends Activity { 3 4private TextView imageDetail; 5 6@Override 7 protected void onCreate(Bundle savedInstanceState) { 8 9~~~~~~~~~~~~~~~~~~~~~~~~中略~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10 11protected void onPostExecute(String result) { 12 MainActivity activity = mActivityWeakReference.get(); 13 if (activity != null && !activity.isFinishing()) { 14 imageDetail = activity.findViewById(R.id.image_details);//imageDetailsを上書きする形になる 15 imageDetail.setText(result);//画面に表示 16 17 } 18 }
しかし、このように変えた結果、"Non-static field 'imageDetail' cannot be referenced from a static context"(非静的フィールド 'imageDetail'は静的コンテキストから参照できません)というエラーとなってしまいました。
~~また、2つ目のソースコードで、グローバル変数として宣言した際に、"Variable 'imageDetail' is already defined in the scope"(変数 'imageDetail'はすでにスコープ内で定義されています)と表示され、文字の色が灰色になってしまっています..
~~
↑単に同じものを2回宣言してしまっていただけなので、これは解決。
色々調べてみましたが、解決できずにいます。
これを解決するには、どのような方法があるのでしょうか。
やりたいことは、とにかく、グローバル変数として、クラス内の別の箇所でも利用できるようにしたいです。
追記:onPostExecuteメソッドが入ってるLableDetectionTask extends AsyncTask<Object, Void, String>クラスを載せます
java
1 private static class LableDetectionTask extends AsyncTask<Object, Void, String> { 2 private final WeakReference<MainActivity> mActivityWeakReference; 3 private Vision.Images.Annotate mRequest; 4 5 LableDetectionTask(MainActivity activity, Vision.Images.Annotate annotate) { 6 mActivityWeakReference = new WeakReference<>(activity); 7 mRequest = annotate; 8 } 9 10 @Override 11 protected String doInBackground(Object... params) { 12 try { 13 Log.d(TAG, "created Cloud Vision request object, sending request"); 14 BatchAnnotateImagesResponse response = mRequest.execute(); 15 return convertResponseToString(response); 16 17 } catch (GoogleJsonResponseException e) { 18 Log.d(TAG, "failed to make API request because " + e.getContent()); 19 } catch (IOException e) { 20 Log.d(TAG, "failed to make API request because of other IOException " + e.getMessage()); 21 } 22 return "Cloud Vision API request failed. Check logs for details."; 23 } 24 25 26 protected void onPostExecute(String result) { 27 MainActivity activity = mActivityWeakReference.get(); 28 if (activity != null && !activity.isFinishing()) { 29 imageDetail = activity.findViewById(R.id.image_details); 30 imageDetail.setText(result); 31 32 } 33 } 34 }
回答2件
あなたの回答
tips
プレビュー