gpt4 book ai didi

android - ListView NullPointerException @ ArrayAdapter.createViewFromResource

转载 作者:搜寻专家 更新时间:2023-11-01 08:53:59 27 4
gpt4 key购买 nike

每当我的 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){

}



}

项目.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(null));//稍后删除 customerItems.removeAll(Arrays.asList(null,"")); ArrayAdapter适配器;

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

}



}

最佳答案

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 文件中包含您的 View 的 View 声明:

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

引用示例和 Android 文档以明确理解,这会很好,并为您的代码奠定良好的基础。

希望对您有所帮助。

关于android - ListView NullPointerException @ ArrayAdapter.createViewFromResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20607826/

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