PaulPerkins
PaulPerkins

Reputation: 113

Android ListAdapter from Array List

My company has an fleet of delivery trucks that they want to view as: 1) pins on a map on the upper half of an Android screen; and, 2) a ListView containing the truck and driver details at the lower half of the same screen. I have a web service that provides me with the truck's "name", its slogan (our driver's nickname), and geopoint location stored in an ArrayList called 'TruckArray'. I have created a main.xml layout with both a mapview and listview in FrameLayout, extended the MapView class, obtained the geolocation and have created a map overlay to display the location of each truck. My screen shows the map and truck locations correctly (if I comment out my call to the ListAdapter, below). MY PROBLEM is that my call to set the ListAdapter errors and force closes my application with neither the map nor listarray being shown.

Any help that you could offer this Android newbie would be greatly appreciated!

My call to initialize my ListView is:

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

MY TRUCKADAPTER IS:

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 FILE IS:

<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 FILE IS:

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

Upvotes: 2

Views: 4499

Answers (1)

Ole
Ole

Reputation: 7989

First of all.. We can see from the stacktrace that the issue is in line 273 of your Adapter, in your getView() method. But we don't have a clue which line, line 273 is... Its really nice if you could tell us what code is on that line :)

Now, back to the issue.

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

This context is null ;) You have it as a member variable, but never initalize it.

Edit: You don't have to store the ArrayList as a member. The call to super-constructor takes care of that, and you can fetch an item from the ArrayList with getItem(int position)

Upvotes: 4

Related Questions