首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Listview中的水平Listview

Listview中的水平Listview
EN

Stack Overflow用户
提问于 2012-12-26 12:34:47
回答 2查看 1.2K关注 0票数 1

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

代码语言:javascript
复制
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的

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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)

请帮助我:(

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-26 13:07:04

你需要初始化你的Adapter类的n个getView

代码语言:javascript
复制
        HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview2);
            listview.setAdapter(mAdapter);

喜欢

代码语言:javascript
复制
        HorizontalListView listview = (HorizontalListView) convertview.findViewById(R.id.listview2);
            listview.setAdapter(mAdapter);

您正在初始化Activity's主布局中的HorizontalListView。但它就在你的list_item.xml

票数 0
EN

Stack Overflow用户

发布于 2016-12-04 09:02:47

请去掉这个

代码语言:javascript
复制
 xmlns:android="http://schemas.android.com/apk/res/android"

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

代码语言:javascript
复制
<?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>

如果这不起作用,您可以尝试使用演示的here中的recyclerView创建图库元素的水平列表

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14036035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档