我正在寫一個JSON文件被訪問,以消除從我的應用程序的網絡訪問/延遲儘可能多。我還沒有處理過將文件寫入內存的問題,並從Google的文檔中讀取了大部分讀/寫代碼,但我仍然遇到問題。FileOutputStream問題與寫入JSON從URL到內部存儲
目前,我在這條線得到一個空指針異常:
FileOutputStream fOut = openFileOutput("runesInfo.json", Context.MODE_PRIVATE)
這使我相信,從來沒有創建的文件,或者說我初始化它錯了,但我不知道怎麼樣。
完整的代碼段:
public class GetRunes extends AsyncTask<String, String, String> {
boolean runesCached = false;
protected String doInBackground(String[] runeId) {
String url = "https://prod.api.pvp.net/api/lol/static-data/" + region + "/v1.2/rune?api_key=" + api_key;
JSONParser jsonParser = new JSONParser();
JSONObject runeInfo = jsonParser.getJSONFromUrl(url);
String jsonString = runeInfo.toString();
String readJson = null;
Context context = GetStaticData.this;
if(!runesCached) {
Log.d("Cache", "Caching File");
try {
File file = new File(context.getFilesDir(), "runesInfo.json");
FileOutputStream fOut = openFileOutput("runesInfo.json", Context.MODE_PRIVATE);
fOut.write(jsonString.getBytes());
fOut.close();
Log.d("Cache", "Cache Complete");
runesCached = true;
} catch (Exception e){
e.printStackTrace();
}
}
String name = null;
try {
FileInputStream fis = openFileInput("runesInfo.json");
InputStreamReader isr = new InputStreamReader(fis);
fis.read(readJson.getBytes());
JSONObject storedJson = new JSONObject(readJson);
Log.d("Stored JSON", "" + storedJson);
JSONObject idJson = storedJson.getJSONObject("data");
JSONObject single = idJson.getJSONObject(runeId[0]);
try {
name = single.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
}
的logcat:
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats D/Cache﹕ Caching File
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ java.lang.NullPointerException
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at android.content.ContextWrapper.getFilesDir(ContextWrapper.java)
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetRunes.doInBackground(GetSt
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetRunes.doInBackground(GetSt
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java)
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java)
04-29 09:53:58.199 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
04-29 09:53:58.209 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
04-29 09:53:58.209 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
04-29 09:53:58.209 11273-11732/edu.appdesign.leaguestats W/System.err﹕ at java.lang.Thread.run(Thread.java)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ java.util.concurrent.ExecutionException: java.lang.NullPointerException
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.FutureTask.report(FutureTask.java)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.FutureTask.get(FutureTask.java)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask.get(AsyncTask.java)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData.getRuneInfo(GetStaticData.jav
04-29 09:53:58.209 11273-11732/edu.appdesign.leaguestats W/dalvikvm﹕ threadid=20: thread exiting with uncaught exception (group=0x41650e18)
04-29 09:53:58.209 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.RunesActivity$GetRunes.onPostExecute(RunesA
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.RunesActivity$GetRunes.onPostExecute(RunesA
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.Looper.loop(Looper.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java)
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit
04-29 09:53:58.219 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:133)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ Caused by: java.lang.NullPointerException
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.content.ContextWrapper.openFileInput(ContextWrapper.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetRunes.doInBackground(GetSt
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at edu.appdesign.leaguestats.GetStaticData$GetRunes.doInBackground(GetSt
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
04-29 09:53:58.229 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
04-29 09:53:58.239 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
04-29 09:53:58.239 11273-11273/edu.appdesign.leaguestats W/System.err﹕ at java.lang.Thread.run(Thread.java)
請分享logcat。 –
@ManishMulimani相關logcat部分加入 – Nate
我想,這可能是因爲錯誤的上下文。 GetRunes內部類是什麼? openFileOutput是Context的一種方法(很可能是Activity)。在代碼中它可能爲空,因爲doInBackground在單獨的線程中執行。所以,嘗試添加「上下文」。並檢查它是否不爲空。順便說一句,儘量避免在靜態變量中保持上下文。 – Olsavage