2016-07-13 50 views
1

我wan't设置的TextView的字符串值文本我在string.xml变化的TextView与Java类string.xml资源

,所以我想在java类与开关的情况下做到这一点

但不能置我TextView的文本,当我运行应用程序,TextView的是空

对不起,如果它是一个简单的愚蠢的错误:)

这是我的布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent" 
android:background="@drawable/shape"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textview" 
    android:textColor="@color/txt_dialog" 
    android:textSize="25sp"/> 

</RelativeLayout> 

,这是string.xml

<resources> 
<string name="t1"> hi </string> 
<string name="t2"> hello </string> 
<string name="t3"> ok </string> 
</resources> 

最后,这是我的Java代码应该选择那些字符串蒙山随机选择一个,然后设置,在对话框

public void hi(){ 
    TextView txt_truth = (TextView) findViewById(R.id.textview); 
    final Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.mylayout); 
    Random random = new Random(); 
    int hi_random = random.nextInt(2) + 1; 

    switch (hi_random){ 
     case 1: 
      if (txt_truth != null) { 
       txt_truth.setText(getResources().getString(R.string.t1)); 
      } 
      break; 
     case 2: 
      if (txt_truth != null) { 
       txt_truth.setText(getResources().getString(R.string.t2));} 
      break; 
     case 3: 
      if (txt_truth != null) { 
       txt_truth.setText(getResources().getString(R.string.t3));} 
      break;} 
    dialog.show(); 
} 
+1

显而易见的:'txt_truth.setText(getResources()的getString(R.string.t1));'但是你的字符串被命名为'A1,A2, ...',而不是't1,t2,...' –

+0

对不起,这个错误发生在我写这个时,我现在编辑 –

+0

另外,如果你是基于这个:'int hi_random = random.nextInt 45)+ 1;'很有可能你得到的数字超出了你的转换范围。 –

回答

3

你必须得到从对话视图通货膨胀的TextView的id这样

View view = LayoutInflater.from(this).inflate(R.layout.mylayout, null); 
    dialog.setContentView(view); 
    TextView txt_truth = (TextView) view.findViewById(R.id.textview); 
+0

那是什么ctx? –

+0

@MohsenMohseni:ctx表示上下文,你可以通过这个,取决于你在哪里使用 –

+0

这个工作,非常感谢 –

0
的TextView

检查您的字符串资源的名称,他们是a1或t1系列

public void hi(){ 
TextView txt_truth = (TextView) findViewById(R.id.textview); 
final Dialog dialog = new Dialog(this); 
dialog.setContentView(R.layout.mylayout); 
Random random = new Random(); 
int hi_random = random.nextInt(45) + 1; 

switch (hi_random){ 
    case 1: 
     if (txt_truth != null) { 
      txt_truth.setText(getResources().getString(R.string.a1)); 
     } 
     break; 
    case 2: 
     if (txt_truth != null) { 
      txt_truth.setText(getResources().getString(R.string.a2));} 
     break; 
    case 3: 
     if (txt_truth != null) { 
      txt_truth.setText(getResources().getString(R.string.a3));} 
     break;} 
    dialog.show(); 
} 
+0

他已纠正它 –

2

不能置我TextView的文本,当我运行应用程序,TextView的 是空

string.xml只有三个,其希望在TextView中显示随机字符串,但使用的是45获取随机数,生成范围为[0-45]+1

1之间

获取数3为:

int hi_random = random.nextInt(2) + 1; 
0

在我的头顶,我认为错误在你的随机生成器中。您指定的上限值为45.

nextInt(int n)方法用于获取伪随机数,均匀分布的int值在0(包含)与指定值(不包含)之间,从此随机数绘制发电机的顺序。

int hi_random = random.nextInt(3) + 1; 

请尝试上面的代码,并评论它是否有效。

您还可以检查nextInt的Java文档:nextInt javadoc

+1

'random.nextInt(3)+ 1'这会给int作为'1,2,3,4'但'4'不在开关的情况下 –

+0

它的下限和上限排他。我在答案中提到了这一点 –