как вы видите, я пытаюсь установить массив String и получить его в Android, он не дает мне такой файл / каталог, я добавил разрешение в mainfist для использования внешнего хранилища
попробовал его на моем устройстве Android и эмуляторес той же ошибкой
иногда выдает "не удается создать исключение нулевого указателя активности"
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv= (TextView) findViewById(R.id.ee);
String[] descs = {
"Java T-shirt",
"Java Mug",
"Duke Juggling Dolls",
"Java Pin",
"Java Key Chain"
};
saveArray(descs);
String[] s =loadArray();
tv.setText(s[2]);
}
public void saveArray(String[] output_veld) {
try {
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath() + "/MyFiles");
directory.mkdirs();
File file = new File(directory,"textfile.txt");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(output_veld);
out.flush();
out.close();
}
catch (IOException e) {
System.out.println(e);
}
}
public String[] loadArray() {
try {
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath() + "/MyFiles");
File file = new File(directory +"textfile.txt");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fis);
String[] gelezen_veld = (String[])in.readObject();
in.close();
return gelezen_veld;
}
catch (Exception e) {
System.out.println(e);
}
return null;
}
logcat:
02-17 17:34:07.884: E/AndroidRuntime(1533): FATAL EXCEPTION: main
02-17 17:34:07.884: E/AndroidRuntime(1533): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.one/com.test.one.TestActivity}: java.lang.NullPointerException
02-17 17:34:07.884: E/AndroidRuntime(1533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-17 17:34:07.884: E/AndroidRuntime(1533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-17 17:34:07.884: E/AndroidRuntime(1533): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-17 17:34:07.884: E/AndroidRuntime(1533): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-17 17:34:07.884: E/AndroidRuntime(1533): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 17:34:07.884: E/AndroidRuntime(1533): at android.os.Looper.loop(Looper.java:123)
02-17 17:34:07.884: E/AndroidRuntime(1533): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-17 17:34:07.884: E/AndroidRuntime(1533): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 17:34:07.884: E/AndroidRuntime(1533): at java.lang.reflect.Method.invoke(Method.java:521)
02-17 17:34:07.884: E/AndroidRuntime(1533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-17 17:34:07.884: E/AndroidRuntime(1533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-17 17:34:07.884: E/AndroidRuntime(1533): at dalvik.system.NativeStart.main(Native Method)
02-17 17:34:07.884: E/AndroidRuntime(1533): Caused by: java.lang.NullPointerException
02-17 17:34:07.884: E/AndroidRuntime(1533): at com.test.one.TestActivity.onCreate(TestActivity.java:50)
02-17 17:34:07.884: E/AndroidRuntime(1533): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-17 17:34:07.884: E/AndroidRuntime(1533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)