2011-04-01 76 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