从 android 中的资产文件中读取

Reading from assets file in android

我正在开发 Hangman 游戏以自学 Android 开发。我已经在 Java 中为 PC 完成了所有工作并且运行良好。我正在使用 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

尝试打开扩展名为:

的文件
    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