如何更改string.xml文件的字符串值

时间:2011-04-01 07:24:06

标签: android textview nullpointerexception

我有一个布局文件,如 的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文件。 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);
    }
}

我要做的是在上面的代码中显示temp的值。但它会抛出一个NullPointerException。那怎么办如何设置sting.xml的值??? 提前谢谢..

1 个答案:

答案 0 :(得分:3)

setContentView(R.layout.test);应该出现在(TextView)findViewById(R.id.test_string);

之前
相关问题