AnnBnnCnnD 2013-02-25 03:16 采纳率: 0%
浏览 3514
已采纳

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 2013-02-25 03:35
    关注

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

    ImageView icon = (ImageView) row.findViewById(R.id.categoryimage);
    Button category = (Button) row.findViewById(R.id.categorybutton);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 驱动学习 环境部署中的问题
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题