由于ResourcesNotFound,Android应用程序崩溃了

时间:2016-02-14 01:11:49

标签: android crash android-softkeyboard enter

package com.example.michaelzhou.scraperapp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class ScraperActivity extends AppCompatActivity {

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

    final TextView display = (TextView) findViewById(R.id.txtDisplay);
    //Button enter = (Button)findViewById(onKeyDown());
    final int[] displays = {184576, 38347, 28198, 643892, 38471872,        4783654, 1235433, 2324, 65344232, 43212, 63345543, 4321,
            1554534, 65432, 324154, 7658434, 13543, 64534532, 6543563, 6543342, 7645354, 543322,
            545324, 542232, 54353453, 456456, 345343453, 943242, 56454353, 1231216543, 765463532, 6453489, 43242, 54354, 5483958,
            3485304, 2132151, 42345463, 23213445, 5432908, 9649372,    754938554};
    EditText input = (EditText) findViewById(R.id.txtEdit);
    input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
            || (actionId == EditorInfo.IME_ACTION_DONE)) {
                int rando = (int) (Math.random() * displays.length);
                display.setText(displays[rando]);
                return true;
            }

            return false;
        }
    });




}


}

完成'按下键,文本视图应显示数组显示的随机数。然而,当它被按下时,应用程序崩溃了。终端显示StringResourceNotFound异常错误。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

这是因为您使用int来设置文本而不是String

  

display.setText(显示器[RANDO]);

首先应该将int解析为String,如:

  

display.setText(显示器[RANDO] + “”);

答案 1 :(得分:0)

TextView.setText(int) 将尝试在资源文件中找到值。

将int转换为String