Search code examples
javaandroidviewviewflipper

i can't add view to my viewflipper


Here's my code :

public View anEpisode(String n, String i, String d){
    View v;
    LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.episodedescription, null);

    TextView nameOfEpisode = (TextView) findViewById(R.id.episodedescriptionname);
    ImageView imageOfEpisode = (ImageView) findViewById(R.id.episodedescriptionimage);
    TextView descriptionOfEpisode = (TextView) findViewById(R.id.episodedescriptiondescription);

    nameOfEpisode.setText(n);
    descriptionOfEpisode.setText(d);
    createUrlImage(imageOfEpisode, i);

    return v;
}


    flipper.addView(anEpisode("test", "url image", "test description"));

my error is at this line :

    nameOfEpisode.setText(n);

Can you guys help me ? Maybe it's because of my inflater ?


Solution

  • I think you need to call findViewById on the results of inflater.inflate (notice the v.):

    public View anEpisode(String n, String i, String d){
        View v;
        LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.episodedescription, null);
    
        TextView nameOfEpisode = (TextView) v.findViewById(R.id.episodedescriptionname);
        ImageView imageOfEpisode = (ImageView) v.findViewById(R.id.episodedescriptionimage);
        TextView descriptionOfEpisode = (TextView) v.findViewById(R.id.episodedescriptiondescription);
    
        nameOfEpisode.setText(n);
        descriptionOfEpisode.setText(d);
        createUrlImage(imageOfEpisode, i);
    
        return v;
    }
    
    
    flipper.addView(anEpisode("test", "url image", "test description"));