滚动时Android ListView崩溃

时间:2012-04-12 07:17:48

标签: android android-listview

我在这里看到一个奇怪的错误..我有一个列表视图,它是从异步中的服务器填充的...它首先填充然后显示列表视图...但是当我向下滚动它崩溃...从日志中我看到它与列表视图的人口和所有......无关......你能帮我吗?

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)

这是扩展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;
}

这就是我打电话的方式

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

这是我的列表视图

<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>

2 个答案:

答案 0 :(得分:8)

您应该使用ViewHolder模式。但这不是你看到空指针的原因。 您需要始终为rowLayout分配一个值。

@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 :(得分:1)

  1. 在定义自定义适配器
  2. 时,您没有遵循 ViewHolder 模式
  3. 从以下代码中删除评论:
  4.   // else
     // convertView.getTag();