如何在一个android页面中保存一个文本文件并在另一个页面中打开它?

svgewumm  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(228)

我正在构建一个应用程序,在其中,你在一个活动中输入文本到edittext中,然后你进入另一个活动,在那里你按下一个按钮,然后你进入diaryentry页面,这就是错误所在。
下面是save()代码,用于保存edittext中的文本

private String textFile;
private String fileName;
...
public void save(View v){
        fileName = getApplication().getExternalFilesDir("/").getAbsolutePath();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss", Locale.US);
        Date now = new Date();
        textFile = "Vent2Me.text." + formatter.format(now) + ".txt";

        String text = inputFeelings.getText().toString();
        FileOutputStream fos = null;

        try {
            fos = openFileOutput(textFile, MODE_PRIVATE);
            fos.write(text.getBytes());

            inputFeelings.getText().clear();
            Toast.makeText(this, "Saved to " + getFilesDir() + "/" + textFile, Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

以下是diaryentry.java:

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import androidx.appcompat.app.AppCompatActivity;

public class DiaryEntry extends AppCompatActivity {
    private ImageButton backToDiary;
    private EditText readText;
    private Button loadDiaryEntry;
    private String textFileToRead;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_entry);
        Intent intent = getIntent();

        //Initialising things
        backToDiary = (ImageButton) findViewById(R.id.back_to_diary);
        readText = (EditText) findViewById(R.id.read_text);
        loadDiaryEntry = (Button) findViewById(R.id.load_diary_entry);
    }

    public void load(View v){

        FileInputStream fis = null;
        try {
            fis = openFileInput(textFileToRead);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            String text;
            while ((text = br.readLine()) != null) {
                sb.append(text).append("\n");
            }
            readText.setText(sb.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

这是日志中的错误:

java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:6256)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:6256) 
        at android.view.View$PerformClick.run(View.java:24701) 
        at android.os.Handler.handleCallback(Handler.java:789) 
        at android.os.Handler.dispatchMessage(Handler.java:98) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.indexOf(int)' on a null object reference
        at android.app.ContextImpl.makeFilename(ContextImpl.java:2420)
        at android.app.ContextImpl.openFileInput(ContextImpl.java:513)
        at android.content.ContextWrapper.openFileInput(ContextWrapper.java:189)
        at android.content.ContextWrapper.openFileInput(ContextWrapper.java:189)
        at com.example.venttome.DiaryEntry.load(DiaryEntry.java:41)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
        at android.view.View.performClick(View.java:6256) 
        at android.view.View$PerformClick.run(View.java:24701) 
        at android.os.Handler.handleCallback(Handler.java:789) 
        at android.os.Handler.dispatchMessage(Handler.java:98) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

我真的很感激任何帮助,因为我不知道什么是错的,以及如何修复它。提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题