ListView NullPointerException @ ArrayAdapter.createViewFromResource

时间:2013-12-16 09:53:26

标签: android listview nullpointerexception android-arrayadapter

我目前正在获取空指针异常,只要我的listview中有6个或更多项目&&当我尝试点击任何listview行。注意:当我点击包含少于6个项目的listview行时,一切都按预期工作。

任何帮助都会很棒我已经被困在这个问题很长一段时间了= \(大约一个月)

     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)

我的代码我认为我收到此错误的原因是

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); //这是错的?         }

    @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(){//这是如何将项添加到ListView         customerItems.removeAll(Collections.singleton(空)); //稍后删除         customerItems.removeAll(Arrays.asList(NULL, “”));         ArrayAdapter适配器;

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

    }



}

3 个答案:

答案 0 :(得分:2)

1)您的编码标准不好,我建议您改进一下

2)你没有正确地提到这个问题而且你粘贴的代码评论太多了,因为很难知道你想要达到的目的,或者你是否有目的地粘贴SO。

3)你的getView()方法实现错误,请检查以下正确的方法:

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");

而您的ViewHolder包含xml文件中视图的视图声明:

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

参考示例和Android文档以清除相同的理解,这将是很好的,并为您的代码提供良好的基础。

希望它有所帮助。

答案 1 :(得分:1)

好的,所以在永远调试我的程序后,我终于找到了解决方案。我将在这里试着清楚地总结一下。

问题:当我从我的Listview调用Android的AlertDialog(多个选项项!)时,它会崩溃我的程序。

解决方案:而不是使用Androids AlertDialog进行多项选择。我必须创建自己的多项选择项的自定义版本。然后,当我点击列表视图激活alertdialog时,一切都很好。

如果您从列表视图中调用多选警报对话框,则表示您遇到了android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)的空指针异常。然后尝试创建自己的自定义多项选择!

请原谅我的重复希望将来可能对某人有所帮助。

答案 2 :(得分:0)

试试这个:

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

  }

用于设置列表视图:列表是您的项目列表;

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

试试这个,如果不起作用而不是评论: