0

我开始使用片段来开发一个新的应用程序,其中一个片段是 a ListFragment,当我使用我制作的适配器来显示列表的行时,我nullfindViewById我在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;
    }
}

如果有人问,我正在尝试检索相关布局以在之后动态添加一些控件。

谢谢

4

2 回答 2

4

您发布了 XML ,detail_list_item.xml但在您的代码中膨胀了menu_list_item.xml

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
}
于 2013-07-02T14:56:32.590 回答
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...我以为您遇到了同样的问题...

@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);
于 2013-07-02T14:58:19.567 回答