数组列表中的Android ListAdapter

时间:2012-05-31 00:05:11

标签: android listview listadapter

我的公司有一批他们想要查看的送货卡车:1)Android屏幕上半部分地图上的图钉; 2)在同一屏幕的下半部分包含卡车和驾驶员详细信息的ListView。我有一个Web服务,它为我提供了卡车的“名称”,它的口号(我们的驱动程序的昵称),以及存储在名为“TruckArray”的ArrayList中的地理位置。我在FrameLayout中创建了一个包含mapview和listview的main.xml布局,扩展了MapView类,获得了地理位置并创建了一个地图叠加层来显示每辆卡车的位置。我的屏幕正确显示地图和卡车位置(如果我在下面注释我对ListAdapter的调用)。我的问题是我调用设置ListAdapter错误并强制关闭我的应用程序,既没有显示map也没有显示listarray。

您可以提供这个Android新手的任何帮助将非常感谢!

我初始化ListView的调用是:

public void initList() {


    listView = null;
    ListView listView = (ListView) findViewById(R.id.mylist);
    adapter = new TruckAdapter(this, R.id.mylist, TruckArray);
    listView.setAdapter(adapter);  // If I comment out this line, the map appears.  If I uncomment it, the whole app force closes before the map is drawn.
}   

我的TRUCKADAPTER是:

public class TruckAdapter extends ArrayAdapter<Truck> {

    private ArrayList<Truck> trucks;
    private Context context;

    public TruckAdapter(Context context, int textViewResourceId, ArrayList<Truck> objects){
        super(context, textViewResourceId, objects);
        this.trucks = objects;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        View view = convertView;

        if (view == null) {
            LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = viewInflater.inflate(R.layout.row, null);
        }

        Truck truck = trucks.get(position);

        if (truck != null) {
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView slogan = (TextView) view.findViewById(R.id.slogan);

            if (name != null) {
                name.setText(truck.name);
            }

            if (slogan != null){
                slogan.setText(truck.slogan);
            }
        }
        return view;
    }
}

MY ROW.XML文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:padding="4dip"
  >
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    > 
    <TextView android:id="@+id/name"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:gravity="center_vertical"
      android:textStyle="bold"
      android:maxLines="1"
      android:ellipsize="end"
    />
    <TextView android:id="@+id/slogan"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:gravity="center_vertical"
      android:maxLines="1"
      android:ellipsize="end"
    />
      </LinearLayout>
    </LinearLayout>

MY MAIN.XML文件是:

<?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="fill_parent"
android:orientation="vertical"
android:weightSum="2" >

    <FrameLayout    android:id="@+id/mapContainer"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" >

                    <com.google.android.maps.MapView
                            xmlns:android="http://schemas.android.com/apk/res/android"
                            android:id="@+id/map"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:apiKey="my APIkey is here..."
                            android:clickable="true" />
    </FrameLayout>

    <FrameLayout    android:layout_width="match_parent"
            android:layout_height="0dp"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_weight="1" >
    <ListView   android:id="@+id/mylist"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#000000"
                android:gravity="center_horizontal" />
    </FrameLayout>

</LinearLayout>

MY CRASH LOG IS:

05-30 20:17:49.754: W/CursorWrapperInner(30659): Cursor finalized without prior close()
05-30 20:17:49.965: D/dalvikvm(30659): GC_CONCURRENT freed 717K, 48% free 3783K/7175K, external 2357K/2773K, paused 2ms+3ms
05-30 20:17:50.035: D/dalvikvm(30659): GC_EXTERNAL_ALLOC freed 528K, 52% free 3447K/7175K, external 2757K/2773K, paused 34ms
05-30 20:17:50.105: D/AndroidRuntime(30659): Shutting down VM
05-30 20:17:50.105: W/dalvikvm(30659): threadid=1: thread exiting with uncaught exception (group=0x40018560)
05-30 20:17:50.105: E/AndroidRuntime(30659): FATAL EXCEPTION: main
05-30 20:17:50.105: E/AndroidRuntime(30659): java.lang.NullPointerException
05-30 20:17:50.105: E/AndroidRuntime(30659):    at com.deltruck.DlvrTruckMain$TruckAdapter.getView(DlvrTruckMain.java:273)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.AbsListView.obtainView(AbsListView.java:1456)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.ListView.makeAndAddView(ListView.java:1821)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.ListView.fillDown(ListView.java:686)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.ListView.fillFromTop(ListView.java:754)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.ListView.layoutChildren(ListView.java:1670)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.AbsListView.onLayout(AbsListView.java:1286)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.os.Looper.loop(Looper.java:130)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at android.app.ActivityThread.main(ActivityThread.java:3806)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at java.lang.reflect.Method.invokeNative(Native Method)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at java.lang.reflect.Method.invoke(Method.java:507)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-30 20:17:50.105: E/AndroidRuntime(30659):    at dalvik.system.NativeStart.main(Native Method)
05-30 20:17:53.238: I/Process(30659): Sending signal. PID: 30659 SIG: 9

1 个答案:

答案 0 :(得分:4)

首先..我们可以从堆栈跟踪中看到问题出在适配器的第273行,在getView()方法中。但是我们没有线索,第273行是哪一行...如果你能告诉我们那行代码是什么真的很好:)

现在,回到这个问题。

LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

此上下文为null;)您将其作为成员变量,但绝不会将其初始化。

修改 您不必将ArrayList存储为成员。对超级构造函数的调用会解决这个问题,你可以使用getItem(int position)从ArrayList中获取一个项目