2016-08-11 18 views
0

簡単な説明を使用してImageViewの上に画像を設定します。これは、彼らが話し言葉でも、データベースファイル内の単語であれば、それも話さその言葉のイメージを持っていますが、アプリテキストへのスピーチです。は、ArrayListの文字列と文字列

だから私は、画像を設定するimageResourceを使用しようとしましたが、それはのArrayListとimageResoruce機能の最初の部分の文字列を使用しているとして、それは、失敗しました。これは、アプリケーションを開いたときにクラッシュするため、エラーメッセージの原因となっていると思われます。

Main.java

public class Main extends Activity { 

    private static final int VR_Request = 100; 

    private final String pathname = ".png"; //path name of an image file stored in the drawable folder 

    TextView speechInput; 
    TextView matchOrNot; 

    String[] wordBank;  // 
    ArrayList<String> wordBANK; 

    ImageButton speechBtn; 

    ImageView image; 
    Resources res = getResources(); 
    int resID; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_reverse_pictionary); 

     speechInput = (TextView) findViewById(R.id.english_word); 
     matchOrNot = (TextView) findViewById(R.id.matchOrNot); 
     wordBank = getResources().getStringArray(R.array.Words); 
     speechBtn = (ImageButton) findViewById(R.id.mic_pic_button); 
     wordBANK = new ArrayList<String>(Arrays.asList(wordBank)); 
     image = (ImageView) findViewById(R.id.imageOfword); 

    } 

    public void onMic(View view) { 
      promptSpeechInput(); 

    } 

    public void promptSpeechInput() { 

    } 

    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 

     if(requestCode == VR_Request && resultCode == RESULT_OK) { 

      ArrayList<String> result = intent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

      if(wordBANK.contains(result.get(0).toLowerCase())){ 
       speechInput.setText(result.get(0).toUpperCase()); 
       matchOrNot.setText("MATCH"); 
       resID = res.getIdentifier(result.get(0).toLowerCase()+pathname, "drawable", getPackageName()); 
       image.setImageResource(resID); 
      }else { 
       speechInput.setText(result.get(0)); 
       matchOrNot.setText("NO MATCH"); 
      } 
     } 
     super.onActivityResult(requestCode, resultCode, intent); 

    } 
} 

ランタイムエラーメッセージ:

08-10 21:07:37.678 2344-2344/com.example.speechtotext E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.speechtotext, PID: 2344 
                      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.speechtotext/com.example.speechtotext.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
                       at android.content.ContextWrapper.getResources(ContextWrapper.java:87) 
                       at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81) 
                       at com.example.speechtotext.Main.<init>(Main.java:38) 
                       at java.lang.Class.newInstance(Native Method) 
                       at android.app.Instrumentation.newActivity(Instrumentation.java:1067) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                       at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                       at android.os.Handler.dispatchMessage(Handler.java:102)  
                       at android.os.Looper.loop(Looper.java:148)  
                       at android.app.ActivityThread.main(ActivityThread.java:5417)  
                       at java.lang.reflect.Method.invoke(Native Method)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

任意のアイデア?前もって感謝します!

答えて

1

コード移動:onCreate()方法に

Resources res = getResources(); 

を。

活動が作成する前に、getResources()を使用することはできません。

+0

あなたの答えをありがとう!アプリケーションを実行して正しい単語を取得したときに画像が表示されない、エラーメッセージが表示されない、何が間違っているのか分かりますか? – Brandon

関連する問題