2011-04-01 74 views
1

我哈瓦布局文件中像 的test.xml如何改变

<?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"> 
<TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:id="@+id/test_string" /> 
</RelativeLayout> 

和java文件中像这样sting.xml文件的字符串值。 test1.java

public class test1 extends Activity { 
String temp="This is String"; //which change dynamically 
TextView tempview; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    tempview=(TextView)findViewById(R.id.test_string); 
    tempview.setText(temp); 
    setContentView(R.layout.test); 
    } 
} 

什么,我试图做的是显示温度的活性值作为上述代码。但它会抛出一个NullPointerException。那么该怎么办。如何设置sting.xml的值? 在此先感谢..

回答

3

setContentView(R.layout.test);应该来之前(TextView)findViewById(R.id.test_string);

+0

感谢回答..我已经做到了你的建议。它不会抛出空指针异常。但它没有显示活动中的temp值 – Bikesh 2011-04-01 07:34:39

+0

您可以在test.xml中添加android:text =“This is a String”作为textView的参数 – MByD 2011-04-01 07:38:47

+0

感谢您的回复。如果我添加一个参数android:text =“这是一个字符串”,那么它将是静态的。但我需要动态改变文本。我怎样才能做到这一点? PLZ帮我解决这个问题。 – Bikesh 2011-04-03 04:25:18