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);
} ...
fileHandler クラス
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());
}
}
すべてのクラスが正しくインポートされ、ログ 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