我无法将视图添加到我的viewflipper

时间:2011-09-09 21:31:58

标签: java android view viewflipper

这是我的代码:

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

我的错误就在这一行:

    nameOfEpisode.setText(n);
你能帮助我吗?也许是因为我的inflater?

1 个答案:

答案 0 :(得分:2)

我认为你需要在inflater.inflate的结果上调用findViewById(注意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"));