【问题标题】:Android: Can't Pass Data Read From File To Multiple ClassesAndroid:无法将从文件读取的数据传递给多个类
【发布时间】:2025-11-25 03:00:02
【问题描述】:

我在将从文本文件读取的数据传递到应用程序中的另一个类时遇到问题。我可以很好地从文件中读取,但我认为我需要使用 Bundle,但我不知道该怎么做。我想让第二个类处理文本文件中的数据,然后在第一类中显示。

编辑:我已经弄清楚如何使用意图从文件中传递字符串。我仍在努力解决一些错误。

第二次编辑:我知道有一种更有效的方法。我可以让它工作的唯一方法是让 MainActivity 中的第一个按钮使用 startActivity(intent) 来允许 secondActivity 捆绑从文件中读取的字符串。

MainActivity.java:

public class MainActivity extends Activity {

    Button btn;
    Button bReadFile;
    TextView tvRead;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.btnNext);
        bReadFile = (Button) findViewById(R.id.btnRead);
        tvRead = (TextView) findViewById(R.id.tvMain);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //trying to find a way to remove this button
                Intent intent = new Intent(MainActivity.this, econdActivity.class);
                startActivity(intent);
            }
        });

        bReadFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String value = getIntent().getExtras().getString("key");
                tvRead.setText(value);
            }
        });
    }
}

secondActivity.java:

public class secondActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent mIntent = new Intent(secondActivity.this, MainActivity.class);
        mIntent.putExtra("key", readDataFile());
        startActivity(mIntent);
    }

    public String readDataFile() {
        String strData = null;
        InputStream is = null;
        try {
            is = getResources().openRawResource(R.raw.data_text);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            strData = br.readLine();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strData;
    }
}

【问题讨论】:

  • 奇怪你在做什么。从mainactivity你调用secondactivity,然后你再次在oncreate()中启动mainactivty,那么这个secondactivity有什么用。您可以在 mainactivity 本身中使用该 readDataFile() 方法。是不是你正在尝试做一些你实际需要的事情
  • 我真正想做的是让 secondActivity 句柄读取文本文件。我已经按照您的建议使用 MainActivity 处理文件输入进行了设置,但我想在添加更多内容时将其移至另一个类。
  • 我想我解释得不是很好。我希望第二个类处理文件的内容,然后根据需要将内容发送给第一类。我已经更新了代码,希望它更清晰。

标签: java android android-intent bundle onclicklistener


【解决方案1】:

根据您的要求使用以下编辑过的代码 MainActivity.java

public class MainActivity extends Activity {

    Button btn;
    Button bReadFile;
    TextView tvRead;
    String value;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.btnNext);
        bReadFile = (Button) findViewById(R.id.btnRead);
        tvRead = (TextView) findViewById(R.id.tvMain);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //trying to find a way to remove this button
                Intent intent = new Intent(MainActivity.this, secondActivity.class);
                startActivityForResults(intent,0);
            }
        });

        bReadFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                tvRead.setText(value);
            }
        });
    }
    @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data){
       value = data.getStringExtra("key");
}
}

secondActivity.java 的代码

public class secondActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = new Intent();
    i.putExtra("key", readDataFile());
    setResult(RESULT_OK, i);
    finish();
    }

    public String readDataFile() {
        String strData = null;
        InputStream is = null;
        try {
            is = getResources().openRawResource(R.raw.data_text);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            strData = br.readLine();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strData;
    }
}

【讨论】:

  • 谢谢,帮了大忙。看起来 setResult 方法正是我想要的。