gpt4 book ai didi

java - 导致应用程序崩溃的未知错误

转载 作者:行者123 更新时间:2023-11-30 02:57:54 24 4
gpt4 key购买 nike

我正在尝试用三个变量填充数组列表。之后,数组列表应在 ListView 中显示其内容。当我运行这个类时,logcat 显示一个我完全不知道的错误。以下是我正在使用的类(class)。

import java.util.ArrayList;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Secondscreen extends Activity {

String pName ;
int pPrice;
String pDisc;
int total;

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

//TextView showCartContent = (TextView) findViewById(R.id.showCart);
final Button thirdBtn = (Button) findViewById(R.id.third);
//final LinearLayout lb = (LinearLayout) findViewById(R.id.secondLinear);

final Controller aController = (Controller) getApplicationContext();

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

final ArrayList<Listitem> arrayList=new ArrayList<Listitem>();


BaseAdapter 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(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);
tv.setText(pName);
tv2.setText(pPrice);
tv3.setText(pDisc);
return view;
}

};
lv.setAdapter(adapter);


if(cartSize >0)
{

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

pName = aController.getCart().getProducts(i).getProductName();
pPrice = aController.getCart().getProducts(i).getProductPrice();
pDisc = aController.getCart().getProducts(i).getProductDesc();

Listitem item=new Listitem(pName, pPrice,pDisc);
arrayList.add(item);
adapter.notifyDataSetChanged();

}

}

}
@Override
protected void onDestroy() {

super.onDestroy();


}
}

请帮我解决这个问题。

最佳答案

tv2.setText(pPrice);

pPrice 是一个整数。 setText(int) 在 R 类中查找字符串 ID,如果查找失败则抛出 ResourceNotFoundException。快速修复是

tv2.setText(""+pPrice);

通过这种方式,您向 setText 提供了一个 String 对象

编辑:

以这种方式更改您的 getView:

   @Override
public View getView(int position, View view, ViewGroup viewgroup) {
if (view == null) {
view=inflater.inflate(R.layout.pattern, null);
}
Listitem item = (Listitem)getItem(position)
TextView tv=(TextView) view.findViewById(R.id.nameview);
TextView tv2=(TextView) view.findViewById(R.id.pdesc);
TextView tv3=(TextView) view.findViewById(R.id.priceView);
tv.setText(item. pName);
tv2.setText(""+item.pPrice);
tv3.setText(item.pDisc);
return view;
}

关于java - 导致应用程序崩溃的未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22985455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com