這是我目前的情況:
Eclipse的
Win7的
三星SII(用於測試)安卓:寫入文件的OnDestroy()
我寫將從設備上私自存儲在文件中讀取的應用當它啓動時,然後,我想要在應用程序的onDestroy()
被調用時寫入相同的文件(基本覆蓋)。
我首先嚐試用我自己的類擴展應用程序,然後讀取和寫入該文件的構造函數並銷燬該類,但是,該類中的讀取或寫入似乎不被允許作爲我的應用程序每次啓動時都會崩潰。所以我想了一會兒,然後自己在主菜單上做了這件事,因爲這是我唯一不需要的活動,不是"destroy" (or ActivityName.this.finish();)
。但我的程序員的大腦想要在應用程序級別實現這一點,而不是活動級別。
那麼,我有可能從我創建的Application類構造函數中的文件中讀取,然後寫入onDestroy()
中的文件?如果是這樣,怎麼樣?我的應用程序只是不斷崩潰/甚至不會啓動...
這是我使用的代碼。這有效地讀取一個文件,並將一些字符串存儲到全局內存中(請注意「不,不」)請全局變量對我非常有用),然後使用全局變量中的值調用onDestroy()
時覆蓋文件瓦爾(我知道這一點,因爲它工作在活動水平):現在
CONSTRUCTOR:
super();
try
{
String origprefs =",0,1-1";
File f = new File(this.getFilesDir(),FILENAME);
if(!f.exists())
{
FileOutputStream fos2 = new FileOutputStream(f);
fos2.write(origprefs.getBytes());
fos2.close();
}
FileInputStream fis;
fis = openFileInput(FILENAME);
StringBuffer fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
fileContent.append(new String(buffer));
}
fis.close();
origprefs = fileContent.toString();
//set our globals
Username = origprefs.split(",")[0];
Score = origprefs.split(",")[1];
Stage = origprefs.split(",")[2];
}
catch (IOException e)
{
throw new RuntimeException("Unable to either Read or Write to Match Two file.");
}
onDestroy:
if(GameRunning)
{
try
{
FileOutputStream fis;
fis = openFileOutput(FILENAME, this.MODE_PRIVATE);
String fileContent = Username + "," + Score + "," + Stage;
fis.write(fileContent.getBytes());
fis.close();
}
catch(IOException e)
{
throw new RuntimeException("Unable to either Read or Write to Match Two file.");
}
}
,這是相同的過程中,減去了一些改建,以獲得適當的情況下,MainMenu的類中。當我在應用程序級別執行操作時,我無法將自己的頭包裹起來,爲什麼會使我的應用程序崩潰?謝謝你的幫助!
這裏的logcat的條目 logcat的是神祕的我覺得......
08-08 02:03:58.362: E/AndroidRuntime(10360): FATAL EXCEPTION: main
08-08 02:03:58.362: E/AndroidRuntime(10360): java.lang.RuntimeException: Unable to instantiate application maxovrdrv.FirstApp.MatchTwo: java.lang.NullPointerException
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.app.LoadedApk.makeApplication(LoadedApk.java:466)
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3268)
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.app.ActivityThread.access$2200(ActivityThread.java:117)
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.os.Looper.loop(Looper.java:130)
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.app.ActivityThread.main(ActivityThread.java:3691)
08-08 02:03:58.362: E/AndroidRuntime(10360): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 02:03:58.362: E/AndroidRuntime(10360): at java.lang.reflect.Method.invoke(Method.java:507)
08-08 02:03:58.362: E/AndroidRuntime(10360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
08-08 02:03:58.362: E/AndroidRuntime(10360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
08-08 02:03:58.362: E/AndroidRuntime(10360): at dalvik.system.NativeStart.main(Native Method)
08-08 02:03:58.362: E/AndroidRuntime(10360): Caused by: java.lang.NullPointerException
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)
08-08 02:03:58.362: E/AndroidRuntime(10360): at maxovrdrv.FirstApp.MatchTwo.<init>(MatchTwo.java:26)
08-08 02:03:58.362: E/AndroidRuntime(10360): at java.lang.Class.newInstanceImpl(Native Method)
08-08 02:03:58.362: E/AndroidRuntime(10360): at java.lang.Class.newInstance(Class.java:1409)
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.app.Instrumentation.newApplication(Instrumentation.java:957)
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.app.Instrumentation.newApplication(Instrumentation.java:942)
08-08 02:03:58.362: E/AndroidRuntime(10360): at android.app.LoadedApk.makeApplication(LoadedApk.java:461)
08-08 02:03:58.362: E/AndroidRuntime(10360): ... 11 more
馬特
發佈logcat輸出也如果它的崩潰 – nandeesh 2012-08-08 05:54:21
logcat是神祕的我發現... – MaxOvrdrv 2012-08-08 06:04:46
請不要使用StringBuffer時,你可以使用StringBuilder。 – 2012-08-13 08:09:18