使用Sherlock ListFragment的自定义适配器

时间:2013-02-15 15:53:10

标签: android android-listfragment

我正在尝试使用SherlockFragmentActivity,SherlockListFragment和CustomAdapter(扩展ArraAdapter )为SherlockListFragment实现自定义行我有3个主要布局文件

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- This FrameLayout will be the PlaceHolder of the Fragments. -->
    <FrameLayout android:id="@+id/fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingLeft="8dp"
         android:paddingRight="8dp">
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView android:id="@android:id/empty"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#FF0000"
              android:text="No data"/>
</LinearLayout>

最后是我的行

list_row_layout.xml

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

    <Button
        android:id="@+id/list_row_button_fav"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:padding="8dp"
        android:src="@drawable/fav_star_icon">
    </Button>
    <TextView
        android:id="@+id/list_row_text_dist_value"
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:text=".2"/>
</LinearLayout>

我在getView方法的自定义适配器中遇到NullPointerException,我不确定为什么会这样?我的getView伪代码如下..

private Context mContext;
    private LayoutInflater mInflater;
    private final List<Model> myData;

    public MyListAdapter(Context context, int textViewResourceId, List<Model> data){
        super(context, R.layout.list_layout, data);
        mContext = context;
        this.myData = data;
    }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;

            if(view == null){
                mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = mInflater.inflate(R.layout.list_row_layout, parent, false);
            }
                // find button and text views by Id and set them accordingly, yadda, yadda...
               return view;
        }

堆栈跟踪:

02-15 10:58:36.384: E/AndroidRuntime(16734): FATAL EXCEPTION: main
02-15 10:58:36.384: E/AndroidRuntime(16734): java.lang.NullPointerException
02-15 10:58:36.384: E/AndroidRuntime(16734):    at com.app.test.MyListAdapter.getView(MyListAdapter.java:41)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.AbsListView.obtainView(AbsListView.java:2159)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.ListView.onMeasure(ListView.java:1158)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.View.measure(View.java:15513)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.View.measure(View.java:15513)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.View.measure(View.java:15513)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.View.measure(View.java:15513)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.View.measure(View.java:15513)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.View.measure(View.java:15513)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.View.measure(View.java:15513)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.View.measure(View.java:15513)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.Choreographer.doFrame(Choreographer.java:532)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.os.Handler.handleCallback(Handler.java:725)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.os.Looper.loop(Looper.java:137)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at java.lang.reflect.Method.invokeNative(Native Method)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at java.lang.reflect.Method.invoke(Method.java:511)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-15 10:58:36.384: E/AndroidRuntime(16734):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

如果你在getView()中得到一个NullPointerException,那么这一行看起来就像罪魁祸首:

LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

我猜你忘了初始化mContext ...而且你只需要一个LayoutInflater就可以在你的Adapter的构造函数中创建mInflater