2012-12-26 100 views
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); 

这里的布局: 我使用Horitontal列表视图从开发 - SmartLib

<?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 :(((((

+0

这将有助于看到DashboardActivity画廊元素的水平上市。 java行468和1(根据错误信息) – mfrankli

+0

为什么要在ListView中使用'Horizo​​ntalListView'。你想实现某种'Gallery'或'GridView'吗? –

+0

我会后它.. 是的,我尽量让应用程序看起来像PULSE .. – Lluvia

回答

0

您需要初始化您的Adapter n getView calss。

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

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

你在主Activity's布局初始化HorizontalListView。但它是在你的list_item.xml

+0

感谢布局list_item.xml里面..它的工作.. – Lluvia

0

请从你的第二个LinearLayout中删除此

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

。你的xml代码应该像这样...或者用LinearLayout替换RelativeLayout。如图所示,如果用作父布局,则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作为证明here