程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview-Android - Listview報錯

listview-Android - Listview報錯

編輯:編程綜合問答
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)

最佳回答:


iconcategory你應該從row中尋找視圖:

ImageView icon = (ImageView) row.findViewById(R.id.categoryimage);
Button category = (Button) row.findViewById(R.id.categorybutton);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved