私は 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;
}
}
フード.ジャワ
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 トレース
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)