-1

当我在下面的类中声明一个 gridview 时,我得到了一系列错误。

class LoadAllProducts extends Activity //get call on dynamic btn action 
{
    public void onCreate() {
        System.out.println("I have enterd in external class");
        // ***GridView gridView3 = (GridView) findViewById(R.id.gridview3);***//cant declare getting error  
        ArrayList mButtons3 = new ArrayList(); //to print button dynamically on gridview 
        Button cb3; //=new Button(this); if do so getting error 
        JSONObject json2 = null;
        json2 = jParser.makeHttpRequest(url_all_products, "GET", params);

        //  System.out.println("success to get json object for all products/t"+json2);// getting data from php an mysql database   

        JSONArray products2 = null;
        String[] Itemname = null;
        String[] rcatacode = null;
        try {
            int success2 = json2.getInt(TAG_ALLPRODUCT_SUCCESS);
            products2 = json2.getJSONArray(TAG_ALLPRODUCT_PRODUCTS);

            Itemname = new String[products2.length()];
            rcatacode = new String[products2.length()];
            if (success2 == 1) {
                cb3 = new Button(this);
                for (int i = 0; i < products2.length(); i++) {
                    JSONObject c = products2.getJSONObject(i);
                    // Storing each json item in variable 
                    String itemcode = c.getString(TAG_ALLPRODUCT_MCODE); //to store data from database 
                    String itemname = c.getString(TAG_ALLPRODUCT_ITEM_NAME);
                    String item_rate = c.getString(TAG_ALLPRODUCT_RATE);
                    String r_cata = c.getString(TAG_ALLPRODUCT_RCATA);
                    Itemname[i] = itemname;
                    rcatacode[i] = r_cata;

                }
            }
        } // try closed 
        catch (JSONException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < 20; i++) {
            // Button b=new Button(this); //getting error on here also 

            System.out.println(Itemname[i]);
        }
    }
}
4

3 回答 3

1

我什至不知道从哪里开始...

  1. 您扩展Activity但您onCreate()缺少Bundle参数
  2. 你不打电话super.onCreate()
  3. ArrayList mButtons3 = new ArrayList();不管用。ArrayList需要一个类型,比如ArrayList<Button> mButtons3 = new ArrayList<Button>();
  4. 你从不打电话setContentView()意味着你的活动永远不会显示什么

这只是第一行代码……您确定您了解 Android 开发的基础知识吗?

于 2013-04-03T12:47:57.423 回答
0

除非您调用定义此 Activity 将创建的布局的 setContentView,否则您不能调用 findViewById。想一想,你从哪里拿起gridview?哪种布局?

于 2013-04-03T12:50:46.987 回答
0

问题已通过在块外声明动态按钮来解决try-catch... Button, TextView, EditText.. etc 的声明不能在 中完成try-catchinstance.setOnclickLisner(new Onclick..){}而是可以通过调用任务函数来完成

于 2013-05-29T04:36:05.653 回答