在Horizo​​ntalListView中滚动的问题(android)

时间:2014-12-27 23:38:58

标签: android listview android-listview scroll

我使用HorizontalListView滚动我使用下面的代码(方法setOnItemClickListener)

 listview.post(new Runnable() {
                public void run() {
                    int centerX = listview.getChildAt(position).getLeft();

                    listview.scrollTo(centerX);
                }
            });

但是当位置达到3或更高时会出现问题,然后我收到错误:

12-28 01:33:37.814  20841-20841/com.example.SmsService E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.SmsService, PID: 20841
java.lang.NullPointerException
        at com.example.SmsService.VideoView$1$1.run(VideoView.java:68)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5118)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
        at dalvik.system.NativeStart.main(Native Method)

我想这可能是因为我使用循环listView

  @Override
    public int getCount() {
        return Integer.MAX_VALUE;//
    }

我怎么能解决这个问题?也许还有其他方法滚动listView? (滚动平滑不起作用)My Class.和.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_weight="1" android:id="@+id/ll">
    <VideoView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/videoView"
               android:layout_gravity="center_horizontal"
               android:layout_alignParentTop="true"
               android:layout_alignParentRight="true"
               android:layout_alignParentBottom="true"
            />
    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="150dp" android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true" android:layout_marginBottom="20dp">
        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <com.devsmart.android.ui.HorizontalListView
                    android:id="@+id/listview2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="true" android:layout_weight="1"/>
            <com.devsmart.android.ui.HorizontalListView
                    android:id="@+id/listview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:smoothScrollbar="true"
                    android:clickable="true" android:layout_alignParentRight="true"            android:layout_weight="1"/>
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

更改此行

public void run() {
    int centerX = listview.getChildAt(position).getLeft();
    listview.scrollTo(centerX)
}

public void run() {
    View v = listView.getChildAt(position);
    if(v != null)
    {
        int centerX = v.getLeft();
        listview.scrollTo(centerX);
    }
}

您可能需要找到另一种移动项目的方法。此代码仅修复空指针异常。