以下は、assetsフォルダに保存されたJSONファイルを読み取るためのコードです。androidのJSONファイルを読み取ることができません
public class ReadJson extends Activity {
public String loadJSONFromAsset() {
String json1 = null;
try {
InputStream is = getAssets().open("jsonfile1.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json1 = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json1;
}
}
アプリがクラッシュし、これを解決する方法
"Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference" exception.
を示して?
パスが正しいことを確認してください。 –
アクティビティの作成後にloadJSONを呼び出していることを確認してください。または、getApplicationContext.getAssets() – X3Btel
これを参照してください:http://stackoverflow.com/questions/9544737/read-file-from-assets – DysaniazzZ