我尝试在Android设备内存中保存并读取字符串。 但是我无法理解错误:
当我在活动类中使用这些代码时,我想使用它,但是当我尝试将它放在另一个类中并将该类添加到另一个活动类时,它会崩溃。
我安装此功能就像这样:
SaveData save = new SaveData(getbasecontext());
保存并阅读字符串代码:
package com.example.aac_gps_gprs_v1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.content.Context;
import android.util.Log;
public class SaveData {
private Context context;
public SaveDate(Context mycontext)
{
context = mycontext;
}
public void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
public String readFromFile() {
String ret = "";
try {
InputStream inputStream = context.openFileInput("config.txt");
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}
}
我得到的Logcat:
09-25 09:26:36.324: E/AndroidRuntime(1460): FATAL EXCEPTION: main
09-25 09:26:36.324: E/AndroidRuntime(1460): Process: com.example.aac_gps_gprs_v1, PID: 1460
09-25 09:26:36.324: E/AndroidRuntime(1460): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aac_gps_gprs_v1/com.example.aac_gps_gprs_v1.MainActivity}: java.lang.NullPointerException
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.app.ActivityThread.access$700(ActivityThread.java:135)
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.os.Handler.dispatchMessage(Handler.java:102)
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.os.Looper.loop(Looper.java:137)
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.app.ActivityThread.main(ActivityThread.java:4998)
09-25 09:26:36.324: E/AndroidRuntime(1460): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 09:26:36.324: E/AndroidRuntime(1460): at java.lang.reflect.Method.invoke(Method.java:515)
09-25 09:26:36.324: E/AndroidRuntime(1460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
09-25 09:26:36.324: E/AndroidRuntime(1460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
09-25 09:26:36.324: E/AndroidRuntime(1460): at dalvik.system.NativeStart.main(Native Method)
09-25 09:26:36.324: E/AndroidRuntime(1460): Caused by: java.lang.NullPointerException
09-25 09:26:36.324: E/AndroidRuntime(1460): at com.example.aac_gps_gprs_v1.SaveData.readFromFile(SaveData.java:41)
09-25 09:26:36.324: E/AndroidRuntime(1460): at com.example.aac_gps_gprs_v1.MainActivity.onCreate(MainActivity.java:233)
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.app.Activity.performCreate(Activity.java:5243)
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-25 09:26:36.324: E/AndroidRuntime(1460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
09-25 09:26:36.324: E/AndroidRuntime(1460): ... 11 more