2014-02-25 47 views
0

我想動態添加textview。當我嘗試這樣做IAM越來越零點異常動態添加TextView導致空點異常

這是我的代碼片段:提前

LinearLayout layout = (LinearLayout) findViewById(R.layout.activity_main); 
LayoutInflater Inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
View vs = (View) Inflater.inflate(R.layout.serverd_details, null); 
TextView textView = (TextView) vs.findViewById(R.id.SerText); 
textView.setText("server details"); 
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.WRAP_CONTENT, 
     LinearLayout.LayoutParams.WRAP_CONTENT); 
layout.addView(textView, p); 

謝謝...

+0

發佈您的日誌... – Vamshi

+0

從logcat發佈錯誤日誌 –

回答

0

改變你的LinearLayout這樣

LinearLayout layout=(LinearLayout)findViewById(R.Id.your_layout_ID) 

TextView textView = (TextView) vs.findViewById(R.id.SerText); 

是SerText ID被放置在你的XML佈局

0

你好Karthick Pandiyan,

我覺得你做得很好,但看你的代碼後,我想建議你下面的代碼

使用。

TextView textView = new TextView(context); 
textView.setText("server details"); 
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, 
LinearLayout.LayoutParams.WRAP_CONTENT); 
layout.addView(textView, p); 

我認爲它會工作。