1

在列表视图中创建水平列表视图时出现问题..

myAdapter adapter = new myAdapter(thisActivity, kategoriList,
                        R.layout.list_item,
                        new String[] { "nama","thumbnail"}, new int[] {
                                R.id.name, R.id.thumb_kategori});

                setListAdapter(adapter);

                HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview2);
                listview.setAdapter(mAdapter);

这是布局:我正在使用 Dev-SmartLib 中的 Horitontal Listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">  
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- Name Label -->

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/thumb_kategori"
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:id="@+id/name"
                android:layout_width="238dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:paddingBottom="2dip"
                android:paddingTop="6dip"
                android:textColor="#43bd00"
                android:textSize="16sp"
                android:textStyle="bold" />

        </LinearLayout>
    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >

      <com.devsmart.android.ui.HorizontalListView
          android:id="@+id/listview2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="40dp"
          android:layout_marginLeft="70dp"
          android:layout_marginRight="70dp"
          android:layout_marginTop="50dp"
          android:divider="#FFCC00"
          android:fillViewport="false" >

      </com.devsmart.android.ui.HorizontalListView>

  </RelativeLayout>

    </LinearLayout>

</LinearLayout>

但是,当我尝试这种方法时发生了错误:这是 Log Cat:

12-26 11:09:16.350: E/AndroidRuntime(7102): FATAL EXCEPTION: main
12-26 11:09:16.350: E/AndroidRuntime(7102): java.lang.NullPointerException
12-26 11:09:16.350: E/AndroidRuntime(7102):     at com.lluvia.digilib.DashboardActivity$HeavyWorker.onPostExecute(DashboardActivity.java:468)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at com.lluvia.digilib.DashboardActivity$HeavyWorker.onPostExecute(DashboardActivity.java:1)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at android.os.AsyncTask.finish(AsyncTask.java:590)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at android.os.AsyncTask.access$600(AsyncTask.java:149)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at android.os.Looper.loop(Looper.java:132)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at android.app.ActivityThread.main(ActivityThread.java:4123)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at java.lang.reflect.Method.invoke(Method.java:491)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
12-26 11:09:16.350: E/AndroidRuntime(7102):     at dalvik.system.NativeStart.main(Native Method)

请帮助meeee :((((((

4

2 回答 2

0

you need to initialized this n getView of your Adapter calss.

        HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview2);
            listview.setAdapter(mAdapter);

like

        HorizontalListView listview = (HorizontalListView) convertview.findViewById(R.id.listview2);
            listview.setAdapter(mAdapter);

you are initializing HorizontalListView in main Activity's layout. But it is in your list_item.xml

于 2012-12-26T05:07:04.453 回答
0

请删除此

 xmlns:android="http://schemas.android.com/apk/res/android"

从你的第二个 LinearLayout。您的 xml 代码应如下所示...或将 RelativeLayout 替换为 LinearLayout。如果用作父布局,RelativeLayout 可以正常工作,如图所示。如果下面的代码不起作用,请使用RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">  

 <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- Name Label -->

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/thumb_kategori"
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:id="@+id/name"
                android:layout_width="238dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:paddingBottom="2dip"
                android:paddingTop="6dip"
                android:textColor="#43bd00"
                android:textSize="16sp"
                android:textStyle="bold" />

        </LinearLayout>
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >

      <com.devsmart.android.ui.HorizontalListView
          android:id="@+id/listview2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="40dp"
          android:layout_marginLeft="70dp"
          android:layout_marginRight="70dp"
          android:layout_marginTop="50dp"
          android:divider="#FFCC00"
          android:fillViewport="false" >

      </com.devsmart.android.ui.HorizontalListView>

  </LinearLayout>

    </LinearLayout>

</RelativeLayout>

如果这不起作用,您可以尝试使用 recyclerView 创建画廊元素的水平列表,如此处所示

于 2016-12-04T01:02:47.073 回答