2013-05-12 83 views
1

我嘗試保存我的密碼到活動&我想將其恢復到不同的活動,但是當我的應用程序啓動第二個活動,它崩潰。 有人可以幫助我嗎?保存在一個活動我的價值,並顯示該數值到不同的活動

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); 
    } 
} 

第一部分工作,因爲我可以在同一活動中讀取我保存的文件。 但是當我嘗試讀取到另一個活動,它一點兒也不工作:

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); 
    } 

} 
+0

發表您的logcat數據 – stinepike 2013-05-12 18:09:39

+0

我認爲你應該使用startActivityForResult而不是startActivity .. 反正,發佈您的logcat,這將有助於我們看到什麼導致應用程序崩潰 – Elior 2013-05-12 18:13:10

+0

確保ü宣佈'SecondActivity'活動在'AndroidManifest .xml'? – 2013-05-12 18:15:27

回答

2

如果你想活動之間共享數據,你可以這樣做:

  1. 意向,並把額外的數據在包
  2. SharePreferences(您可以訪問它,即使在應用程序關閉&再次重新啓動)
  3. 的SQLite數據庫(您可以訪問它,即使在應用程序關閉&再次重新啓動)
  4. 靜態類

這是怎麼了你可以使用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"); 
+0

這不僅是在活動之間共享數據,而且在同一時間保存這些數據。因爲在這裏,我嘗試保存密碼... – d3vpasha 2013-05-12 20:24:57

+0

好的...然後你需要使用SharedPrefs,我將編輯我的答案 – 2013-05-12 20:27:42

+0

好吧謝謝,但是我顯示的只是一個測試,因爲我的目標是創建一個登錄頁面,用戶只輸入密碼(不是用戶名)。在這個主要活動中,我添加一個按鈕來更改他的密碼。所以我想在新的活動中恢復這個新密碼並更改MainActivity中的舊密碼.. – d3vpasha 2013-05-12 20:35:38

4

我知道傳遞活動之間變量的最簡單方法如下:

您的活動創建一個新的意圖:

Intent intent = new Intent(getApplicationContext(), NextActivity.class); 
intent.putExtra("nameofvariable","valueofvariable"); 
startActivity(intent); 

而且從下一個活動,你可以用這種方式檢索此值:

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    String value = extras.getString("nameofvariable"); 
} 

然而,對於使用的FileInputStream正確的做法是這樣的:

FileInputStream in = openFileInput("hello_file.txt"); 
    InputStreamReader inputStreamReader = new InputStreamReader(in); 
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while ((line = bufferedReader.readLine()) != null) { 
     sb.append(line); 
    } 
+0

是的,但是當你使用putExtra作爲intent時,這個變量不會被保存。在這裏,我嘗試保存密碼並顯示另一個活動。我嘗試使用SharedPrefences,但它不工作太多..我會嘗試你的第二個解決方案,我回來;) – d3vpasha 2013-05-12 20:24:11

+0

它也沒有工作,同樣的錯誤:\ – d3vpasha 2013-05-12 20:30:58

+0

@ forgive90發佈你的錯誤。編輯您的問題,並將其附加在底部。 – GVillani82 2013-05-12 20:32:37

0

我測試您的應用程序在仿真器(8級)和三星galaxy S3(16級),它工作正常!

第二屆活動的TextView的是1234

在哪些設備

你的測試?

而且你能不能給我們你的logcat登錄?

不要錯過申報在AndroiManifest.xml文件中的所有活動。

感謝

1

如果你想活動之間共享數據,可以使用:

  • 意向,並把額外的數據在包
  • SharePreferences在應用程序類
  • SQLite數據庫
  • 靜態變量
0

您可以使用SharePreferences在第一activty保存您的密碼進去:

package com.example.test; 

public class FirstActivity extends Activity { 
SharedPreferences pref; 
SharedPreferences.Editor editor editor; 

private final String KEY_PASSWORD = "password"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button button = (Button)findViewById(R.id.button); 
    button.setText(finall); 
    button.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) { 
      pref = getSharedPreferences("passwordPref", 0); 
      SharedPreferences.Editor editor editor = pref.edit(); 
      editor.putString(KEY_PASSWORD ,YourEditBox.getText()); 
      editor.commit(); 
      Intent intent = new Intent(FirstActivity.this, SecondActivity.class); 
      startActivity(intent); 

     } 
    }); 

} 

} 

而在第二activty使用這個檢索您的密碼:

package com.example.test; 

public class SecondActivity extends Activity { 
SharedPreferences pref; 
SharedPreferences.Editor editor editor; 

private final String KEY_PASSWORD = "password"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    pref = getSharedPreferences("passwordPref", 0); 
    YourEditText.setText(pref.getString(KEY_PASSWORD,"")); 
} 

} 
+0

嗨,我的朋友,我會嘗試你的代碼,但你可以在我以前的代碼中編輯你的答案嗎?非常感謝你 – d3vpasha 2013-05-12 21:11:37

+0

嗨,我很抱歉延誤,我更新了我的答案。 – 2013-05-14 13:05:54

相關問題