user3048652
user3048652

Reputation:

ListView NullPointerException @ ArrayAdapter.createViewFromResource

i am currently getting a null pointer exception whenever my listview has 6 or more items in it && when i try to click on any listview row. Note: when i click on a listview row with less than 6 items everything works as intended.

Any help would be wonderful i've been stuck on this problem for a very long time = \ (about a month)

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

My Code Where I believe that I am getting this error is

private class MyListAdapter extends ArrayAdapter<Item> 
{


    public MyListAdapter(){
        //Important 4 SO: customerItems is an arraylist of <Item> Objects

super(UserOrderMenu.this,R.layout.itemview, customerItems); //is this wrong? }

    @Override 
    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);


        } else{
            itemView = convertView;
        }

        //find the item to work with. using item class and customerItem ArrayList

        final Item currentItem = customerItems.get(position);


        ImageView imageview = (ImageView) itemView.findViewById(R.id.imageButtonTrashCan);
        imageview.setClickable(true);


        imageview.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



                remove(currentItem); 
                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());

            }
        });





        TextView n = (TextView) itemView.findViewById(R.id.tvItemName);
            n.setText(currentItem.getItem().toString());



            // 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){

    }



}

Item.java:

public class Item {

private String item="none";
private float itemP=0;
private String top1="none";
private float top1P=0;
private String top2="none";
private float top2P=0;
private String top3="none";
private float top3P=0;
private String top4="none";
private float top4P=0;
private String top5="none";
private float top5P=0;


private boolean topping1used = false;
private boolean topping2used = false;
private boolean topping3used = false;
private boolean topping4used = false;
private boolean topping5used = false;

public Item(){
    super();
}

public Item(String item, float itemP){
    super();
    this.item = item;
    this.itemP = itemP;
}

public Item(String item, float itemP,String top1,float top1P,String top2,float top2P,String top3,
             float top3P,String top4,float top4P,String top5,float top5P)
{       
    super();


    this.item = item;

    this.itemP = itemP;
    //this is a price dont have to worry about it


    this.top1 = top1;
    this.top1P = top1P;


    this.top2 = top2;
    this.top2P = top2P;


    this.top3 = top3;
    this.top3P = top3P;


    this.top4 = top4;
    this.top4P = top4P;


    this.top5 = top5;
    this.top5P = top5P;
}

public Item(boolean t1, boolean t2, boolean t3, boolean t4, boolean t5){
    super();
    this.topping1used = t1;
    this.topping2used = t2;
    this.topping3used = t3;
    this.topping4used = t4;
    this.topping5used = t5;

}


//set 
public void setTopping1used(boolean topping1used) {
    this.topping1used = topping1used;
}

public void setTopping2used(boolean topping2used) {
    this.topping2used = topping2used;
}

public void setTopping3used(boolean topping3used) {
    this.topping3used = topping3used;
}

public void setTopping4used(boolean topping4used) {
    this.topping4used = topping4used;
}

public void setTopping5used(boolean topping5used) {
    this.topping5used = topping5used;
}




        //get 
public String getItem() {
    return item;
}

public float getItemP() {
    return itemP;
}

public String getTop1() {
    return top1;
}

public float getTop1P() {
    return top1P;
}

public String getTop2() {
    return top2;
}

public float getTop2P() {
    return top2P;
}

public String getTop3() {
    return top3;
}

public float getTop3P() {
    return top3P;
}

public String getTop4() {
    return top4;
}

public float getTop4P() {
    return top4P;
}

public String getTop5() {
    return top5;
}

public float getTop5P() {
    return top5P;
}

public boolean getTopping1Used(){
    return topping1used;
}
public boolean getTopping2Used(){
    return topping2used;
}
public boolean getTopping3Used(){
    return topping3used;
}
public boolean getTopping4Used(){
    return topping4used;
}
public boolean getTopping5Used(){
    return topping5used;
}


}//end of Item class


private void populateList(String item, float price,String t1n, float t1p, String t2n, float t2p,
                            String t3n,float t3p, String t4n, float t4p, String t5n, float t5p)
{

    customerItems.add(new Item(item,price,t1n,t1p,t2n,t2p,t3n,t3p,t4n,t4p,t5n,t5p));
    populateListView();
}

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

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

    }



}

Upvotes: 2

Views: 3182

Answers (3)

user3048652
user3048652

Reputation:

Ok so after forever debugging my program i finally found a solution. I will try to clearly summarize it here.

The problem: When i was calling Android's AlertDialog(of Multiple Choices items!) from my Listview it would crash my program.

The Solution: Instead of using Androids AlertDialog for Multiple choice items. I had to create my own custom version of multiple choice item selection. Then when i clicked on the listview to activate the alertdialog all was well again.

If you are calling a multiple choice alert dialog from a listview click, and you are experiencing the null pointer exception of android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394). Then try creating your own custom multiple choice selection!

Excuse my repetition hope this might help someone in the future.

Upvotes: 1

Cropper
Cropper

Reputation: 1177

Try with this :

private class MyListAdapter extends BaseAdapter 
{
 Context mContext;
List<Item> mItem;

public MyListAdapter(Context context,List<Item> item){
    super(context, item); 
   this.mContext =context;
  this.mItem=item;
}
@Override
    public int getCount() {

        return item.size();
    }

    @Override
    public Object getItem(int position) 
        return position;
    }

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) {


    View itemView = convertView;

    if(itemView == null){ 
        itemView = getLayoutInflater().inflate(R.layout.itemview, null); 


    }


    final Item currentItem = customerItems.get(position);



    ImageView imageview = (ImageView) itemView.findViewById(R.id.imageButtonTrashCan);
    imageview.setClickable(true);


    imageview.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {



        }
    });





    TextView n = (TextView) itemView.findViewById(R.id.tvItemName);
        n.setText(currentItem.getItem.toString());


    TextView p = (TextView) itemView.findViewById(R.id.tvItemPrice);

        p.setText(String.valueOf(currentItem.getItemP()));

        return itemView;
}

  }

And for setting list view : And list is your List of Items ;

     MyListAdapter adapter = new MyListAdapter(this, list);
     ListView list = (ListView) findViewById(R.id.listView1);
     list.setAdapter(adapter);

try with this and If doesn't work than comment :

Upvotes: 0

abhy
abhy

Reputation: 951

1) Your coding standards are not good, I suggest you to please improve on that

2) You didn't mention the problem correctly and the code you pasted is too much commented, as it's difficult to know what you are trying to achieve or you commented on purpose for pasting over SO.

3) Your getView() method implementation is wrong, check the correct way below:

if (convertView == null) 
{

    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = mLayoutInflater.inflate(R.layout.Your_Row_XML_File, null);

    viewHolder = new ViewHolder();
    viewHolder.textView1 = (TextView) convertView.findViewById(R.id.textView1_in_row_xml_inflated_as_above);
    convertView.setTag(viewHolder);

}
else
{
    viewHolder = (ViewHolder) convertView.getTag();
}

viewHolder.textView1.setText("Any string value");

whereas your ViewHolder contains view declarations of your views in your xml file:

private final class ViewHolder 
{
    public TextView textView1;
    public TextView textView2; // if any and add views like this
}

Refer examples and Android Docs to clear out understanding for the same, it will be good and give a good base to your code.

Hope it helps.

Upvotes: 2

Related Questions