ArrayAdapter.createViewFromResource(Null PointerException)

时间:2013-11-29 09:00:58

标签: android nullpointerexception android-arrayadapter

所以我找到了导致我的问题的确切原因,但不知道如何解决它。首先,我有一个看起来像这样的列表。 !(http://i.imgur.com/CJPiCcq.png

我可以用5个或更少的项目填写此列表视图并单击任何项​​目,警报对话框将弹出而没有任何问题。 但是当我尝试将6个或更多项添加到列表视图时,和单击程序崩溃的任何项目。并给我错误

12-18 03:31:27.582: D/AndroidRuntime(2187): Shutting        down VM
12-18 03:31:27.582: W/dalvikvm(2187): threadid=1: thread exiting with uncaught exception (group=0x95b57648)
12-18 03:31:27.582: E/AndroidRuntime(2187): FATAL EXCEPTION: main
12-18 03:31:27.582: E/AndroidRuntime(2187): java.lang.NullPointerException
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at com.android.internal.app.AlertController$AlertParams$1.getView(AlertController.java:879)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.AbsListView.obtainView(AbsListView.java:2161)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.ListView.onMeasure(ListView.java:1159)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.View.measure(View.java:15848)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.View.measure(View.java:15848)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.View.measure(View.java:15848)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.View.measure(View.java:15848)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.View.measure(View.java:15848)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.View.measure(View.java:15848)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1079)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.Choreographer.doFrame(Choreographer.java:532)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.os.Handler.handleCallback(Handler.java:730)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.os.Looper.loop(Looper.java:137)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at java.lang.reflect.Method.invoke(Method.java:525)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 03:31:27.582: E/AndroidRuntime(2187):     at     dalvik.system.NativeStart.main(Native Method)

代码:

    private class MyListAdapter extends ArrayAdapter<Item> {

    public MyListAdapter(){
        super(UserOrderMenu.this,R.layout.itemview, customerItems);
    }

    @Override // position = is which listview item
    public View getView(int position, View convertView, ViewGroup parent) {


        View itemView = convertView;

        if(itemView == null){ //makes sure we have a view to work with
            itemView = getLayoutInflater().inflate(R.layout.itemview,parent,false);
        }

        //find the item to work with. using item class and customerItem ArrayList
        final Item currentItem = customerItems.get(position);
            //currentItem is the current Item object @ location position
            //customerItems is the ArrayList that is storing all the Item objects

        ImageView imageview = (ImageView) itemView.findViewById(R.id.imageButtonTrashCan);
        imageview.setClickable(true);
        imageview.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                remove(currentItem); //this removes the current item fromt the ListView
                notifyDataSetChanged(); //this notifies the Arrayadapter to update

                float t1price, t2price, t3price, t4price, t5price;
                System.out.println(currentItem.getTopping1Used());

                if(currentItem.getTopping1Used()){      
                    t1price = currentItem.getTop1P();

                }else
                    t1price = 0;

                if(currentItem.getTopping2Used()){
                    t2price = currentItem.getTop2P();
                }else
                    t2price =0;

                if(currentItem.getTopping3Used()){
                    t3price = currentItem.getTop3P();
                }else
                    t3price = 0;

                if(currentItem.getTopping4Used()){
                    t4price = currentItem.getTop4P();
                }else
                    t4price =0;

                if(currentItem.getTopping5Used()){
                    t5price = currentItem.getTop5P();       
                }else
                    t5price =0;
                    findTotalPrice(-t1price,-t2price,-t3price,-t4price,-t5price,true);
                    findTotalPrice(-currentItem.getItemP());                
            }
        });

        //Item Name
        TextView n = (TextView) itemView.findViewById(R.id.tvItemName);
        n.setText(currentItem.getItem());

        //Item Price
        //left here
        // do we want to add User Selected Toppings to the total price on the List View?
        //if so use currentItem.getTopping1Used();
            // then currentItem.getTop1P();
        TextView p = (TextView) itemView.findViewById(R.id.tvItemPrice);    
        p.setText(String.valueOf(currentItem.getItemP()));

        return itemView;
    }

}//end of MyListAdapter Class





private void populateListView(){ //this is how to add an item to ListView
    customerItems.removeAll(Collections.singleton(null)); //remove later
    customerItems.removeAll(Arrays.asList(null,""));
    ArrayAdapter<Item> adapter;

    try{
         adapter = new MyListAdapter();
         ListView list = (ListView) findViewById(R.id.listView1);
         list.setAdapter(adapter);
    }catch(Exception e){

    }



}

2 个答案:

答案 0 :(得分:0)

Item中只有customerItems时,此代码会崩溃。确保你有超过5个。

答案 1 :(得分:0)

使用以下代码更改getView方法的前4行。它应该工作。

    View itemView = convertView;

   if(itemView == null){ //makes sure we have a view to work with
       LayoutInflater inflater =(LayoutInflater)yourContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       itemView = inflater.inflate(R.layout.itemview, parent , false);
   }