滚动ListView期间的致命异常

时间:2013-07-31 14:31:17

标签: android android-listview

应用程序运行正常,直到您在ListView中滚动几次。

这是我从LOGCAT回来的错误。我使用的自定义BaseAdapter的代码位于它下面。

07-31 10:08:43.550: E/AndroidRuntime(32570): FATAL EXCEPTION: main
07-31 10:08:43.550: E/AndroidRuntime(32570): java.lang.NullPointerException
07-31 10:08:43.550: E/AndroidRuntime(32570):    at com.duobility.leathr.HomeScreen$TimeLineAdapter.getView(HomeScreen.java:485)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at com.haarman.listviewanimations.BaseAdapterDecorator.getView(BaseAdapterDecorator.java:87)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at com.haarman.listviewanimations.swinginadapters.AnimationAdapter.getView(AnimationAdapter.java:94)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.widget.AbsListView.obtainView(AbsListView.java:2452)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.widget.ListView.makeAndAddView(ListView.java:1775)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.widget.ListView.fillDown(ListView.java:678)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.widget.ListView.fillGap(ListView.java:642)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5709)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3420)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:4085)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.View.dispatchTouchEvent(View.java:7341)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2460)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2195)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2466)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2466)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2466)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2466)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2115)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1468)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2063)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.View.dispatchPointerEvent(View.java:7526)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3505)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3437)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4541)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4519)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4623)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4591)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4642)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.Choreographer.doFrame(Choreographer.java:523)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.os.Handler.handleCallback(Handler.java:615)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.os.Looper.loop(Looper.java:137)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at android.app.ActivityThread.main(ActivityThread.java:4898)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at java.lang.reflect.Method.invoke(Method.java:511)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-31 10:08:43.550: E/AndroidRuntime(32570):    at dalvik.system.NativeStart.main(Native Method)

这是Custom BaseAdatper

私有类TimeLineAdapter扩展了BaseAdapter {

TimeStampHandler timeStampHandler = new TimeStampHandler();
ImageOptions iconImageOptions, photoImageOptions = new ImageOptions();

long currentUnixTime = (System.currentTimeMillis())/1000;
String currentMonthYear = timeStampHandler.get_MMYYYY(currentUnixTime);
ArrayList<TimeLineObject> monthArray = getMonthEntry(currentMonthYear);

@Override
public int getCount() {
    return monthArray.size();
}

@Override
public Object getItem(int position) {
    return monthArray.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    TimeLineViewHolder holder;
    photoImageOptions.round = 5;
    photoImageOptions.fileCache = true;
    photoImageOptions.memCache = true;

    if (convertView == null) {
        holder = new TimeLineViewHolder();
        // Find which type of card entry it is /////
        if ( (monthArray.get(position).type).equals(AppConstants.TYPE_PICTURE) ) {

            Log.d(AppConstants.HOME_SCREEN, "thumbnailArraySize: " + monthArray.get(position).photoThumbnailArrayList.size());

            if (monthArray.get(position).photoThumbnailArrayList.size() == 1) {
                convertView = getLayoutInflater().inflate(R.layout.photo_card_1, parent, false);
                holder.photo1 = (ImageView) convertView.findViewById(R.id.photo1);

            } else if (monthArray.get(position).photoThumbnailArrayList.size() == 2) {
                convertView = getLayoutInflater().inflate(R.layout.photo_card_2, parent, false);
                holder.photo1 = (ImageView) convertView.findViewById(R.id.photo1);
                holder.photo2 = (ImageView) convertView.findViewById(R.id.photo2);

            } else if (monthArray.get(position).photoThumbnailArrayList.size() == 3) {
                convertView = getLayoutInflater().inflate(R.layout.photo_card_3, parent, false);
                holder.photo1 = (ImageView) convertView.findViewById(R.id.photo1);
                holder.photo2 = (ImageView) convertView.findViewById(R.id.photo2);
                holder.photo3 = (ImageView) convertView.findViewById(R.id.photo3);
                holder.photoText = (TextView) convertView.findViewById(R.id.photoText);

            }

            holder.photoDate = (TextView) convertView.findViewById(R.id.photoDate);
            convertView.setTag(holder);

        } else if ( (monthArray.get(position).type).equals(AppConstants.TYPE_TEXT) ) {

            convertView = getLayoutInflater().inflate(R.layout.message_card, parent, false);
            holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
            holder.messageContent = (TextView) convertView.findViewById(R.id.messageContent);
            holder.messageDate = (TextView) convertView.findViewById(R.id.messageDate);
            convertView.setTag(holder);

        } else if ( (monthArray.get(position).type).equals(AppConstants.TYPE_LINK) ) {

            convertView = getLayoutInflater().inflate(R.layout.link_card, parent, false);
            holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
            holder.linkContent = (TextView) convertView.findViewById(R.id.linkContent);
            holder.linkDate = (TextView) convertView.findViewById(R.id.linkDate);
            convertView.setTag(holder);

        } else if ( (monthArray.get(position).type).equals(AppConstants.TYPE_LOCATION) ) {

            convertView = getLayoutInflater().inflate(R.layout.location_card, parent, false);
            holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
            holder.locationContent = (TextView) convertView.findViewById(R.id.locationContent);
            holder.locationDate = (TextView) convertView.findViewById(R.id.locationDate);
            convertView.setTag(holder);

        }
        // End of card type ////////////////////////
    } else {
        holder = (TimeLineViewHolder) convertView.getTag();
    }

    if ( (monthArray.get(position).type.equals(AppConstants.TYPE_PICTURE)) ) {

        // TODO Deal with the photo albums
        long unixTime = Long.parseLong( monthArray.get(position).unixtime );
        int cardWidth = (displayMetrics.widthPixels)-32;

        if (monthArray.get(position).photoThumbnailArrayList.size() == 1) {
            String photoURL = monthArray.get(position).thumbnail.replace("s144/", "w" + cardWidth + "/");
            aQuery.id(holder.photo1).image(photoURL, photoImageOptions);

        } else if (monthArray.get(position).photoThumbnailArrayList.size() == 2) {
            String photoURL1 = monthArray.get(position).photoThumbnailArrayList.get(0).replace("s144/", "w" + (cardWidth/2) + "/");
            String photoURL2 = monthArray.get(position).photoThumbnailArrayList.get(1).replace("s144/", "w" + (cardWidth/2) + "/");
            aQuery.id(holder.photo1).image(photoURL1, photoImageOptions);
            aQuery.id(holder.photo2).image(photoURL2, photoImageOptions);

        } else if (monthArray.get(position).photoThumbnailArrayList.size() == 3) {
            String photoURL1 = monthArray.get(position).photoThumbnailArrayList.get(0).replace("s144/", "w" + (cardWidth/3) + "/");
            String photoURL2 = monthArray.get(position).photoThumbnailArrayList.get(1).replace("s144/", "w" + (cardWidth/3) + "/");
            String photoURL3 = monthArray.get(position).photoThumbnailArrayList.get(2).replace("s144/", "w" + (cardWidth/3) + "/");
            aQuery.id(holder.photo1).image(photoURL1, photoImageOptions);
            aQuery.id(holder.photo2).image(photoURL2, photoImageOptions);
            aQuery.id(holder.photo3).image(photoURL3, photoImageOptions);
            holder.photoText.setText(timeStampHandler.get_dayFullLenght(unixTime));

        }
        holder.photoDate.setText(timeStampHandler.get_dd_MMM(unixTime));
        Log.d(AppConstants.HOME_SCREEN, "photo card was created");

    } else if ( (monthArray.get(position).type.equals(AppConstants.TYPE_TEXT)) ) {

        long unixTime = Long.parseLong( monthArray.get(position).unixtime );
        holder.messageDate.setText(timeStampHandler.get_dd_MMM(unixTime));
        holder.messageContent.setText(monthArray.get(position).content);
        Log.d(AppConstants.HOME_SCREEN, "text card was created");

    } else if ( (monthArray.get(position).type.equals(AppConstants.TYPE_LINK)) ) {

        long unixTime = Long.parseLong( monthArray.get(position).unixtime );
        holder.linkDate.setText(timeStampHandler.get_dd_MMM(unixTime));
        holder.linkContent.setText(monthArray.get(position).content);
        Log.d(AppConstants.HOME_SCREEN, "link card was created");

    } else if ( (monthArray.get(position).type.equals(AppConstants.TYPE_LOCATION)) ) {

        long unixTime = Long.parseLong( monthArray.get(position).unixtime );
        holder.locationDate.setText(timeStampHandler.get_dd_MMM(unixTime));
        holder.locationContent.setText(monthArray.get(position).content);
        Log.d(AppConstants.HOME_SCREEN, "location card was created");

    }

    return convertView;
}

}

编辑:485行低于

holder.photoText.setText(timeStampHandler.get_dayFullLenght(unixTime));

2 个答案:

答案 0 :(得分:0)

该例外意味着photoText == null。这要么是因为:

  • monthArray.get(position).photoThumbnailArrayList.size() != 3
  • R.layout.photo_card_3没有TextView,标识为@+id/photoText

检查两种情况!

答案 1 :(得分:0)

我已经通过在任何代码之前在getView()方法中将convertView = null;设置为null来解决了这个问题。

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

    TimeLineViewHolder holder;
    convertView = null;
    photoImageOptions.round = 5;
    photoImageOptions.fileCache = true;
    photoImageOptions.memCache = true;

    if (convertView == null) {
        holder = new TimeLineViewHolder();
        // Find which type of card entry it is /////
        if ( (monthArray.get(position).type).equals(AppConstants.TYPE_PICTURE) ) {

            Log.d(AppConstants.HOME_SCREEN, "thumbnailArraySize: " + monthArray.get(position).photoThumbnailArrayList.size());

            if (monthArray.get(position).photoThumbnailArrayList.size() == 1) {
                convertView = getLayoutInflater().inflate(R.layout.photo_card_1, parent, false);
                holder.photo1 = (ImageView) convertView.findViewById(R.id.photo1);

            } else if (monthArray.get(position).photoThumbnailArrayList.size() == 2) {
                convertView = getLayoutInflater().inflate(R.layout.photo_card_2, parent, false);
                holder.photo1 = (ImageView) convertView.findViewById(R.id.photo1);
                holder.photo2 = (ImageView) convertView.findViewById(R.id.photo2);

            } else if (monthArray.get(position).photoThumbnailArrayList.size() == 3) {
                convertView = getLayoutInflater().inflate(R.layout.photo_card_3, parent, false);
                holder.photo1 = (ImageView) convertView.findViewById(R.id.photo1);
                holder.photo2 = (ImageView) convertView.findViewById(R.id.photo2);
                holder.photo3 = (ImageView) convertView.findViewById(R.id.photo3);
                holder.photoText = (TextView) convertView.findViewById(R.id.photoText);

            }

            holder.photoDate = (TextView) convertView.findViewById(R.id.photoDate);
            convertView.setTag(holder);

        } else if ( (monthArray.get(position).type).equals(AppConstants.TYPE_TEXT) ) {

            convertView = getLayoutInflater().inflate(R.layout.message_card, parent, false);
            holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
            holder.messageContent = (TextView) convertView.findViewById(R.id.messageContent);
            holder.messageDate = (TextView) convertView.findViewById(R.id.messageDate);
            convertView.setTag(holder);

        } else if ( (monthArray.get(position).type).equals(AppConstants.TYPE_LINK) ) {

            convertView = getLayoutInflater().inflate(R.layout.link_card, parent, false);
            holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
            holder.linkContent = (TextView) convertView.findViewById(R.id.linkContent);
            holder.linkDate = (TextView) convertView.findViewById(R.id.linkDate);
            convertView.setTag(holder);

        } else if ( (monthArray.get(position).type).equals(AppConstants.TYPE_LOCATION) ) {

            convertView = getLayoutInflater().inflate(R.layout.location_card, parent, false);
            holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
            holder.locationContent = (TextView) convertView.findViewById(R.id.locationContent);
            holder.locationDate = (TextView) convertView.findViewById(R.id.locationDate);
            convertView.setTag(holder);

        }
        // End of card type ////////////////////////
    } else {
        holder = (TimeLineViewHolder) convertView.getTag();
    }

    if ( (monthArray.get(position).type.equals(AppConstants.TYPE_PICTURE)) ) {

        // TODO Deal with the photo albums
        long unixTime = Long.parseLong( monthArray.get(position).unixtime );
        int cardWidth = (displayMetrics.widthPixels)-32;

        if (monthArray.get(position).photoThumbnailArrayList.size() == 1) {
            String photoURL = monthArray.get(position).thumbnail.replace("s144/", "w" + cardWidth + "/");
            aQuery.id(holder.photo1).image(photoURL, photoImageOptions);

        } else if (monthArray.get(position).photoThumbnailArrayList.size() == 2) {
            String photoURL1 = monthArray.get(position).photoThumbnailArrayList.get(0).replace("s144/", "w" + (cardWidth/2) + "/");
            String photoURL2 = monthArray.get(position).photoThumbnailArrayList.get(1).replace("s144/", "w" + (cardWidth/2) + "/");
            aQuery.id(holder.photo1).image(photoURL1, photoImageOptions);
            aQuery.id(holder.photo2).image(photoURL2, photoImageOptions);

        } else if (monthArray.get(position).photoThumbnailArrayList.size() == 3) {
            String photoURL1 = monthArray.get(position).photoThumbnailArrayList.get(0).replace("s144/", "w" + (cardWidth/3) + "/");
            String photoURL2 = monthArray.get(position).photoThumbnailArrayList.get(1).replace("s144/", "w" + (cardWidth/3) + "/");
            String photoURL3 = monthArray.get(position).photoThumbnailArrayList.get(2).replace("s144/", "w" + (cardWidth/3) + "/");
            aQuery.id(holder.photo1).image(photoURL1, photoImageOptions);
            aQuery.id(holder.photo2).image(photoURL2, photoImageOptions);
            aQuery.id(holder.photo3).image(photoURL3, photoImageOptions);
            holder.photoText.setText(timeStampHandler.get_dayFullLenght(unixTime));

        }
        holder.photoDate.setText(timeStampHandler.get_dd_MMM(unixTime));
        Log.d(AppConstants.HOME_SCREEN, "photo card was created");

    } else if ( (monthArray.get(position).type.equals(AppConstants.TYPE_TEXT)) ) {

        long unixTime = Long.parseLong( monthArray.get(position).unixtime );
        holder.messageDate.setText(timeStampHandler.get_dd_MMM(unixTime));
        holder.messageContent.setText(monthArray.get(position).content);
        Log.d(AppConstants.HOME_SCREEN, "text card was created");

    } else if ( (monthArray.get(position).type.equals(AppConstants.TYPE_LINK)) ) {

        long unixTime = Long.parseLong( monthArray.get(position).unixtime );
        holder.linkDate.setText(timeStampHandler.get_dd_MMM(unixTime));
        holder.linkContent.setText(monthArray.get(position).content);
        Log.d(AppConstants.HOME_SCREEN, "link card was created");

    } else if ( (monthArray.get(position).type.equals(AppConstants.TYPE_LOCATION)) ) {

        long unixTime = Long.parseLong( monthArray.get(position).unixtime );
        holder.locationDate.setText(timeStampHandler.get_dd_MMM(unixTime));
        holder.locationContent.setText(monthArray.get(position).content);
        Log.d(AppConstants.HOME_SCREEN, "location card was created");

    }

    return convertView;
}