Witam
W jaki sposób prawidłowy otworzyć istniejący plik, a gdy nie istnieje utworzyć nowy?
Próbowałem w ten sposób ale otrzymuję błędy:
public class MyActivity extends Activity {
...
File_Class File_B = new File_Class(getFilesDir());
...
}
package com.example.kamil.towerdefense_v3;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class File_Class {
File HighScores;
boolean HighScores_B = true;
File_Class(File Dir){
HighScores = LoadFile(HighScores, "HS.save");
}
File LoadFile(File Dir, String Name){
File ThisFile = new File(Dir, Name);
if (!ThisFile.exists()){
try {
ThisFile.createNewFile();
}
catch(IOException IOE){
HighScores_B = false;
}
}
return ThisFile;
}
}
10-24 13:30:50.298 27913-27913/com.example.kamil.towerdefense_v3 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40ff82a0)
10-24 13:30:50.308 27913-27913/com.example.kamil.towerdefense_v3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kamil.towerdefense_v3/com.example.kamil.towerdefense_v3.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2005)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.access$700(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4856)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:192)
at com.example.kamil.towerdefense_v3.MyActivity.<init>(MyActivity.java:38)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1996)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.access$700(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4856)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)