findViewByID在BaseAdapter上返回null

时间:2013-07-02 14:53:14

标签: android android-fragments android-adapter

我开始使用片段开发一个新应用,其中一个片段是ListFragment, 当我使用我制作的适配器来显示列表的行时,我从null方法中使用的某些findViewById获得了一些getView()。 结构是:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/fragment_container">
    </FrameLayout>
</RelativeLayout>

frag_detail.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list"
        android:layout_gravity="center"/>
</LinearLayout>

detail_list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/detailItemLayout" >

    <ImageButton
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:id="@+id/imgIcon"
        android:layout_below="@+id/txtTitle"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="txtTitle"
        android:id="@+id/txtTitle"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="label1"
        android:id="@+id/label1"
        android:layout_below="@+id/imgIcon"
        android:layout_alignParentLeft="true"/>
</RelativeLayout>

DetailFragment类

public class DetailFragment extends ListFragment {

    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_detail,container,false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MonsterDataSource ds = new MonsterDataSource(context);
        List<Monster> lsMonster = ds.getAllMonsters();

        //ListView lsView = (ListView)this.getActivity().findViewById(R.id.lsDetail);
        MonsterListAdapter adapter = new MonsterListAdapter(context,lsMonster);
        //lsView.setAdapter(adapter);
        setListAdapter(adapter);

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity.getApplicationContext();
    }
}

MonsterListAdapter类

public class MonsterListAdapter extends BaseAdapter {
    static class ViewHolder {
        private TextView txtTitle;
        private ImageView imgIcon;
        private TextView txtHealth;
    }

    private Context context;
    private LayoutInflater inflater;
    private List<Monster> data;
    private ViewHolder holder;

    public MonsterListAdapter(Context c, List<Monster> data){
        context = c;
        this.data = data;
        inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    /*Basic BaseAdapter Methods*/
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.menu_list_item, null);
            holder = new ViewHolder();



            /*Those Guys get null*/
            RelativeLayout layout = (RelativeLayout)view.findViewById(R.id.detailItemLayout);
            holder.txtTitle = (TextView) view.findViewById(R.id.txtTitle);
            holder.imgIcon = (ImageView) view.findViewById(R.id.imgIcon);



            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }


        Monster monster = data.get(i);

        holder.txtTitle.setText(monster.name);
        holder.imgIcon.setImageResource(monster.imgResID);
        holder.txtHealth.setText(monster.health);

        return view;
    }
}

如果有人问,我试图检索相对布局,以便动态添加一些控件。

由于

2 个答案:

答案 0 :(得分:4)

您发布了detail_list_item.xml的XML,但在您的代码中,您需要menu_list_item.xml。{/ p>

detail_list_item.xml包含您要查找的所有ID,因此您似乎意外地夸大了View中的错误getView

此外,您应该将ViewHolder作为成员变量删除,并将其作为局部变量放在getView方法中。您正在同时为多个视图重用该变量,这意味着您有可能在不同位置修改错误的视图(数据转到错误的位置等)。

public View getView(int i, View view, ViewGroup viewGroup) {
 ViewHolder holder;
 ...
 ...//all of your other code here
}

答案 1 :(得分:0)

尝试不影响“观看”

    RelativeLayout layout = (RelativeLayout)findViewById(R.id.detailItemLayout);
    holder.txtTitle = (TextView) findViewById(R.id.txtTitle);
    holder.imgIcon = (ImageView) findViewById(R.id.imgIcon);

编辑这对我有用...我的texviews nbCommentaires和Comment给NullPointerException如果我向他们充气View ...我认为你得到了同样的问题......

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.comment_row, null);
        }
        v.setTag(position);
        Commentaires o = Global_reco.CommRecos.get(position);
        if (o != null) {    
            TextView nom = (TextView) v.findViewById(R.id.nom_com);
            TextView com = (TextView) v.findViewById(R.id.com);
            ImageView photo = (ImageView) v.findViewById(R.id.profile_com);
            TextView nbCommentaires = (TextView) findViewById(R.id.nb_commentaires);
            TextView Comment = (TextView)findViewById(R.id.nbre_comment);
            photo.setTag(position);