2012-04-05 9 views
0

Ich versuche, eine Ansicht abzurufen, aber sie gibt immer null zurück, wie finde ich sie?Wie finde ich viewbyid einer Ansicht innerhalb eines Tabhost von einer Aktivität, die innerhalb des Tabhost ist?

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)); 
} 

} 

Ich halte eine Nullpointer bei v bekommen = (Ansicht) .... usw.

+0

teilen Sie Ihren Code .. –

+0

Sie müssen überprüfen Sie Ihre ViewId korrekt in Ihrem Layout.Teilen Sie den Code und kürzen Sie die Frage Beschreibung, wie es noch größer ist als die eigentliche Frage. – Sankalp

+0

Post einige Code ... –

Antwort

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

ersetzen die über zwei Leitungen mit unter

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); 

können auch Textview ersetzt werden mit Ihrer Kontrolle entweder ImageView und andere. diese

0

Verwendung:

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); 

Wenn Sie Textview

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

hinzufügen Da die null macht Warnung ..

R.id.trip_layout ist das übergeordnete Layout Ihrer XML-Layout.