2 annbnncnnd AnnBnnCnnD 于 2013.02.25 11:16 提问

Android - Listview报错

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)

1个回答

Kill_it
Kill_it   2013.02.25 11:35
已采纳

iconcategory你应该从row中寻找视图:

ImageView icon = (ImageView) row.findViewById(R.id.categoryimage);
Button category = (Button) row.findViewById(R.id.categorybutton);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!