listview顯示一張圖片,圖片來自可拉取的文件夾和標題。但是結果報錯。不知道應該怎麼解決了?
public class Categories extends Activity {
ListView EventCategories;
String CategoryList[] = { "Sinema", "Tiyatro", "Spor Faaliyetleri",
"Müzik", "Sahne Sanatları" };
int CategoryImageList[] = { R.drawable.sinema, R.drawable.tiyatro,
R.drawable.spor, R.drawable.concert, R.drawable.sahnesanatlari };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_list);
EventCategories = (ListView) findViewById(R.id.categorylist);
final MyAdapter<String> categories = new MyAdapter<String>(this,
android.R.layout.simple_list_item_1, R.id.categorylist,
CategoryList);
EventCategories.setAdapter(categories);
}
public class MyAdapter<String> extends ArrayAdapter<String> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.category_list_item, parent,
false); //category list item has an Imageview and a Button
ImageView icon = (ImageView) findViewById(R.id.categoryimage);
Button category = (Button) findViewById(R.id.categorybutton);
category.setText(CategoryList[position]);
icon.setImageResource(CategoryImageList[position]);
return row;
}
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] mylist) {
super(context, resource, textViewResourceId, mylist);
}
}
}
報錯信息如下,其中44行是category.setText(CategoryList[position]);
02-25 02:46:06.415: E/AndroidRuntime(458): FATAL EXCEPTION: main
02-25 02:46:06.415: E/AndroidRuntime(458): java.lang.NullPointerException
02-25 02:46:06.415: E/AndroidRuntime(458): at com.example.example.Categories$MyAdapter.getView(Categories.java:44)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.ListView.makeAndAddView(ListView.java:1727)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.ListView.fillDown(ListView.java:652)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.ListView.fillFromTop(ListView.java:709)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.ListView.layoutChildren(ListView.java:1580)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.view.View.layout(View.java:7035)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.view.View.layout(View.java:7035)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.view.View.layout(View.java:7035)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.view.View.layout(View.java:7035)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.os.Looper.loop(Looper.java:123)
02-25 02:46:06.415: E/AndroidRuntime(458): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-25 02:46:06.415: E/AndroidRuntime(458): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 02:46:06.415: E/AndroidRuntime(458): at java.lang.reflect.Method.invoke(Method.java:521)
02-25 02:46:06.415: E/AndroidRuntime(458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-25 02:46:06.415: E/AndroidRuntime(458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-25 02:46:06.415: E/AndroidRuntime(458): at dalvik.system.NativeStart.main(Native Method)
看icon
和category
你應該從row
中尋找視圖:
ImageView icon = (ImageView) row.findViewById(R.id.categoryimage);
Button category = (Button) row.findViewById(R.id.categorybutton);