0

I am getting error while creating listView

Error is as follows

     FATAL EXCEPTION: main
 java.lang.NullPointerException
    at com.teamXDev.onlineordering.FinalOrderListAdapter.getView(FinalOrderListAdapter.java:131)
    at android.widget.AbsListView.obtainView(AbsListView.java:2090)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1485)
    at android.widget.ListView.onMeasure(ListView.java:1348)
    at android.view.View.measure(View.java:12937)
    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392)
    at android.view.View.measure(View.java:12937)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5045)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
    at android.view.View.measure(View.java:12937)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
    at android.view.View.measure(View.java:12937)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5045)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2180)
    at android.view.View.measure(View.java:12937)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1227)
    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2700)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:5099)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
    D/Process(31726): killProcess, pid=31726

My Layout:activity_final_order.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/lbl_FinalOrder"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="4dp"
        android:background="#8d9190"
        android:gravity="center"
        android:text="YOUR ORDER"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFF" />

    <ListView
        android:id="@+id/lView_FinalOrder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lbl_FinalOrder"
         >

    </ListView>
</RelativeLayout>

My List View Child Item: final_order_child_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#eeeeee"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/lbl_itemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="2dp"
        android:text="Item 1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#8d9190"
        android:layout_below="@id/lbl_itemName"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/lbl_itemQty"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_margin="2dp"
            android:gravity="left"
            android:padding="5dp"
            android:text="Qty"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/lbl_itemServingSize"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_gravity="center_vertical"
            android:padding="5dp"
            android:text="Regular"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/lbl_itemTotalCost"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="right"
            android:padding="5dp"
            android:layout_marginRight="10dp"
            android:text="120"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

</RelativeLayout>

My listViewClass:FinalOrder

public class FinalOrder extends Activity {

    private static final String TAG = "FinalOrder";
    Button btn_CheckOut, btn_Apply;
    TextView txt_coupneCode, txt_subTotal, txt_finalTotal, txt_taxesFees,
            txt_couponOff;

    ListView lView_finalOrder;

    ArrayList<String> orderedItemName = new ArrayList<String>();
    ArrayList<String> orderedItemQty = new ArrayList<String>();
    ArrayList<String> orderedItemServingSize = new ArrayList<String>();
    ArrayList<String> orderedItemTotalCost = new ArrayList<String>();

    Boolean logedIn = false;
    SessionManager session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_final_order);
        lView_finalOrder = (ListView) findViewById(R.id.lView_FinalOrder);
        orderedItemName.add("SampleItem");
        orderedItemQty.add("1");
        orderedItemServingSize.add("Regular");
        orderedItemTotalCost.add("0.0");
        setView();


    }

    private void setView() {

        FinalOrderListAdapter fItemAdapter = new FinalOrderListAdapter(
                lView_finalOrder.getContext(), R.layout.final_order_child_item,
                orderedItemName, orderedItemQty, orderedItemServingSize,
                orderedItemTotalCost);

        Log.e("setView","setting Adapter");

        lView_finalOrder.setAdapter(fItemAdapter);

    }
}

My ListView Adapter Class: FinalOrderListAdapter

public class FinalOrderListAdapter extends BaseAdapter {

    // local variables that will be assigned data from the activity
    ArrayList<String> orderedItemName = new ArrayList<String>();
    ArrayList<String> orderedItemQty = new ArrayList<String>();
    ArrayList<String> orderedItemServingSize = new ArrayList<String>();
    ArrayList<String> orderedItemTotalCost = new ArrayList<String>();

    // Resource ID of the list
    int resourceID;

    // To inflate the list view
    LayoutInflater inflater;

    // the Context
    Context context;

    // Template Class of Graphic Elements which list Item will hold
    private static class ListItemHolder {

        TextView lbl_itemName, lbl_itemQty, lbl_itemServingSize,
                lbl_itemTotalCost;

    }

    // constructor which receives the context, list Resource ID along with data
    // to be displayed from the activity class

    public FinalOrderListAdapter(Context _ctx, int _rID,
            ArrayList<String> _orderedItemName,
            ArrayList<String> _orderedItemQty,
            ArrayList<String> _orderedItemServingSize,
            ArrayList<String> _orderedItemTotalCost) {
        super();

        // receive the info from activity variables to local class variable
        this.orderedItemName = _orderedItemName;
        this.orderedItemQty = _orderedItemQty;
        this.orderedItemServingSize = _orderedItemServingSize;
        this.orderedItemTotalCost = _orderedItemTotalCost;

        // receive the context from the activity
        this.context = _ctx;

        // receive the resource ID of the list
        this.resourceID = _rID;

        // initialize the inflater with the context received
        this.inflater = LayoutInflater.from(_ctx);

    }

    @Override
    public int getCount() {
        return orderedItemName.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // This view will inflate UI and add the list Item to it

        final ListItemHolder listItem;

        /*
         * covertView is layout of the Child Item of list parent is the parent
         * of list view layout position of the list Item
         */

        if (convertView == null) {
            // if this view is null its first Item of list that needs to be
            // created

            // get layout inflater
            LayoutInflater vInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vInflater.inflate(resourceID, parent, false);

            /*
             * Initialize the Item holder class view variables with the view
             * elements of convert view i.e. nothing but list child item layout
             */
            listItem = new ListItemHolder();
            listItem.lbl_itemName = (TextView) convertView
                    .findViewById(R.id.lbl_itemName);
            listItem.lbl_itemQty = (TextView) convertView
                    .findViewById(R.id.lbl_itemQty);
            listItem.lbl_itemServingSize = (TextView) convertView
                    .findViewById(R.id.lbl_itemServingSize);
            listItem.lbl_itemTotalCost = (TextView) convertView
                    .findViewById(R.id.lbl_itemCost);

            // assigns the tag listItem to the
            convertView.setTag(listItem);

        } else {

            // reading list

            listItem = (ListItemHolder) convertView.getTag();
        }

        // setting label values from the received data from the string arrays

            /* line: 129 is below line */   
        listItem.lbl_itemName.setText(orderedItemName.get(position));
        listItem.lbl_itemQty.setText(orderedItemQty.get(position));
        listItem.lbl_itemServingSize.setText(orderedItemServingSize.get(position));
listItem.lbl_itemTotalCost.setText(orderedItemTotalCost.get(position));


        return convertView;
    }

}

Any idea why this error ?

4

2 回答 2

1

正如 tolgap 和日志中所述:问题在第 131 行。

我错误地将文本设置为

 listItem.lbl_itemTotalCost.setText(orderedItemTotalCost.get(position));

但 lbl_itemTotalCost 被错误地初始化为

listItem.lbl_itemTotalCost = (TextView) convertView
                .findViewById(R.id.lbl_itemCost);

代替

listItem.lbl_itemTotalCost = (TextView) convertView
                    .findViewById(R.id.lbl_itemTotalCost);

PS 我错误地将资源分配给子项布局中不存在的膨胀 UI。

愚蠢的错误!:P

于 2013-09-08T10:46:42.397 回答
0

像这样修改你的获取视图。

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // This view will inflate UI and add the list Item to it
View rowView = convertView;


    /*
     * rowView is layout of the Child Item of list parent is the parent
     * of list view layout position of the list Item
     */

    if (rowView== null) {
        // if this view is null its first Item of list that needs to be
        // created

        // get layout inflater
        LayoutInflater vInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView= vInflater.inflate(resourceID, parent, false);

        /*
         * Initialize the Item holder class view variables with the view
         * elements of convert view i.e. nothing but list child item layout
         */
        ListItemHolder listItem = new ListItemHolder();
        listItem.lbl_itemName = (TextView) convertView
                .findViewById(R.id.lbl_itemName);
        listItem.lbl_itemQty = (TextView) convertView
                .findViewById(R.id.lbl_itemQty);
        listItem.lbl_itemServingSize = (TextView) convertView
                .findViewById(R.id.lbl_itemServingSize);
        listItem.lbl_itemTotalCost = (TextView) convertView
                .findViewById(R.id.lbl_itemCost);

        // assigns the tag listItem to the
        rowView.setTag(listItem);

    } else {

        // reading list

        ListItemHolder item = (ListItemHolder) rowView.getTag();
    }

    // setting label values from the received data from the string arrays

    listItem.lbl_itemName.setText(orderedItemName.get(position));
    listItem.lbl_itemQty.setText(orderedItemQty.get(position));
    listItem.lbl_itemServingSize.setText(orderedItemServingSize.get(position));
    listItem.lbl_itemTotalCost.setText(orderedItemTotalCost.get(position));


    return rowView;
}

}

于 2013-09-08T10:06:26.440 回答