快速滚动时CustomListView崩溃

时间:2014-06-29 23:49:44

标签: android listview android-listview android-custom-view

我正在尝试实现自定义列表视图。它显示很好,当我慢慢滚动它时它正常工作。但是,当我开始更快地滚动时,应用程序崩溃了。下面是我的列表视图适配器的代码。

import java.util.List;

import MainActivity.ListViewItem;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class CustomListViewAdapter extends BaseAdapter {

LayoutInflater inflater;
List<ListViewItem> items;

public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
    super();

    this.items = items;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return items.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ListViewItem item = items.get(position);

    View vi = convertView;

    if (item.Type.equals("Homework")) {

        if (convertView == null)
            vi = inflater.inflate(R.layout.item_row_homework, null);

        ImageView imgThumbnail = (ImageView) vi
                .findViewById(R.id.homework_imgThumbnail);
        TextView txtTitle = (TextView) vi
                .findViewById(R.id.homework_txtTitle);
        TextView txtSubTitle = (TextView) vi
                .findViewById(R.id.homework_txtSubTitle);

        imgThumbnail.setImageResource(item.ThumbnailResource);
        txtTitle.setText(item.Title);
        txtSubTitle.setText(item.SubTitle);
    }
    if (item.Type.equals("Study")) {

        if (convertView == null)
            vi = inflater.inflate(R.layout.item_row_study, null);

        ImageView imgThumbnail = (ImageView) vi
                .findViewById(R.id.study_imgThumbnail);
        TextView txtTitle = (TextView) vi
                .findViewById(R.id.study_txtTitle);
        TextView txtSubTitle = (TextView) vi
                .findViewById(R.id.study_txtSubTitle);

        imgThumbnail.setImageResource(item.ThumbnailResource);
        txtTitle.setText(item.Title);
        txtSubTitle.setText(item.SubTitle);
    }
    if (item.Type.equals("Project")) {

        if (convertView == null)
            vi = inflater.inflate(R.layout.item_row_project, null);

        ImageView imgThumbnail = (ImageView) vi
                .findViewById(R.id.project_imgThumbnail);
        TextView txtTitle = (TextView) vi
                .findViewById(R.id.project_txtTitle);
        TextView txtSubTitle = (TextView) vi
                .findViewById(R.id.project_txtSubTitle);

        imgThumbnail.setImageResource(item.ThumbnailResource);
        txtTitle.setText(item.Title);
        txtSubTitle.setText(item.SubTitle);
    }

    return vi;
}
}

这是logcat输出。

06-29 19:21:59.543: E/AndroidRuntime(2507): FATAL EXCEPTION: main
06-29 19:21:59.543: E/AndroidRuntime(2507): Process: com.example.customlistview, PID: 2507
06-29 19:21:59.543: E/AndroidRuntime(2507): java.lang.NullPointerException
06-29 19:21:59.543: E/AndroidRuntime(2507):     at com.example.customlistview.CustomListViewAdapter.getView(CustomListViewAdapter.java:68)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.widget.AbsListView.obtainView(AbsListView.java:2240)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.widget.ListView.fillDown(ListView.java:691)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.widget.ListView.fillGap(ListView.java:655)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5136)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3236)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.widget.AbsListView.onTouchMove(AbsListView.java:3580)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3424)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.View.dispatchTouchEvent(View.java:7706)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.View.dispatchPointerEvent(View.java:7886)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5655)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5701)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.Choreographer.doFrame(Choreographer.java:542)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.os.Handler.handleCallback(Handler.java:733)
06-29 19:21:59.543: E/AndroidRuntime(2507):     at android.os.Handler.dispatchMessage(Handler.java:9

1 个答案:

答案 0 :(得分:5)

您需要为getViewTypeCount()正确实施getItemViewType()ListAdapter

getViewTypeCount()需要返回不同行类型的数量(在您的情况下,它似乎是3)。

然后

getItemViewType()需要返回一个数字,从0到getViewTypeCount()-1,表示哪个视图类型对应于提供的position。在您的情况下,您将返回0,1或2,具体取决于这是HomeworkStudy还是Project行。

如果您正确实施,convertView传入getView()的行将是正确的类型。现在,您假设 convertView属于正确的类型,如果不是,则findViewById()查找将失败。