如何读取其他 activity 中的 public static final String

How to read public static final String in other activity

我有 2 个主 activity,我将在其中将字符串保存在目录文件中,另一个 activity 将读取它的文件目录,这是我来自 activity 的代码保存文件

package com.example.java;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity2 extends AppCompatActivity {
    public static final String extraint = "1";
    public static final String namabarang = "2";
    public static final String jumlahstock ="3";
    public static final String FILE_NAME="example.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Intent intent = getIntent();

        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    public void onBtnClick(View view){


        TextView plain1 = findViewById(R.id.NamaBarang);
        TextView plain2 = findViewById(R.id.SKU);
        TextView plain3 = findViewById(R.id.JumlahStok);
        TextView plain4 = findViewById(R.id.HargaBeli);
        TextView plain5 = findViewById(R.id.HargaJual);
        Intent intent = new Intent(this,MainActivity3.class);
        String x = "1";

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

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

            Toast.makeText(this,"Saved to" +getFilesDir() + "/" + FILE_NAME,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();
                }
            }
        }

        intent.putExtra(extraint,x);
        intent.putExtra(FILE_NAME,text);
        intent.putExtra(namabarang,plain1.getText().toString());
        intent.putExtra(jumlahstock,plain3.getText().toString());
        startActivity(intent);



    }
}

这是应该读取文件的 activity

package com.example.java;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.renderscript.ScriptGroup;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;

import org.w3c.dom.Element;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;


public class MainActivity3 extends AppCompatActivity {
    private static final String Tag = "MainAcitivity3";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        Log.d(Tag, "onCreate: Started.");
        ListView mListView = (ListView) findViewById(R.id.listView);
        Intent intent = getIntent();

        String[] barang = new String[20];
        String[] jumlah = new String[20];
        ArrayList<Item> itemlist = new ArrayList<Item>();

        barang[0] = intent.getStringExtra(MainActivity2.namabarang);
        jumlah[0] = intent.getStringExtra(MainActivity2.jumlahstock);
        String text;
        FileInputStream fis = null;
        try {
            fis = openFileInput(FILE_NAME);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();

            while ((text = br.readLine()) != null){
                sb.append(text).append("\n");
            }


        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try{
                    fis.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }

        for(int i=0;i<20;i++) {
            Item sparepart = new Item(barang[i], jumlah[i]);

            itemlist.add(text);
        }
        PersonListAdapter adapter = new PersonListAdapter(this, R.layout.adapter_view_layout,itemlist);
        mListView.setAdapter(adapter);

        String number = intent.getStringExtra(MainActivity2.extraint);
        if(number != null) {
            FrameLayout lay = (FrameLayout) findViewById(R.id.frames);
            if (number.equals("1")) {
                lay.setVisibility(View.INVISIBLE);
                mListView.setVisibility(View.VISIBLE);
            } else {

            }
        }
        else{}

    }

    public void onBtnClick (View view){
        Intent intent = new Intent(this,MainActivity2.class);

        startActivity(intent);
    }



}

但是代码 fis = openFileInput(FILE_NAME); 中的问题无法读取 public 静态字符串 FILE_NAME,我尝试使用 intent 但它不起作用

错误很可能与语法有关。静态对象属于 classes 本身而不是那些 classes 的实例。由于 FILE_NAMEMainActivity2 class 的一个字段,而您正试图从 MainActivity3 访问它,那么您只需包含 class:

fis = openFileInput(MainActivity2.FILE_NAME);

例如,如果您在不同 class 中有另一个同名变量,则需要指定。如果您在 MainActivity3 中定义了 FILE_NAME 那么它当然会从中提取。