2014-09-24 38 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

相关问题