简体   繁体   中英

Android ListView crashes while scrolling

i see a weird bug here.. i have a list view which is populated from the server in an async... it populates first and then displays the list view... but when i scroll down it crashes... from the logs i see it is not related to population of list view and all.. ... can you please help me?

04-12 12:39:54.676: E/AndroidRuntime(1984): FATAL EXCEPTION: main
04-12 12:39:54.676: E/AndroidRuntime(1984): java.lang.NullPointerException
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.widget.AbsListView.obtainView(AbsListView.java:1304)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.widget.ListView.fillUp(ListView.java:682)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.widget.ListView.correctTooHigh(ListView.java:1349)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.widget.ListView.fillGap(ListView.java:624)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:2065)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.widget.ListView.onTouchEvent(ListView.java:3315)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.view.View.dispatchTouchEvent(View.java:3766)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.os.Looper.loop(Looper.java:123)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at java.lang.reflect.Method.invokeNative(Native Method)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at java.lang.reflect.Method.invoke(Method.java:521)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
04-12 12:39:54.676: E/AndroidRuntime(1984):     at dalvik.system.NativeStart.main(Native Method)

this is the custom adapter class which extends Base Adapter

@Override
public int getCount() {

    return mAppIconMapList.size();
}

@Override
public Object getItem(int position) {

    return mAppIconMapList.get(position);
}

public Object getAppName(int position) {

    return mAvaiableApps.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // sets the view onto list view
    LinearLayout rowLayout = null;

    System.out.println("getView " + position + " " + convertView);

    if (convertView == null) {
        // inflating the row
        rowLayout = (LinearLayout) mInflater.inflate(
                R.layout.list_custom_row, parent, false);

        mAppIcon = (ImageView) rowLayout.findViewById(R.id.icon);
        mAppName = (TextView) rowLayout.findViewById(R.id.application);
        mAppName.setTextColor(Color.WHITE);
        mAppHint = (TextView) rowLayout.findViewById(R.id.hint);
        mAppHint.setTextColor(Color.WHITE);
        mDownloadButton = (Button) rowLayout.findViewById(R.id.download);
        mDownloadButton.setFocusable(false);

    }
    // else
    // convertView.getTag();


    mIcon = mAppIconMapList.get(position);
    System.out.println("Icon " + mIcon);
    mCurrentApplication = mAvaiableApps.get(position);
    System.out.println("Current App " + mCurrentApplication);
    mAppIcon.setImageBitmap(mIcon.get(mCurrentApplication));
    mAppName.setText(mCurrentApplication.replace(".apk", ""));
    mAppHint.setText("Click here to view Description");

    return rowLayout;
}

And this is how i am calling

mCustomAdapter = new BWCustomAdapter(mCategory, avaiableApps,
                            appIconMap, BWListApplication.this);
                    setListAdapter(mCustomAdapter);

and this is my list view

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="450dip"
    android:layout_height="300dip"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fadingEdge="none"
        android:listSelector="@drawable/list_selector" />
</LinearLayout>

You should really use the ViewHolder pattern. But that is not reason you see a null pointer. You need to assign a value to rowLayout at all times.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // sets the view onto list view
    LinearLayout rowLayout = null;

    System.out.println("getView " + position + " " + convertView);

    if (convertView == null) {
        // inflating the row
        rowLayout = (LinearLayout) mInflater.inflate(
                R.layout.list_custom_row, parent, false);


    } else {
        rowLayout = convertView;
    }
    mAppIcon = (ImageView) rowLayout.findViewById(R.id.icon);
    mAppName = (TextView) rowLayout.findViewById(R.id.application);
    mAppName.setTextColor(Color.WHITE);
    mAppHint = (TextView) rowLayout.findViewById(R.id.hint);
    mAppHint.setTextColor(Color.WHITE);
    mDownloadButton = (Button) rowLayout.findViewById(R.id.download);
    mDownloadButton.setFocusable(false);


    mIcon = mAppIconMapList.get(position);
    System.out.println("Icon " + mIcon);
    mCurrentApplication = mAvaiableApps.get(position);
    System.out.println("Current App " + mCurrentApplication);
    mAppIcon.setImageBitmap(mIcon.get(mCurrentApplication));
    mAppName.setText(mCurrentApplication.replace(".apk", ""));
    mAppHint.setText("Click here to view Description");

    return rowLayout;
}
  1. You haven't followed the ViewHolder pattern while defining custom adapter
  2. Remove the comment from the below code:
  // else // convertView.getTag(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM