robermorales
robermorales

Reputation: 3553

Android: Application has stopped unexpectedly (What is the best way to further investigate?)

My test application contains a ListActivity that populate the content from Contacts. Error from LogCat does not explain the source of the exception since there are no project files involved.

It stops unexpectedly if running on an ADV or on a device (two different phones tested).

IMPORTANT NOTE: Error is thrown exactly WHEN the user scrolls the list to see more items: when two or three more items are shown. (Always crashes at the same point on the list, and both: scrolling with a finger or with a trackball).

What is the best way to investigate further?

10-13 13:21:00.662: E/AndroidRuntime(8031): FATAL EXCEPTION: main
10-13 13:21:00.662: E/AndroidRuntime(8031): java.lang.NullPointerException
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.widget.AbsListView.obtainView(AbsListView.java:1304)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.widget.ListView.fillDown(ListView.java:652)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.widget.ListView.fillGap(ListView.java:623)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2485)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.os.Handler.handleCallback(Handler.java:587)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.os.Looper.loop(Looper.java:123)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at java.lang.reflect.Method.invoke(Method.java:521)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-13 13:21:00.662: E/AndroidRuntime(8031):     at dalvik.system.NativeStart.main(Native Method)

UPDATE: The code of the adapter:

package com.stripedbee.warayu;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ContactAdapter extends ArrayAdapter<Contact> {

    private final List<Contact> _contacts;
    private final Activity _context;

    public ContactAdapter(Activity context, List<Contact> contacts) {
        super(context, R.layout.contact_list_item, contacts);
        this._contacts = contacts;
        this._context = context;
    }

    static class ViewHolder {
        protected TextView display_name;
        protected TextView number;
        protected TextView number_type;
        private Contact contact;

        protected void setContact(Contact contact) {
            this.display_name.setText(contact.get_display_name());
            this.number.setText(contact.get_number());
            this.number_type.setText(contact.get_number_type());
            this.contact = contact;
        }

        protected Contact getContact() {
            return contact;
        }
    }

    @Override
    public Contact getItem(int position) {
        return this._contacts.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflater = this._context.getLayoutInflater();
            view = inflater.inflate(R.layout.contact_list_item, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.display_name = (TextView) view
                    .findViewById(R.id.txtDisplayName);
            viewHolder.number = (TextView) view.findViewById(R.id.txtNumber);
            viewHolder.number_type = (TextView) view
                    .findViewById(R.id.txtNumberType);
            viewHolder.setContact(_contacts.get(position));
            view.setTag(viewHolder);
        }

        return view;
    }
}

Upvotes: 1

Views: 474

Answers (1)

user
user

Reputation: 87064

You return a null view from your getView method when the convertView is not null(when the user starts scrolling for example). Your getView method should be like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater inflater = this._context.getLayoutInflater();
        view = inflater.inflate(R.layout.contact_list_item, parent, false);
        holder = new ViewHolder();
        viewHolder.display_name = (TextView) view
                .findViewById(R.id.txtDisplayName);
        viewHolder.number = (TextView) view.findViewById(R.id.txtNumber);
        viewHolder.number_type = (TextView) view
                .findViewById(R.id.txtNumberType);            
        view.setTag(viewHolder);
    } else {
        view = convertView;
        holder = (ViewHolder) view.getTag();
    }        
    holder.setContact(_contacts.get(position));
    return view;
}

Upvotes: 2

Related Questions