我想在一個 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);
}
}
如果你想在 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");