znl_12 2013-08-05 09:17 采纳率: 0%
浏览 3024
已采纳

在一个 activity 中保存值,然后在不同的 activity 中显示这个值

我想在一个 activity 保存我的密码,我想把密码恢复到不同的 activity 中,但是当程序开启第二个 activity 时就奔溃了,这是什么原因呢?

package com.example.test;
public class MainActivity extends Activity {
    String finall;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String FILENAME = "hello_file.txt";
        String string = "1234";

        FileOutputStream fos;
        try
        {
            fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            fos.write(string.getBytes());
            fos.close();
        } 
        catch (FileNotFoundException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); }

        try
        {
            FileInputStream in = openFileInput("hello_file.txt");
            StringBuffer fileContent = new StringBuffer("");
            byte[] buffer = new byte[4];

            while(in.read(buffer) != -1)
            {
                fileContent.append(new String(buffer));
            }
            finall = fileContent.toString();
            in.close();
        }
        catch (FileNotFoundException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); }

        Button button = (Button)findViewById(R.id.button);
        button.setText(finall);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                sendGo(v);

            }
        });
    }

    public void sendGo(View v)
    {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}

第一部分正常运行,因为我在相同的activity 中能读取我保存的文件。但是当我试着在另一个activity读取时,就不能运行。

package com.example.test;
public class SecondActivity extends Activity {
    String finall="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        // Show the Up button in the action bar.
        setupActionBar();
        try
        {
            FileInputStream in = openFileInput("hello_file.txt");
            StringBuffer fileContent = new StringBuffer("");
            byte[] buffer = new byte[4];
            while(in.read(buffer) != -1)
            {
                fileContent.append(new String(buffer));
            }
            finall = fileContent.toString();
            in.close();
        }
        catch (FileNotFoundException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); }

        TextView text = (TextView)findViewById(R.id.mehmet);
        text.setText(finall);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
  • 写回答

2条回答

  • Curie-87 2013-08-06 02:25
    关注

    如果你想在 activities 之间分享数据,你可以:
    1. 在 Bundle 中 中 Intent 然后再放入附加的数据
    2. SharePreferences(当程序关闭或重启时你可以访问它)
    3. SqLite 数据库(当程序关闭或重启时你可以访问它)
    4. Static 类

    如何使用 SharedPrefs 的方法:

    SharedPreferences pref = getBaseContext().getSharedPreferences(PREFERENCE_NAME, 0); 
    SharedPreferences.Editor editor editor = pref.edit();
    editor.putString("KEY_PASSORD", "Password to Save");
    editor.commit();
    

    当你想从 SharedPrefs 中检索密码时,使用:

    SharedPreferences Pref = getBaseContext().getSharedPreferences(PREFERENCE_NAME, 0);
    String password = pref.getString("KEY_PASSWORD","Default Password");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题