从android中的资源文件中读取

时间:2016-05-05 08:48:32

标签: java android assets

我正在使用Hangman游戏来自学Android开发。我已经用Java完成了所有这一切,并且运行良好。我使用的是Android Studio 2.1,显示器不断向我发送大量错误,我无法弄清楚我在看什么!

我努力的部分是从文件" WordsDoc"资产。我得到的代码是:

修改2

到目前为止应用程序的所有代码(真的不多!)

    public class MainActivity extends AppCompatActivity {

    static String strWord = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TextView theWord = (TextView)
                findViewById(R.id.textViewWord);
        final EditText editTextGuess = (EditText)
                findViewById(R.id.editTextGuess);
        final Button buttonGuess = (Button)
                findViewById(R.id.btnGuess);
        final TextView theIncorrectGuesses = (TextView)
                findViewById(R.id.textViewIncorrectLetters);
        setContentView(R.layout.activity_main);
        alertMessage(this,"ERROR");
        strWord = getTheWord();
        theWord.setText(strWord);
        newGame();
        buttonGuess.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){

                theIncorrectGuesses.setText("ABCDEF");
            }
        });
    }
    public String getTheWord() {

        List<String> lines = new ArrayList<String>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                    new InputStreamReader(getAssets().open("WordsDoc.txt")));

            // do reading, usually loop until end of file reading
            String mLine;
            while ((mLine = reader.readLine()) != null) {
                //process line
                lines.add(mLine);
            }
            int intLen = 0;
            String[] strWords = lines.toArray(new String[lines.size()]);
            for (String strTemp : strWords){
                intLen ++;
            }
            int intRand = (int)(Math.random()* intLen);
            return strWords[intRand];

        } catch (IOException e) {
            //log the exception
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    //log the exception
                }
            }
        }
        return null;
    }
    public void newGame() {

    }
    public static void alertMessage(Activity a, String errMsg){

        final AlertDialog.Builder builder = new AlertDialog.Builder(a);
        builder.setTitle("Alert Dialog");
        builder.setMessage(errMsg);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setNeutralButton("OK", null);
        builder.create().show();
    }
}

编辑3

WordsDoc.txt文件的示例如下:

aardvark
Aarhus
Aaron
... continues 45000 more times

修改

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xxxx.hangman, PID: 4532
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.hangman/com.xxxx.hangman.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    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 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.xxxx.hangman.MainActivity.onCreate(MainActivity.java:34)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    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)
    05-05 08:44:32.994 4532-4532/com.xxxx.hangman I/Process: Sending signal. PID: 4532 SIG: 9

1 个答案:

答案 0 :(得分:1)

尝试使用扩展程序打开文件:

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(
                new InputStreamReader(getAssets().open("WordsDoc.txt")))
      ...

编辑:我的代码解决方案:

您正在尝试在setContentView(R.layout.activity_main)之前访问该视图,这就是您的视图为空的原因。

把这一行:

setContentView(R.layout.activity_main);
紧接着之后

 super.onCreate(savedInstanceState);
在您的MainActivity中

。像这样:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // the rest goes here