2012-04-05 88 views
0

我試圖檢索一個視圖,但它一直返回null,我怎麼找到它?如何從tabhost內部的活動中查找tabhost內的視圖的viewviewid?

public class TripDailyItinerary extends Activity { 
ViewGroup layout; 
View v; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onCreate(savedInstanceState); 
setContentView(R.layout.html_content); 
TextView content = (TextView) findViewById(R.id.htmlContent); 
layout = (ViewGroup) findViewById(R.layout.trip_content); 
v = (View) layout.findViewById(R.id.bg); //where the error is 


JSONObject reservation; 
int resNumber = ((MyApp) this.getApplication()).getResNum(); 
String dailyitinerary = ""; 

try { 
reservation = ((MyApp) this.getApplication()).getJSON().getJSONObject("response").getJSONArray("reservations").getJSONObject(resNumber); 
dailyitinerary = reservation.getString("dailyitinerary"); 
} catch (JSONException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
content.setText(Html.fromHtml(dailyitinerary)); 
} 

} 

我保持在V =(視圖)獲取一個NullPointerException ....等

+0

分享你的代碼.. – 2012-04-05 05:25:35

+0

你需要在layout中正確檢查你的viewId。分享代碼並縮短問題描述,因爲它甚至比實際的問題更大。 – Sankalp 2012-04-05 05:31:36

+0

發表了一些代碼... – 2012-04-05 05:33:15

回答

0
layout = (ViewGroup) findViewById(R.layout.trip_content); 
v = (View) layout.findViewById(R.id.bg); //where the error is 

下面

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view view = inflater.inflate(R.layout.R.layout.trip_content,null); 
TextView name = (TextView) view.findViewById(R.id.bg); 

也TextView的可更換更換上述兩個線與您的控制ImageView和任何其他。

0

使用此:

ViewGroup parent = (ViewGroup)findViewById(R.id.trip_layout); 
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View myView = inflater.inflate(R.layout.trip_content,parent); 

如果你想添加的TextView

TextView content = (TextView)myView.findViewById(R.id.htmlContent);

因爲null使得警告..

R.id.trip_layout是你的XML佈局的父佈局。