ListViewAnimations库中的ExpandableListItemAdapter给出了错误

时间:2014-09-21 22:36:21

标签: android listview android-library getview

我在尝试使用ExpadableListItemAdapter时遇到了麻烦。根据这个link我需要做的就是覆盖适配器的getTitleView和getContentView。所以我用这个适配器写了一个测试应用程序:

public class ListItemAdapter extends ExpandableListItemAdapter<String> {

    Context mContext;
    List mList;

    public ListItemAdapter(Context context, List<String> list){
        super(context, R.layout.activity_main, R.id.top, R.id.bottom, list);
        mContext = context;
        mList = list;
    }

    @Override
    public View getTitleView(int position, View convertView, ViewGroup parent){
        TextView textView;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
        }
        RelativeLayout rel = (RelativeLayout) convertView.findViewById(R.id.top);
        textView = (TextView)rel.getChildAt(0);
        textView.setText("click me!" + mList.get(position));

        return textView;
    }

    @Override
    public View getContentView(int position, View convertView, ViewGroup parent){
        TextView textView;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
        }
        RelativeLayout rel = (RelativeLayout) convertView.findViewById(R.id.bottom);
        textView = (TextView)rel.getChildAt(1);
        textView.setText("what up!" + mList.get(position));

        return textView;
    }
}

该文档还说我应该“创建一个ExpandableListItemAdapter的新实例,在其上调用setAbsListView,并将其设置为ListView”,但此适配器没有setAbsListView方法。所以我尝试在AlphaInAnimationAdapter上设置它,就像我在其他例子中看到的那样:

public class MainActivity extends Activity {
    @InjectView(android.R.id.list)ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        List<String> list = new ArrayList<String>();
        list.add("test 1");
        list.add("test 2");

        ListItemAdapter adapter = new ListItemAdapter(this, list);
        AlphaInAnimationAdapter alpha = new AlphaInAnimationAdapter(adapter);
        alpha.setAbsListView(mListView);
        mListView.setAdapter(adapter);

    }

但是当我运行应用程序时,我仍然会收到NullPointerException错误:

09-21 18:18:23.392  16952-16952/ward.rydell.expandablelistexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ward.rydell.expandablelistexample, PID: 16952
    java.lang.NullPointerException
            at com.nhaarman.listviewanimations.itemmanipulation.expandablelistitem.ExpandableListItemAdapter.getView(ExpandableListItemAdapter.java:173)
            at android.widget.AbsListView.obtainView(AbsListView.java:2765)
            at android.widget.ListView.makeAndAddView(ListView.java:1801)
            at android.widget.ListView.fillDown(ListView.java:697)
            at android.widget.ListView.fillFromTop(ListView.java:763)
            at android.widget.ListView.layoutChildren(ListView.java:1641)
            at android.widget.AbsListView.onLayout(AbsListView.java:2578)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:459)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15745)
            at android.view.ViewGroup.layout(ViewGroup.java:4868)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2333)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2046)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6585)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
            at android.view.Choreographer.doCallbacks(Choreographer.java:603)
            at android.view.Choreographer.doFrame(Choreographer.java:573)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5579)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)

我做错了什么。我是否需要添加一个getView方法,即使没有指定?如果是这样,应该如何实施?

1 个答案:

答案 0 :(得分:2)

我相信你应该替换

return textView;

return convertView;

我认为您误解了文档中的示例,其中convertViewtextView是相同的。但在您的情况下,textViewconvertView的孩子,但您仍应返回父视图。

更新:,此处

super(context, R.layout.activity_main, R.id.top, R.id.bottom, list);

您应该将ListView项的布局传递给ExpandableListItemAdapter的构造函数。 R.layout.activity_main真的是你想要的吗?