尝试将字符串写入 Android 应用程序上的文件在打开时崩溃



我正在编写一个简单的日记应用程序。我目前已经把它打开了,在文本框中键入内容,然后单击按钮保存它(但尚未实际保存(。如果我按如下所示运行它(带有/注释掉/部件注释掉(,那么它可以工作,但是如果我将注释设置为代码,它会在启动时崩溃。我已将其缩小到第一行注释代码:File path = this.getFilesDir();导致崩溃的原因,尽管其他地方可能还有更多我尚未发现的问题。

知道它为什么崩溃吗?或者更好的方法让我将文本保存到文件?

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) 

问题是您无法全局访问this,因为不会分配上下文,因此返回 null。

在下面尝试

public class MainActivity extends AppCompatActivity {
@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);
File path = this.getFilesDir();
File file = new File(path, "dreamWritings.txt");
FileOutputStream stream;
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());
tv.setText("Hey I think we wrote that to a file!");
}
catch(IOException ex) {
System.out.println(ex);
}finally{ 
stream.close();
}
tv.setText("Your dream has been recorded");
}
});
}

最新更新