在getView()里面的LayoutInflater中获取null

时间:2013-10-14 17:43:40

标签: android android-layout

我正在尝试创建ListFragment。 列表中的每一行都包含图像和文本。很像this。 我正在遵循使用ArrayAdapter实现视图的方法。

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

代码:

ArrayAdapter类:

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;
    }
}

ListFragment:

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();
    }

}

mylayout:

<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>

my_row:

<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>

2 个答案:

答案 0 :(得分: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);

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

编辑:

另外我建议您使用视图持有者模式

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

答案 1 :(得分: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;