2

我的公司有一支送货卡车车队,他们希望将其视为: 1)在 Android 屏幕上半部分的地图上钉住;并且,2) 在同一屏幕的下半部分包含卡车和司机详细信息的 ListView。我有一个 Web 服务,它为我提供卡车的“名称”、它的标语(我们司机的昵称)和存储在名为“TruckArray”的 ArrayList 中的地理位置。我在 FrameLayout 中创建了一个带有 mapview 和 listview 的 main.xml 布局,扩展了 MapView 类,获取了地理位置并创建了一个地图叠加层来显示每辆卡车的位置。我的屏幕正确显示了地图和卡车位置(如果我在下面注释掉对 ListAdapter 的调用)。

您可以为这个 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.
}   

我的卡车适配器是:

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;
    }
}

我的 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>

我的 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>

我的崩溃日志是:

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
4

1 回答 1

4

首先..我们可以从堆栈跟踪中看到问题出在适配器的第 273 行,在您的 getView() 方法中。但是我们不知道第 273 行是哪一行……如果您能告诉我们那一行是什么代码,那就太好了:)

现在,回到问题。

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

此上下文为空;)您将其作为成员变量,但从不初始化它。

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

于 2012-05-31T00:47:46.073 回答