无法访问资产文件夹中的文本文件

时间:2016-11-21 21:46:00

标签: android text bufferedreader android-studio-2.2 asset-management

我试图从位于apps / main / src / asset的txt文件中读取内容。该文本包含以%开头的幸运饼干。但我的主要问题是它打开了文件。在Android Studio中打开文件的正确方法是什么?我创建了资产文件夹并将我的文本文件放在那里。但我不断收到错误。如果有人可以查看代码/错误并告诉我访问该文件的正确方法。我还添加了< 这是我的代码

package com.example.jorge.whatsyourname;

import android.content.res.AssetManager;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

import static java.lang.System.in;

public class MainActivity extends AppCompatActivity {
    //variables declaration
    EditText nameTxt;
    EditText lastnameTxt;
    EditText dobTxt;
    TextView cookieOutTxt;

    int random = 0; //generated random number
    int max = 0; //value to be assign on execution (number of fortunes)
    ArrayList<String> fortunes; //all fortunes will be stored here

    //opening text file located in assets directory
    AssetManager assetManager = getAssets();
    InputStream is = assetManager.open("fortuneCookie.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    public MainActivity() throws IOException {
    }


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

        //references
        nameTxt = (EditText) findViewById(R.id.nameTxt);
        lastnameTxt = (EditText) findViewById(R.id.lastnameTxt);
        dobTxt = (EditText) findViewById(R.id.dobTxt);
        cookieOutTxt = (TextView) findViewById(R.id.cookieOutTxt);

        final Button cookieBtn = (Button) findViewById(R.id.cookieBtn);
        cookieBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //scan file if line contains % count, else add to fortune arraylist
                boolean flag = false;
                String phrase = "";
                try {
                    while (br.readLine() != null && !br.readLine().isEmpty()) {
                        String line = br.readLine();
                        //System.out.println(line);
                        if (line.contains("%") && !flag) {
                            flag = true;
                        }
                        else if (line.contains("%") && flag) {
                            max++;
                            phrase = "";
                        }
                        else {
                            phrase = phrase + line;
                            fortunes.add(max, phrase);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //generating a random fortune
                random = (int) (Math.random() * max + 1);
                //printing output to screen
                cookieOutTxt.setText(fortunes.get(random));

            }
        });

    }


    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Main Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }
}

错误日志

adb shell am start -n "com.example.jorge.whatsyourname/com.example.jorge.whatsyourname.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 1573 on device Nexus_5_API_22 [emulator-5554]
I/art: Not late-enabling -Xcheck:jni (already on)
D/AndroidRuntime: Shutting down VM


                  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.jorge.whatsyourname, PID: 1573
                  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jorge.whatsyourname/com.example.jorge.whatsyourname.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5257)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference
                      at android.content.ContextWrapper.getAssets(ContextWrapper.java:80)
                      at com.example.jorge.whatsyourname.MainActivity.<init>(MainActivity.java:40)
                      at java.lang.reflect.Constructor.newInstance(Native Method)
                      at java.lang.Class.newInstance(Class.java:1606)
                      at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5257) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
I/Process: Sending signal. PID: 1573 SIG: 9
Application terminated.

另外我在app.iml文件中有这三行

<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
    <option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
    <option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
    <option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />

这个在我的&#34; Fortune Cookie.iml&#34;文件

<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />

1 个答案:

答案 0 :(得分:0)

implements Serializable之前,通常在Activity之后调用从onCreate()或其超类继承的方法。

所以,替换:

super.onCreate()

使用:

AssetManager assetManager = getAssets();
InputStream is = assetManager.open("fortuneCookie.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));

并将这些行放在AssetManager assetManager; InputStream is; BufferedReader br; 之后:

super.onCreate()