【问题标题】:Trouble with exception handling and reading text files in Android StudioAndroid Studio 中异常处理和读取文本文件的问题
【发布时间】:2015-05-18 22:55:07
【问题描述】:
public class GameActivity extends ActionBarActivity {
    private int highScore;
    private String displayWord = "";
    private String answerWord;
    private String guessList = "";
    private EditText userGuess;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game2);
        int i =0;

        ImageView mainImage = (ImageView) findViewById(R.id.gallows);
        mainImage.setImageResource(R.drawable.gallow);

        try {
            randWord(readFile());
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println(answerWord);

        while (i < answerWord.length()){
            displayWord += "-";
            i++;
        }
        TextView displayText = (TextView) findViewById(R.id.display_word);
        displayText.setText(displayWord);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_game, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private List readFile() throws Exception {
        List<String> list = new ArrayList<>();
        FileInputStream in = (FileInputStream) getAssets().open("food.txt");
        Scanner scnr = new Scanner(in);

        while (scnr.hasNext()){
            list.add(scnr.next());
        }
        scnr.close();
        return list;
    }
    private void randWord(List list) throws Exception{


        Random rnd = new Random();
        answerWord = (String) list.get(rnd.nextInt(list.size()));

    }
}

我正在尝试在 Android Studio 中制作一个相对简单的hangman 应用程序,但在读取文本文件和处理文件读取的 IOExceptions 时遇到了问题。控制台中出现的错误是:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
        at cisc181.myproject_1.GameActivity.onCreate(GameActivity.java:44)

从那个错误中,我知道 answerWord 永远不会被赋值。那么,我该如何解决这个问题呢?难道是我在文件中读取的方式?

【问题讨论】:

  • 您在日志中看到了什么? (我看到您正在打印 answerWord 的值)

标签: java android file-io android-studio


【解决方案1】:

其中一个问题是,当您不想要一次抛出时,您会得到一个 Exception 抛出,因为您在 try-catch 块之后编写代码需要一个不抛出异常。

那是因为randWordreadFile 正在抛出异常。该方法为answerWord 赋值。只有answerWord有值,才能调用.length()

那么,试试这个……

    try {
        randWord(readFile());

        System.out.println(answerWord);

        while (i < answerWord.length()){
            displayWord += "-";
            i++;
        }
        TextView displayText = (TextView) findViewById(R.id.display_word);
        displayText.setText(displayWord);

    } catch (Exception e) {
        e.printStackTrace();
    }

现在你仍然会得到一个错误......实际上你会明白为什么抛出异常

【讨论】:

  • 修复了 IOException,但我的文件仍然没有被正确读取。我在日志中收到 FileNotFound 错误,但程序很稳定。我以正确的方式读取文件的方式是否适用于 android 应用程序?
  • 好的。我看到你处理文件的方式,我知道这也一定是问题的根源。看看这里...stackoverflow.com/questions/12421814/…
  • 我看到您正在处理资产。文件存储在哪里...路径是什么?
  • C:\\myProj\\app\\src\\main\\assets
  • 那么在 assets 文件夹中有 food.txt?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 2018-08-17
  • 1970-01-01
相关资源
最近更新 更多