0

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

どこが間違っているのかわかりません。

4

5 に答える 5

2

AssetManager assetManager = c.getAssets();あなたが適切に渡していないので、問題はこれにありますContext

宣言しContextていますが、 のどこにも初期化していませんAsynTask。以下のようにします。

Context c;

c=activity.this;

それ以外の場合、同じクラスで ayntask を使用している場合は、アクティビティContextを関数に直接渡します。

readFileFromAssets(fileName,activity.this);

更新しました

の構築を作成しDownloadJSON、コンテキストを取得して、以下のように同じコンテキストを使用します。

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

    }

DownloadJSONそして、必要なパスを呼び出しながらContext

于 2013-09-24T05:30:14.260 に答える
1

ContextのためにNullPointerを取得しています。Contextはnullを渡しているため、Contextを次のように初期化します

コンテキスト c; c=getApplicationContext() または c=Activity name.this

于 2013-09-24T05:42:19.257 に答える
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);

ありがとう....

于 2013-09-24T05:47:45.840 に答える
0

asynctaskからこれを呼び出すときはいつでも、パラメータとしてactivity渡してください。これcontextは最終的にコンテキストのエラーです。

コンテキストを渡す正しい方法をお願いします。

お分かりできると良いのですが。

于 2013-09-24T05:34:41.483 に答える