在我的自定义列表视图Android ==>空指针异常..?

时间:2013-04-19 17:26:47

标签: java android listview arraylist

我是Android和Java的新手, 请帮助我这让我疯狂2天。 我的代码有什么问题。

当我运行代码时,它显示“不幸的是(程序)已停止。

我试着在调试模式中找到答案。我看到Null指针异常。

关键是我不知道错误在哪里。


MainActivity.Java

public class MainActivity extends Activity {

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

    ArrayList<Food> arrayList = new ArrayList<Food>();
    Food f1 = new Food("COKE", 1, 1, 1);
    Food f2 = new Food("PEPSI", 2, 2, 2);
    Food f3 = new Food("Pizza", 3, 3, 2);
    Food f4 = new Food("Sprite", 4, 4, 2);

    arrayList.add(f1);
    arrayList.add(f2);
    arrayList.add(f3);
    arrayList.add(f4);

    FoodAdapter fa = new FoodAdapter(this, R.layout.food_row, arrayList);
    ListView lv = (ListView) findViewById(R.id.listView1);
    lv.setAdapter(fa);

}

FoodAdapter.Java

public class FoodAdapter extends ArrayAdapter<Food> {
private Context context;
private int resourceId;
private ArrayList<Food> objects = new ArrayList<Food> ();

public FoodAdapter(Context context, int textViewResourceId,
        ArrayList<Food>  objects) {
    super(context, textViewResourceId, objects);

    this.context = context;
    this.resourceId = textViewResourceId;
    this.objects = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = null;
    FoodHolder holder = null;
    if(convertView==null){
        LayoutInflater inflater =( (Activity)context).getLayoutInflater();
        v = inflater.inflate(resourceId, null);  

        holder.Name = (TextView) v.findViewById(R.id.textView1);
        holder.FoodId = (TextView) v.findViewById(R.id.textView2);
        holder.SubId = (TextView) v.findViewById(R.id.textView3);

        v.setTag(holder);

    }else{
        holder = (FoodHolder)v.getTag();
    }
    Food food = objects.get(position);
    holder.FoodId.setText(food.FoodId);
    holder.Name.setText(food.Name);
    holder.SubId.setText(food.SubId);

    return v;
}

static class FoodHolder{
    TextView Name;
    TextView FoodId;
    TextView SubId;
}

}


Food.Java

public class Food{
public Food(String name, int foodId, int subId, int productMode) {
    super();
    Name = name;
    FoodId = foodId;
    SubId = subId;
    ProductMode = productMode;
}
public String Name;
public int FoodId;
public int SubId;
public int ProductMode;

}

修改

logcat trace

  04-20 00:47:56.785: W/ResourceType(1486): No package identifier when getting value for resource number 0x00000001
04-20 00:47:56.785: D/AndroidRuntime(1486): Shutting down VM
04-20 00:47:56.785: W/dalvikvm(1486): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-20 00:47:56.814: E/AndroidRuntime(1486): FATAL EXCEPTION: main
04-20 00:47:56.814: E/AndroidRuntime(1486): android.content.res.Resources$NotFoundException: String resource ID #0x1
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.content.res.Resources.getText(Resources.java:247)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.TextView.setText(TextView.java:3428)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at com.example.testconcept.FoodAdapter.getView(FoodAdapter.java:50)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.AbsListView.obtainView(AbsListView.java:2033)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.ListView.makeAndAddView(ListView.java:1772)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.ListView.fillDown(ListView.java:672)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.ListView.fillFromTop(ListView.java:732)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.ListView.layoutChildren(ListView.java:1625)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.AbsListView.onLayout(AbsListView.java:1863)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.View.layout(View.java:11180)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.ViewGroup.layout(ViewGroup.java:4203)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.View.layout(View.java:11180)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.ViewGroup.layout(ViewGroup.java:4203)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.View.layout(View.java:11180)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.ViewGroup.layout(ViewGroup.java:4203)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.View.layout(View.java:11180)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.ViewGroup.layout(ViewGroup.java:4203)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.os.Looper.loop(Looper.java:137)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at android.app.ActivityThread.main(ActivityThread.java:4340)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at java.lang.reflect.Method.invoke(Method.java:511)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-20 00:47:56.814: E/AndroidRuntime(1486):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:5)

您尚未实例化您的持有人

if(convertView==null){

    holder =new FoodHolder();
     ..
     ..

}

修改

第二个问题是

  

您正在为GetView中的TextView分配int值,它将被视为字符串资源ID

因此它会抛出未找到的String资源异常

尝试像这样设置它们

 holder.FoodId.setText(String.valueOf(food.FoodId));
 holder.Name.setText(food.Name);
 holder.SubId.setText(String.valueOf(food.SubId));

答案 1 :(得分:0)

以这种方式初始化并使用它,

ViewHolder holder;              
if (convertView == null) 
{
   holder = new ViewHolder();
   holder.text = (TextView) rowView.findViewById(R.id.list_text);                   
   rowView.setTag(holder);
} 
else
{
    rowView = convertView;
    holder = new ViewHolder();
    holder = (ViewHolder) rowView.getTag();
}