从assets文件夹中读取文本文件时出现NullpointerException

时间:2013-09-24 05:21:18

标签: android android-asynctask nullpointerexception

我正在尝试读取其中包含json格式内容的txt文件,我正在使用异步任务从资产文件夹中读取文件,但是获取nullpointer异常。下面是我的代码。

public class DownloadJSON extends AsyncTask<Void, Void, Void> {
    private MyDBAdapter dbHelper;

    String fileName = "json.txt";
     Context c;
     private static final String result = null;
    ArrayList<HashMap<String, String>> arraylist;

    @Override
    protected Void doInBackground(Void... params) {

         readFileFromAssets(fileName,c);
        return null;

}

 public static String readFileFromAssets(String fileName, Context c) {
     AssetManager assetManager = c.getAssets();
     InputStream is = null;
        try {
            is = assetManager.open(fileName);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String text = new String(buffer);
System.out.println("tex===========t"+ text);
            return text;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
 }

以下是我的日志跟踪

09-24 10:53:25.430: E/AndroidRuntime(1714): Caused by: java.lang.NullPointerException
09-24 10:53:25.430: E/AndroidRuntime(1714):     at com.markupartist.android.actionbar.example.DownloadJSON.readFileFromAssets(DownloadJSON.java:75)
09-24 10:53:25.430: E/AndroidRuntime(1714):     at com.markupartist.android.actionbar.example.DownloadJSON.doInBackground(DownloadJSON.java:27)
09-24 10:53:25.430: E/AndroidRuntime(1714):     at com.markupartist.android.actionbar.example.DownloadJSON.doInBackground(DownloadJSON.java:1)
09-24 10:53:25.430: E/AndroidRuntime(1714):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-24 10:53:25.430: E/AndroidRuntime(1714):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-24 10:53:25.430: E/AndroidRuntime(1714):     ... 5 more

不确定我在哪里做错了。

5 个答案:

答案 0 :(得分:3)

你没有初始化上下文对象,这就是它给你nullpointer exception初始化它的原因..

Context c=getApplicationContext();

答案 1 :(得分:2)

问题出现在AssetManager assetManager = c.getAssets();上,因为您没有传递正确的Context

您宣布Context但未在AsynTask的任何位置初始化,请执行以下操作:

Context c;

c=activity.this;

否则,如果您在同一个班级中使用asyntask,则直接将活动Context传递给您的函数。

  

readFileFromAssets(文件名,activity.this);

<强>更新

创建DownloadJSON的构造,获取上下文并使用如下所示的相同上下文:

  public class DownloadJSON extends AsyncTask<Void, Void, Void> {
   Context ctx;
    public DownloadJSON(Context c) {
    ctx=c;

    }

在调用DownloadJSON时必须通过Context

答案 2 :(得分:1)

由于Context,您正在获取NullPointer,Context正在传递null,因此请将Context初始化为

上下文c; c = getApplicationContext()或c = Activity name.this

答案 3 :(得分:1)

您尚未初始化您的上下文..所以首先制作您的类构造函数...

ArrayList<HashMap<String, String>> arraylist;
Context ctx;

public DownloadJSON (Context c, ArrayList<HashMap<String, String>> list) {
    // TODO Auto-generated constructor stub
    this.ctx = c;
    this.arraylist= list;

}

最后在

中使用此ctx对象
readFileFromAssets(fileName,ctx);

...谢谢

答案 4 :(得分:0)

每当您从asynctask拨打此activity时,请将context作为参数传递。这最终是上下文错误。

所以请正确的方式传递上下文。

我希望你明白。

相关问题