2014-09-24 41 views
0

在我的活動中,我無法通過Java中的putString訪問字符串。下面是代碼:如何在Java中通過putString傳遞字符串值

private void setButtonListeners() 
{ 
    SharedPreferences sp = this.getSharedPreferences("favs", MODE_PRIVATE); 
    Editor editor = sp.edit(); 
    editor.putString("HelloActivity", "com.example.hiapp.HelloActivity, com.example.hiapp.R.string.byebye"); 
    editor.commit(); 
} 

當我開始活動,一切都很好,但而不是顯示字符串的名稱,它只是顯示字符串的位置:com.example.hiapp.R.string。 byebye

對此有何想法?我不熟悉putString的使用。謝謝

編輯:截圖

enter image description here

EDIT 2

正如你在這個問題看,問題是:

editor.putString("HelloActivity", "com.example.hiapp.HelloActivity, com.example.hiapp.R.string.byebye"); 

定義:

putString (String key, String value) 

問題出在字符串值。它由:com.example.hiapp.HelloActivity組成,它是按鈕讓你訪問的活動的名稱。 和:com.example.hiapp.R.string.byebye這是我想要顯示的字符串名稱。這是唯一的錯誤,我不知道如何在不修改putString的情況下顯示任何字符串。

正如您在圖像中看到的,首先顯示字符串,並且有一個使用com.example.hiapp.HelloActivity的按鈕。但該字符串沒有顯示它的名字,它顯示出它的位置

+0

當您談論字符串的位置時,屏幕截圖不夠清晰。請簡要解釋。 – 2014-09-24 09:18:48

+0

目標是添加到最愛的活動。因此,在該活動中,我按下「添加」按鈕,以便在另一個活動中,我可以通過按鈕訪問已添加書籤的活動(在列表中創建)。我在這裏展示的是「被書籤的」活動 – 2014-09-24 09:23:09

+0

的代碼,以及它與String的完整位置有什麼關係? – 2014-09-24 09:25:00

回答

0

它應該是:

private void setButtonListeners() 
{ 
    SharedPreferences sp = this.getSharedPreferences("favs", MODE_PRIVATE); 
    Editor editor = sp.edit(); 
    editor.putString("HelloActivity", getString(R.string.byebye)); 
    editor.commit(); 
} 

這應該現在的工作。

+0

謝謝。我告訴你與MysticMagic相同的東西 – 2014-09-24 09:11:22

0

更改此

editor.putString("HelloActivity", "com.example.hiapp.HelloActivity, com.example.hiapp.R.string.byebye"); 

editor.putString("HelloActivity", getResources().getString(R.string.byebye)); 

editor.putString("HelloActivity", getString(R.string.byebye)); 

要包括位置,也可以試試這個:

editor.putString("HelloActivity", getString(R.string.byebye) + " and Location: com.example.hiapp.R.string.byebye"); 

您正在放置位置。所以它的顯示位置。

參考getResources()getString()

希望它能幫助。

+0

謝謝,但我忘了說字符串旁邊有一個按鈕,所以我需要指出字符串的位置和資源。我編輯並添加活動的圖像 – 2014-09-24 09:10:49

+0

我不確定,我明白你的意思。等待編輯的問題。 – 2014-09-24 09:14:45

+0

你想傳遞位置以及資源嗎? @Isaías – 2014-09-24 09:32:00

相關問題