我尝试使用android的内部存储在txt文件中写入数据,当我直接在mainActivity.java的“buttonClicked”函数中使用代码时它工作正常,但是当我尝试在模拟器类中调用该方法时崩溃。谢谢!
public class MainActivity extends Activity {
// the class object ----------------------
FileHandler myFileHandler = new FileHandler();
public static final String FILENAME = "myFile.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonClicked(View view) {
// Call the method in the Filehandler Class
myFileHandler.saveData(FILENAME);
} ...
文件处理程序类
public class FileHandler extends Activity {
private static final String TAG = MainActivity.class.getName();
FileHandler() {
}
void saveData(String FILENAME) {
Log.d(TAG, FILENAME);
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
outputStreamWriter.write("hallo welt");
outputStreamWriter.close();
}
catch (IOException e) {
Log.e(TAG, "File write failed: " + e.toString());
}
} ...
这很好用……</p>
public void buttonClicked(View view) {
myFileHandler.saveData(FILENAME);
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_PRIVATE));
outputStreamWriter.write("hallo welt");
outputStreamWriter.close();
}
catch (IOException e) {
Log.e(TAG, "File write failed: " + e.toString());
}
}
所有类都正确导入,来自 log cat 的错误
11-11 11:49:22.646: E/AndroidRuntime(833): FATAL EXCEPTION: main
11-11 11:49:22.646: E/AndroidRuntime(833): java.lang.IllegalStateException: Could not execute method of the activity
11-11 11:49:22.646: E/AndroidRuntime(833): at android.view.View$1.onClick(View.java:2072)
11-11 11:49:22.646: E/AndroidRuntime(833): at android.view.View.performClick(View.java:2408)
11-11 11:49:22.646: E/AndroidRuntime(833): at android.view.View$PerformClick.run(View.java:8816)
11-11 11:49:22.646: E/AndroidRuntime(833): at android.os.Handler.handleCallback(Handler.java:587)
11-11 11:49:22.646: E/AndroidRuntime(833): at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 11:49:22.646: E/AndroidRuntime(833): at android.os.Looper.loop(Looper.java:123)
11-11 11:49:22.646: E/AndroidRuntime(833): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-11 11:49:22.646: E/AndroidRuntime(833): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 11:49:22.646: E/AndroidRuntime(833): at java.lang.reflect.Method.invoke(Method.java:521)
11-11 11:49:22.646: E/AndroidRuntime(833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-11 11:49:22.646: E/AndroidRuntime(833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-11 11:49:22.646: E/AndroidRuntime(833): at dalvik.system.NativeStart.main(Native Method)
11-11 11:49:22.646: E/AndroidRuntime(833): Caused by: java.lang.reflect.InvocationTargetException
11-11 11:49:22.646: E/AndroidRuntime(833): at com.example.iLoveMyMobile.MainActivity.buttonClicked(MainActivity.java:55)
11-11 11:49:22.646: E/AndroidRuntime(833): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 11:49:22.646: E/AndroidRuntime(833): at java.lang.reflect.Method.invoke(Method.java:521)
11-11 11:49:22.646: E/AndroidRuntime(833): at android.view.View$1.onClick(View.java:2067)
11-11 11:49:22.646: E/AndroidRuntime(833): ... 11 more
11-11 11:49:22.646: E/AndroidRuntime(833): Caused by: java.lang.NullPointerException
11-11 11:49:22.646: E/AndroidRuntime(833): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
11-11 11:49:22.646: E/AndroidRuntime(833): at com.example.iLoveMyMobile.FileHandler.saveData(FileHandler.java:36)
11-11 11:49:22.646: E/AndroidRuntime(833): ... 15 more