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

问题描述:

我试图从位于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" /> 

这3条线,这一次在我“Fortune Cookie.iml”文件

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

不要调用从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()后:

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

所以你可以声明,但没有初始化任何东西,直到的onCreate()方法? – miatech

+0

@miatech:你可以初始化东西,而不是像继承的方法那样使用太多东西。许多您继承的方法在“Activity”完全设置完成之后才会起作用,并且之后您的第一个控制点就是'onCreate()'方法。 – CommonsWare