listView.setAdapter崩溃(listAdapter)

时间:2016-03-21 19:33:04

标签: java android listview adapter

我完成了本指南的所有步骤: https://www.vlemonn.com/Blog/Android/Android-Custom-ListView-with-ImageView-EditText-and-Button/

问题是它在listView.setAdapter(listAdapter);

崩溃

它给我一个NullPointerException()错误。 指南中是否有未指定的内容?

我把主类的代码:

public class Prova extends AppCompatActivity implements CustomButtonListener {

// Declaring Your View and Variables
private ListView listView;
private ListAdapter listAdapter;
String[] arrItems,  prices;
TypedArray images;

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Antipasti","Primi piatti"};
int Numboftabs =2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ordinazione_2);

    // Creating The Toolbar and setting it as the Toolbar for the activity
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    toolbar.setTitle("Prova");

    arrItems = getResources().getStringArray(R.array.fruitName);
    images = getResources().obtainTypedArray(R.array.fruitImages);
    prices = getResources().getStringArray(R.array.Price);
    listView = (ListView) findViewById(R.id.customListView);
    listAdapter = new ListAdapter(this,arrItems,images,prices);
    listView.setAdapter(listAdapter);
    listAdapter.setCustomButtonListener(this);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);



}

@Override
public void onButtonClickListener(int position,EditText editText, int value) {
    /*
    View view = listView.getChildAt(position);*/
    int quantity = Integer.parseInt(editText.getText().toString());
    quantity = quantity + 1 * value;
    if(quantity<0)
        quantity=0;
    editText.setText(quantity + "");
}

然后 ListAdapter.java

public class ListAdapter extends BaseAdapter {

//public ArrayList<HashMap<String, String>> listQuantity;
public ArrayList<Integer> quantity = new ArrayList<Integer>();
private String[] listViewItems,prices;
TypedArray images;

private Context context;
CustomButtonListener customButtonListener;


public ListAdapter(Context context, String[] listViewItems, TypedArray images, String[] prices) {
    this.context = context;
    this.listViewItems = listViewItems;
    this.images = images;
    this.prices=prices;

    for(int i =0; i< listViewItems.length; i++  )
    {
        quantity.add(0);
        //quantity[i]=0;
    }
}

public void setCustomButtonListener(CustomButtonListener customButtonListner)
{
    this.customButtonListener = customButtonListner;
}

@Override
public int getCount() {
    return listViewItems.length;
}

@Override
public String getItem(int position) {
    return listViewItems[position];
}


@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row;
    final ListViewHolder listViewHolder;
    if(convertView == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.sis ,parent,false);
        listViewHolder = new ListViewHolder();
        listViewHolder.tvFruitName = (TextView) row.findViewById(R.id.textView4);
        listViewHolder.ivFruit= (ImageView) row.findViewById(R.id.icon);
        listViewHolder.tvPrices = (TextView) row.findViewById(R.id.prezzo);
        listViewHolder.btnPlus = (ImageButton) row.findViewById(R.id.add);
        listViewHolder.edTextQuantity = (EditText) row.findViewById(R.id.editText2);
        listViewHolder.btnMinus = (ImageButton) row.findViewById(R.id.remove);
        row.setTag(listViewHolder);
    }
    else
    {
        row=convertView;
        listViewHolder= (ListViewHolder) row.getTag();
    }


    listViewHolder.ivFruit.setImageResource(images.getResourceId(position, -1));
    listViewHolder.tvPrices.setText(prices[position]);

    try{

        listViewHolder.edTextQuantity.setText(quantity.get(position) + "");


    }catch(Exception e){
        e.printStackTrace();
    }

    listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (customButtonListener != null) {
                customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, 1);
                quantity.set(position,quantity.get(position) + 1);
            }

        }
    });
    //listViewHolder.edTextQuantity.setText("0");
    listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (customButtonListener != null) {
                customButtonListener.onButtonClickListener(position,listViewHolder.edTextQuantity,-1);
                if(quantity.get(position)>0)
                    quantity.set(position, quantity.get(position) - 1);
            }
        }
    });
    listViewHolder.tvFruitName.setText(listViewItems[position]);
    return row;
}

1 个答案:

答案 0 :(得分:0)

只要查看该页面上的代码,我就会看到

android:id="@ id/customListView"

应该阅读

android:id="@+id/customListView"

+非常重要,因为它告诉Android为其创建资源,以便您可以执行findViewById(R.id.customListView)

ordinazione_2.xml实际上不包含@+id/customListView,在这种情况下findViewById(R.id.customListView)将返回null,从而导致错误。

事实上,该页面上的所有布局都有@ id ...您可能想尝试better tutorial