【问题标题】:Set an image on imageview using arraylist strings and strings使用 arraylist 字符串和字符串在 imageview 上设置图像
【发布时间】:2016-08-11 01:14:17
【问题描述】:

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

所以我尝试使用 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) 

有什么想法吗?提前谢谢!

【问题讨论】:

    标签: android string arraylist imageview


    【解决方案1】:

    移动代码:

    Resources res = getResources();
    

    进入onCreate() 方法。

    您不能在活动创建之前使用getResources()

    【讨论】:

    • 感谢您的回答!当我运行应用程序并得到正确的单词时,图像似乎没有加载,我没有收到任何错误消息,你有没有机会知道可能出了什么问题?
    猜你喜欢
    • 2011-07-12
    • 2014-06-29
    • 1970-01-01
    • 2014-01-14
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多