gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:40:02 25 4
gpt4 key购买 nike

我正在编写一个简单的日记应用程序。我目前已经可以打开它,在文本框中输入内容,然后单击按钮保存它(但尚未实际保存)。如果我按如下方式运行它(将/注释掉/部分注释掉),那么它就可以工作,但是如果我将注释设为代码,它就会在启动时崩溃。我已将其范围缩小到第一行注释代码: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");
}
});

}

关于java - 尝试将字符串写入 Android 应用程序上的文件,打开时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45646443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com