2 znl 12 znl_12 于 2013.08.05 17:17 提问

在一个 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
curie_87   2013.08.06 10: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");
Jave_
Jave_   2013.08.05 17:28

保存用户名密码可以采用SharedPreferrence,系统提供的保存方法。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!