如果向下滚动列表,则在使用ViewHolder时应用程序崩溃

时间:2013-10-13 22:55:42

标签: android listview crash scroll baseadapter

我正在使用BaseAdapter来显示自定义的ListView项。这段代码工作正常:

public class BA_OneNoneHandler extends BaseAdapter {
private Context coContext;
private Cursor cuCursor;

public BA_OneNoneHandler(Context context, Cursor cur) {
    super();
    coContext = context;
    cuCursor = cur;
}

public View getView(int iPosition, View view, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) coContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.termin_list_item, null);

    cuCursor.moveToPosition(iPosition);

    ((TextView) view.findViewById(R.id.TerminListItem_tvHeadline)).setText("Hallo");

    return view;
}

public int getCount() {
    // return the number of records in cursor
    return cuCursor.getCount();
}

public Object getItem(int iPosition) {
    // TODO Auto-generated method stub
    return iPosition;
}

public long getItemId(int iPosition) {
    // TODO Auto-generated method stub
    return iPosition;
}

}

...但是如果我想使用ViewHolder,那么如果ListView向下滚动,应用程序就会崩溃。

public class BA_OneHandler extends BaseAdapter {
private Context coContext;
private Cursor cuCursor;

public BA_OneHandler(Context context, Cursor cur) {
    super();
    coContext = context;
    cuCursor = cur;
}

public View getView(int iPosition, View view, ViewGroup parent) {

    ViewHolder vhViewHolder;

    if( view == null){
        vhViewHolder = new ViewHolder();

        LayoutInflater inflater = (LayoutInflater) coContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.termin_list_item, null);

        // XML-element
        vhViewHolder.tvHeadLine = (TextView) view.findViewById(R.id.TerminListItem_tvHeadline);
    } else {
        vhViewHolder = (ViewHolder)view.getTag();
    }

    cuCursor.moveToPosition(iPosition);

    vhViewHolder.tvHeadLine.setText("Hallo");

    return view;
}

private static class ViewHolder{
    // XML-elements from "termin_list_adapter.xml"
    protected TextView tvHeadLine;
}

public int getCount() {
    // return the number of records in cursor
    return cuCursor.getCount();
}

public Object getItem(int iPosition) {
    // TODO Auto-generated method stub
    return iPosition;
}

public long getItemId(int iPosition) {
    // TODO Auto-generated method stub
    return iPosition;
}

}

我找到了类似的Questios,但他们都在处理JSON。我只是一个简单的清单。 这是堆栈......

    java.lang.NullPointerException
        at com.progstar.android.contermine.BA_OneHandler.getView(BA_OneHandler.java:51)
        at android.widget.AbsListView.obtainView(AbsListView.java:2143)
        at android.widget.ListView.makeAndAddView(ListView.java:1831)
        at android.widget.ListView.fillDown(ListView.java:674)
        at android.widget.ListView.fillGap(ListView.java:638)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
        at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3087)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:3361)
        at android.view.View.dispatchTouchEvent(View.java:7239)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
        at android.view.View.dispatchPointerEvent(View.java:7419)
        at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:530)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5226)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
        at dalvik.system.NativeStart.main(Native Method)

第51行) - >

vhViewHolder.tvHeadLine.setText("Hallo");

我正在使用BaseAdapter,因为我是android的新手,看过数百个适配器并按照其中一个,这似乎以最佳方式显示我的自定义列表。 我甚至搜索了很多“适用于哪个应用程序的适配器”a.s.o.,但没有令人满意的概述。所以我从代码示例中尝试了一个,而不是用太多的初学者问题打扰社区。选择BaseAdapter不是一个好主意吗?

另一方面,SimpeCursorAdapter是> API 11(我也想支持Android 2.3)和CursorAdapter是抽象的,所以我不得不蠢蠢欲动。

1 个答案:

答案 0 :(得分:1)

发现......我忘记了

view.setTag(vhViewHolder);

创建视图后。这是完整运行的getView方法:

public View getView(int iPosition, View view, ViewGroup parent) {

    ViewHolder vhViewHolder;

    if( view == null){

        LayoutInflater inflater = (LayoutInflater) coContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.termin_list_item, null);

        // XML-element
        vhViewHolder = new ViewHolder();
        vhViewHolder.tvHeadLine = (TextView) view.findViewById(R.id.TerminListItem_tvHeadline);

        view.setTag(vhViewHolder);
    } else {
        vhViewHolder = (ViewHolder)view.getTag();
    }

    cuCursor.moveToPosition(iPosition);

    vhViewHolder.tvHeadLine.setText("Hallo");

    return view;
}