android gridview中的NullpointerException错误

时间:2013-06-14 09:31:38

标签: android android-layout android-arrayadapter android-gridview android-adapter

我必须开发一个Android应用程序。

这里我必须创建自定义gridview适配器类。

但是我得到了以下错误:

      06-14 14:56:26.676: E/AndroidRuntime(797): Caused by: java.lang.NullPointerException
     06-14 14:56:26.676: E/AndroidRuntime(797):     at        com.bestinuk.adapter.CategoriesAdapter.getCount(CategoriesAdapter.java:34)
   06-14 14:56:26.676: E/AndroidRuntime(797):   at android.widget.GridView.setAdapter(GridView.java:180)
    06-14 14:56:26.676: E/AndroidRuntime(797):  at com.example.androidbestinuk.HomePage.onCreate(HomePage.java:43)

我已经编写了如下的适配器类:

  public class CategoriesAdapter extends BaseAdapter {

private Activity activity;

列表类别;     private static LayoutInflater inflater = null;

public CategoriesAdapter(Activity a, List<Category> categories) {
    activity = a;
   this.categories=categories;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return categories.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;

    if(convertView==null)
        vi = inflater.inflate(R.layout.list_item, null);
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.categoryimage);
    TextView title = (TextView)vi.findViewById(R.id.categorytitle); // title


    Category categorylist = (Category) getItem(position);

    title.setText(categorylist.getmCategoryName());




    thumb_image.setImageBitmap(categorylist.getmCategoryImage());

    return vi;
   }
    }

这里为什么会出现abovr错误???

我如何解决这些错误???

请给我这些解决方案???

修改

是的,我的列表为null。这就是为什么我得到空指针异常。

这是我的HomePage.java类:

  public class HomePage extends Activity {

      List<Category> categories = new ArrayList<Category>();;
          GridView category_list;
            CategoriesAdapter categoryViewAdapter;
          private ProgressDialog pDialog;
           static final String URL = "http://dev.mercuryminds.com/xctesting/feed.xml";

           /** Called when the activity is first created. */
              @Override
                public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                 setContentView(R.layout.main);

              category_list = (GridView) findViewById(R.id.categorylist);

               GetXMLTask task = new GetXMLTask(this);
             task.execute(new String[] { URL });

                }



            //private inner class extending AsyncTask
           private class GetXMLTask extends AsyncTask<String, Void, Document> {
                private Activity context;
             public GetXMLTask(Activity context) {
                  this.context = context;

             } 
             @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressbar
            pDialog = new ProgressDialog(HomePage.this);
            // Set progressbar title
            pDialog.setTitle("BestinUk");
            // Set progressbar message
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            // Show progressbar
            pDialog.show();
                    }
                @Override
            protected Document doInBackground(String... urls) {
                Document dom = null;
                String xml = null;
                for (String url : urls) {
                    xml = getXmlFromUrl("http://dev.mercuryminds.com/xctesting/feed.xml");
                    dom= XMLfunctions.XMLfromString(xml);
                    }
                return dom;
            }

                @Override
              protected void onPostExecute(Document dom) {

    Category categoryBean = null;

                if(dom !=null) {
            Element root = dom.getDocumentElement();
            NodeList categoryNL = root.getElementsByTagName("Category");
            if(categoryNL.getLength() > 0){
                for (int i=0;i<categoryNL.getLength();i++){
                    Node categoryNode = categoryNL.item(i);
                    Element categoryElmt = null;
                    if(categoryNode.hasAttributes()){
                        categoryBean = new Category();

                        categoryElmt = (Element)categoryNode;
                        categoryBean.setmCategoryName(categoryElmt.getAttribute("title"));
                        categoryBean.setmCategoryID(Integer.parseInt(categoryElmt.getAttribute("categoryid")));
                        categoryBean.setmCategoryImageLink(categoryElmt.getAttribute("image"));
   categories.add(categoryBean);
                        categoryViewAdapter = new CategoriesAdapter(HomePage.this, categories);

                        category_list.setAdapter(categoryViewAdapter);
                        System.out.println("Category Title is "+ " "+categoryElmt.getAttribute("title"));
                        System.out.println("Category Image is "+ " "+categoryElmt.getAttribute("image"));
                    }

        -----------
        ------------
          ---------
               pDialog.dismiss();
            }

           }

现在出现以下错误:

06-14 16:00:21.076: E/AndroidRuntime(1305): FATAL EXCEPTION: main
06-14 16:00:21.076: E/AndroidRuntime(1305): java.lang.ClassCastException: java.lang.Integer cannot be cast to com.example.androidbestinuk.Category
   06-14 16:00:21.076: E/AndroidRuntime(1305):  at com.bestinuk.adapter.CategoriesAdapter.getView(CategoriesAdapter.java:56)

这是我的第56行:

 Category categorylist = (Category) getItem(position);

我的代码有什么问题???请给我解决方案???

我在添加以下行后得到了输出:

 Category categorylist = categories.get(position);

4 个答案:

答案 0 :(得分:2)

* 编辑 *您可以使用add()在列表中添加内容。

- 改变

private List<Category> categories;

private List<Category> categories = new List<Category>();

(抱歉,List无法实例化,因为它是一个接口)

答案 1 :(得分:1)

这行代码错误:

categoryViewAdapter = new CategoriesAdapter(HomePage.this, categories);
category_list.setAdapter(categoryViewAdapter);

尝试首先填充for循环中的列表,并在填充列表后构造适配器。

编辑:

for (int i=0;i<categoryNL.getLength();i++){
                Node categoryNode = categoryNL.item(i);
                Element categoryElmt = null;
                if(categoryNode.hasAttributes()) {
                    categoryBean = new Category();

                    categoryElmt = (Element)categoryNode;
                    categoryBean.setmCategoryName(categoryElmt.getAttribute("title"));
                    categoryBean.setmCategoryID(Integer.parseInt(categoryElmt.getAttribute("categoryid")));
                    categoryBean.setmCategoryImageLink(categoryElmt.getAttribute("image"));

                    //add to the categories
                    categories.add(categoryBean);

                    System.out.println("Category Title is "+ " "+categoryElmt.getAttribute("title"));
                    System.out.println("Category Image is "+ " "+categoryElmt.getAttribute("image"));
                }
}

//add the categories to the category adapter
categoryViewAdapter = new CategoriesAdapter(HomePage.this, categories);

                    category_list.setAdapter(categoryViewAdapter);

当然你应该初始化类别而不是null!

编辑:

在您的适配器中,您将覆盖getItem(int position)并返回position,而是从您作为参数传递的列表中返回实际的Category对象

答案 2 :(得分:0)

此错误表示您的类别列表为空

检查您的代码在哪里新建了CategoriesAdapter,无论您的类别参数是否为空

答案 3 :(得分:0)

只是因为你可能用空名单来称呼它......

public int getCount() {
    if(categories != null)
    return categories.size();
    return 0;
}