使用arraylist字符串和字符串在imageview上设置图像

时间:2016-08-11 01:14:17

标签: android string arraylist imageview

简要说明:这是一个演讲文字应用程序,如果他们说的单词也是数据库文件中的单词,那么它也会有一个单词的图像。

所以我试图使用imageResource来设置图像,但它失败了,因为它使用了一个ArrayList和一个String作为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 个答案:

答案 0 :(得分:1)

移动代码:

Resources res = getResources();

进入onCreate()方法。

在创建活动之前,您无法使用getResources()