私は簡単なジャーナルアプリを作成しています。私は現在、私はそれを開くことができるポイントに、テキストボックスに材料を入力し、それを保存する(しかし、実際にはそれを保存しない)ボタンをクリックします。下にあるように(/ がとコメントアウトされている)、それが動作しますが、コメントをコードにすると、起動時にクラッシュします。私はコードの最初のコメント行に絞り込んだ:File path = this.getFilesDir();
クラッシュの原因となるが、他の場所ではまだ私が見つけていないかもしれない。Androidアプリでファイルに文字列を書き込もうとすると開くときにクラッシュする
なぜそれがクラッシュしているのですか?または、テキストをファイルに保存するための良い方法はありますか?
public class MainActivity extends AppCompatActivity {
/* File path = this.getFilesDir();
File file = new File(path, "dreamWritings.txt");
FileOutputStream stream;*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView)findViewById(dreamText);
final Button button = (Button) findViewById(R.id.recordDream);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String dreamWords = tv.getText().toString();
/* try {
stream = new FileOutputStream(file, true);
}
catch(FileNotFoundException ex2){
System.out.println(ex2);
}
try {
stream.write(dreamWords.getBytes());
stream.close();
tv.setText("Hey I think we wrote that to a file!");
}
catch(IOException ex) {
System.out.println(ex);
}*/
tv.setText("Your dream has been recorded");
}
});
}
}
そしてここでクラッシュログです:
08-11 22:23:22.772 1443-1443/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sixpencegames.www.dreamjournal, PID: 1443
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.sixpencegames.www.dreamjournal/com.sixpencegames.www.dreamjournal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2568)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:223)
at com.sixpencegames.www.dreamjournal.MainActivity.<init>(MainActivity.java:22)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2558)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
以下
てみてくださいので – TooManyEduardos
を追加してください。 –