1

我有一个列表视图,它显示了从可绘制文件夹和标题中获取的图片。但它给出了错误。我无法确定问题所在,因此无法进行良好的搜索以找到解决方案。这是我的课程:

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)
4

1 回答 1

2

对于您iconcategory您应该从以下位置寻找视图row

ImageView icon = (ImageView) row.findViewById(R.id.categoryimage);
Button category = (Button) row.findViewById(R.id.categorybutton);
于 2013-02-25T01:02:35.207 回答