0

我正在尝试创建一个ListFragment. 列表中的每一行都包含一个图像和文本。很像这样。我正在遵循使用ArrayAdapter.

但是我在ArrayAdapter 的 getView() 中得到NullPointerException @LayoutInflater inflater = fragmentContext.getLayoutInflater(null);

代码 :

数组适配器类:

public class MyListAdapter extends ArrayAdapter {

    Context context;
    String[] items;
    Fragment fragmentContext;

    public MyListAdapter(Context c, int textViewResourceId, String[] listItmes,
            Fragment fragmentContext) {
        super(c, textViewResourceId, listItmes);
            this.items = listItmes;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = fragmentContext.getLayoutInflater(null);
            view.inflate(context, R.layout.my_row, null);
        }
        if (items != null) {
            TextView tv_my_list_item_title = (TextView) view
                    .findViewById(R.id.tv_my_list_item_title);
            if (tv_my_list_item_title != null) {
                tv_my_list_item_title.setText(items[position]);
            }
        }
        return view;
    }
}

列表片段:

public class MyFragment extends ListFragment {

    int mCurCheckPosition = 0;
    String[] listItems = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mylayout, null);
        listItems = getResources().getStringArray(R.array.my_element_list);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Populate list with our static array of titles.
        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        //      android.R.layout.simple_list_item_1, listItems);
        MyListAdapter adapter = new MyListAdapter(getActivity(), R.layout.my_row, listItems, this);

        setListAdapter(adapter);

        if (savedInstanceState != null) { // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetails(position);
    }

    private void showDetails(int position) {
        mCurCheckPosition = position;
        Toast.makeText(getActivity(), listItems[position], Toast.LENGTH_SHORT).show();
    }

}

我的布局:

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

    <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

我的行:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:orientation="vertical"
    android:padding="6dp" >

    <ImageView
        android:id="@+id/iv_mehr_list_item_icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_mehr_list_item_title"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center_vertical" />

</LinearLayout>
4

2 回答 2

10

使用以下

LayoutInflater inflater ;
public MyListAdapter(Context c, int textViewResourceId, String[] listItmes,
            Fragment fragmentContext) {
            super(c, textViewResourceId, listItmes);
            this.items = listItmes;
            inflater = LayoutInfalter.from(c);
        }

并且在getView()使用中

  view = inflater.inflate(R.layout.my_row,parent, false);

无需初始化 inflater getView。您需要将膨胀视图初始化为view您所拥有的View view = convertView,然后使用膨胀视图对象来初始化您的文本视图。

编辑:

另外我建议您使用视图支架模式

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

于 2013-10-14T17:48:18.563 回答
2
    Context context;
    String[] items;
    Fragment fragmentContext;

public MyListAdapter(Context c, int textViewResourceId, String[] listItmes,
            Fragment fragmentContext) {
        super(c, textViewResourceId, listItmes);
            this.items = listItmes;
        }
    }

您没有分配传递给fragmentContext构造函数内部的上下文..所以它仍然为空

在构造函数中添加

        this.fragmentContext = fragmentContext;
于 2013-10-14T17:47:34.233 回答