Search code examples
androidandroid-layoutandroid-tabhost

how do I findviewbyid of a view inside a tabhost from an activity thats inside the tabhost?


I'm trying to retrieve a view but it keeps returning null, how do I find it?

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

}

I keep getting a NullPointerException at v = (view) .... etc.


Solution

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

    replace the above two lines with below

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

    also TextView can be replaced with your control either ImageView and any other.