我有下面的这个类,当它运行并到达应用程序崩溃的类中的某一行时。如果我注释掉那条线,则应用程序运行良好。当我看着logcat时,我找不到任何CausedBy文字。因此无法弄清楚此崩溃的原因。有人请帮我解决这个问题。

public class Secondscreen extends Activity {


int total=0;
final ArrayList<Listitem> arrayList=new ArrayList<Listitem>();
BaseAdapter adapter =null;

@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
ListView lv= (ListView) findViewById(R.id.listView1);


final TextView showtotal    = (TextView) findViewById(R.id.totalprice);


final Button thirdBtn = (Button) findViewById(R.id.third);


final Controller aController = (Controller) getApplicationContext();

final int cartSize = aController.getCart().getCartSize();


//Addition of item to arraylist

if(cartSize >0)
    {


        for(int i=0;i<cartSize;i++)
        {

           String pName   = aController.getCart().getProducts(i).getProductName();
           int pPrice      = aController.getCart().getProducts(i).getProductPrice();
           int pQuantity      = aController.getCart().getProducts(i).getProductQuantity();
           String pDisc       = aController.getCart().getProducts(i).getProductDesc();
           total = total + pPrice;

            Listitem item=new Listitem(pName, pPrice, pDisc, pQuantity);
            Log.e("quantity", ""+pQuantity);
            Log.e("Intem's quantity", ""+item.getQuantity());
            arrayList.add(item);
            Log.e("Arraylist item quantity", ""+arrayList.get(i).getQuantity());

        }
        showtotal.setText(""+total);

    }


         adapter= new BaseAdapter(){

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return arrayList.size();
            }

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

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

            LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            @Override
            public View getView(final int position, View view, ViewGroup viewgroup) {
                if (view == null) {
                    view=inflater.inflate(R.layout.pattern, null);
                }
                TextView tv=(TextView) view.findViewById(R.id.nameview);
                TextView tv2=(TextView) view.findViewById(R.id.pdesc);
                TextView tv3=(TextView) view.findViewById(R.id.priceView);
                TextView tv4=(TextView) view.findViewById(R.id.quantityView);
                Button btn=(Button) view.findViewById(R.id.patternButton);
                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        int tempstore=arrayList.get(position).getPrice();


                        total=total-tempstore;
                        arrayList.remove(position);

                        showtotal.setText(""+total);

                        ModelProducts tempProductObject = aController.getProducts(position);

                        aController.getCart().removeProducts(tempProductObject);
                        adapter.notifyDataSetChanged();

                        int cartSize2 = aController.getCart().getCartSize();


                    }

                });


                tv.setText(arrayList.get(position).getName());
                tv2.setText(""+arrayList.get(position).getPrice());
                tv3.setText(arrayList.get(position).getDesc());
                tv4.setText(arrayList.get(position).getQuantity()); //<-this is the line which when gets executed causes the application to crash.


                return view;
            }

        };
        lv.setAdapter(adapter);





   }
}

最佳答案

是的getQuantity()返回一个int值


所以改变这个

tv4.setText(arrayList.get(position).getQuantity());




tv4.setText(String.valueOf(arrayList.get(position).getQuantity()));


如果没有找到setText(int),则ResourceNotFoundException会查找具有int值的资源,如果找不到的话

您想要的是setText(CharacterSequence),因此您需要使用String.valueof(intvalue)

关于java - 无法确定应用程序崩溃的原因,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23039621/

10-12 03:11