Java 无法确定应用程序崩溃的原因
我有下面这个类,当它运行到类中的某一行时,应用程序崩溃。如果我注释掉那一行,应用程序运行良好。当我查看日志时,我没有发现任何由文本引起的问题。所以我们无法找出这次坠机的原因。请有人帮我解决这个问题Java 无法确定应用程序崩溃的原因,java,android,Java,Android,我有下面这个类,当它运行到类中的某一行时,应用程序崩溃。如果我注释掉那一行,应用程序运行良好。当我查看日志时,我没有发现任何由文本引起的问题。所以我们无法找出这次坠机的原因。请有人帮我解决这个问题 public class Secondscreen extends Activity { int total=0; final ArrayList<Listitem> arrayList=new ArrayList<Listitem>(); BaseAdapter adap
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);
}
}
public class Secondscreen扩展活动{
int-total=0;
最终ArrayList ArrayList=新ArrayList();
BaseAdapter=null;
@凌驾
创建时的公共void(最终捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
ListView lv=(ListView)findViewById(R.id.listView1);
最终文本视图showtotal=(文本视图)findViewById(R.id.totalprice);
最终按钮thirdBtn=(按钮)findViewById(R.id.third);
最终控制器A控制器=(控制器)getApplicationContext();
final int cartSize=aController.getCart().getCartSize();
//将项添加到arraylist
如果(cartSize>0)
{
对于(int i=0;i
yes getQuantity()返回一个int值
所以改变这个
tv4.setText(arrayList.get(position).getQuantity());
到
发生的情况是setText(int)
查找具有int值的资源,如果未找到,则最终得到ResourceNotFoundException
您需要的是setText(CharacterSequence)
所以您需要使用String.valueof(intvalue)
什么是getQuantity()
返回int值?yes getQuantity()返回int值
tv4.setText(String.valueOf(arrayList.get(position).getQuantity()));