android gridview问题

时间:2011-08-01 10:38:43

标签: android gridview imagebutton

我的以下代码存在问题:

public class RestListActivity extends Activity{
    ImageButton [] buttons;
    RelativeLayout rl;
    int restCount=0;
    Resources a;
    int resIndex=0;
    boolean resFound=false;
    int last;
    int resCount=0;
    GridView gv;
    // TODO Auto-generated constructor stub
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.restlist);
        rl = (RelativeLayout) findViewById(R.id.relativeLayoutRest);

        Log.i("asd","asd");

        gv=new GridView(this);
        a=this.getResources();
        int i=0x7f020000;
        boolean done=true;

        Log.i("asd","asd");
        try
        {
            for (i=0x7f020000 ;done&&i<0x7f020040;i++)
            {
                Log.i("rest buttons","----"+i);
                String name=a.getResourceName(i);
                name=name.substring(name.indexOf(':')+1);
                name=name.substring(9);
                if(name.startsWith("rest"))
                {
                    if(!resFound)
                    {
                        resIndex=i;
                        resFound=true;
                    }
                    Log.i("rest buttons",name);

                    resCount++;
                }
            }
        }
        catch(Exception e)
        {
            last=i;
            Log.i("LAST",i+"");
            done=false;
            e.printStackTrace();
        }
        buttons=new ImageButton[resCount];
        Button orderButton = (Button)findViewById(R.id.adbutton);
        orderButton.setText("REKLAMLAR");
        orderButton.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) { 
                Log.i("ACTION","AD BUTTON CLICKED");
                try
                {
                    Intent i = new Intent(RestListActivity.this,AVMSystemActivity.class);   
                    startActivity(i);
                }
                catch(Exception e)
                {
                    Log.i("cort",e.toString());
                }
            } 
        });
        if(rl==null)
            Log.i("------------","RLOUT NULL AGA");
        else
            Log.i("------------","RLOUT EXISTS AGA");
        //************************************************************************************
//ITS OK UNTIL HERE. IT COUNTS THE NUMBER OF IMAGE RESOURCES AND FILTERS THE PNGS WITH NAME //STARTING WITH "rest" (they are restaurant logos). WHEN I TRY TO ADD THE IMAGES TO A //GRIDVIEW AS AN IMAGEBUTTONS THE PROBLEM ABOVE THE CODE OCCURS.
//************************************************************************************
        for(int j=resIndex,f=0;j<resIndex+resCount;j++,f++)
        {
            try
            {
                Log.i("rest buttons","XXXXX"+j);
                String name=a.getResourceName(j);
                name=name.substring(name.indexOf(':')+1);
                name=name.substring(9);
                if(name.startsWith("rest"))
                {
                    Log.i("BUTTON NAME",name);
                    buttons[f]=new ImageButton(this);
                    buttons[f].setImageDrawable(a.getDrawable(j));
                    buttons[f].setOnClickListener(new View.OnClickListener() { 
                        public void onClick(View view) { 
                            Log.i("ACTION","BUTTON CLICKED");
                        } 
                    });
                    if(gv==null)
                        Log.i("------------","GV NULL AGA");
                    else
                        Log.i("------------","GV EXISTS AGA");
                    gv.addView(buttons[f], f);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
                break;
            }
        }
        rl.addView(gv);

    }


}

//LOG
08-01 10:13:19.288: INFO/asd(223): asd
08-01 10:13:19.306: INFO/asd(223): asd
08-01 10:13:19.306: INFO/rest buttons(223): ----2130837504
08-01 10:13:19.306: INFO/rest buttons(223): ----2130837505
08-01 10:13:19.306: INFO/rest buttons(223): ----2130837506
08-01 10:13:19.317: INFO/rest buttons(223): ----2130837507
08-01 10:13:19.326: INFO/rest buttons(223): ----2130837508
08-01 10:13:19.326: INFO/rest buttons(223): ----2130837509
08-01 10:13:19.326: INFO/rest buttons(223): ----2130837510
08-01 10:13:19.326: INFO/rest buttons(223): ----2130837511
08-01 10:13:19.326: INFO/rest buttons(223): ----2130837512
08-01 10:13:19.326: INFO/rest buttons(223): ----2130837513
08-01 10:13:19.336: INFO/rest buttons(223): ----2130837514
08-01 10:13:19.336: INFO/rest buttons(223): ----2130837515
08-01 10:13:19.336: INFO/rest buttons(223): ----2130837516
08-01 10:13:19.336: INFO/rest buttons(223): rest1
08-01 10:13:19.346: INFO/rest buttons(223): ----2130837517
08-01 10:13:19.346: INFO/rest buttons(223): rest10
08-01 10:13:19.346: INFO/rest buttons(223): ----2130837518
08-01 10:13:19.346: INFO/rest buttons(223): rest11
08-01 10:13:19.346: INFO/rest buttons(223): ----2130837519
08-01 10:13:19.346: INFO/rest buttons(223): rest2
08-01 10:13:19.346: INFO/rest buttons(223): ----2130837520
08-01 10:13:19.346: INFO/rest buttons(223): rest3
08-01 10:13:19.346: INFO/rest buttons(223): ----2130837521
08-01 10:13:19.346: INFO/rest buttons(223): rest4
08-01 10:13:19.346: INFO/rest buttons(223): ----2130837522
08-01 10:13:19.346: INFO/rest buttons(223): rest5
08-01 10:13:19.346: INFO/rest buttons(223): ----2130837523
08-01 10:13:19.346: INFO/rest buttons(223): rest6
08-01 10:13:19.346: INFO/rest buttons(223): ----2130837524
08-01 10:13:19.356: INFO/rest buttons(223): rest7
08-01 10:13:19.356: INFO/rest buttons(223): ----2130837525
08-01 10:13:19.356: INFO/rest buttons(223): rest8
08-01 10:13:19.356: INFO/rest buttons(223): ----2130837526
08-01 10:13:19.356: INFO/rest buttons(223): rest9
08-01 10:13:19.356: INFO/rest buttons(223): ----2130837527
08-01 10:13:19.356: WARN/ResourceType(223): getEntry failing because entryIndex 23 is beyond type entryCount 23
08-01 10:13:19.377: INFO/LAST(223): 2130837527
08-01 10:13:19.377: WARN/System.err(223): android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f020017
08-01 10:13:19.406: WARN/System.err(223):     at android.content.res.Resources.getResourceName(Resources.java:1438)
08-01 10:13:19.406: WARN/System.err(223):     at avm.system.RestListActivity.onCreate(RestListActivity.java:45)
08-01 10:13:19.406: WARN/System.err(223):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-01 10:13:19.416: WARN/System.err(223):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-01 10:13:19.426: WARN/System.err(223):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-01 10:13:19.426: WARN/System.err(223):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-01 10:13:19.426: WARN/System.err(223):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-01 10:13:19.437: WARN/System.err(223):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 10:13:19.437: WARN/System.err(223):     at android.os.Looper.loop(Looper.java:123)
08-01 10:13:19.437: WARN/System.err(223):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-01 10:13:19.447: WARN/System.err(223):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 10:13:19.447: WARN/System.err(223):     at java.lang.reflect.Method.invoke(Method.java:521)
08-01 10:13:19.447: WARN/System.err(223):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-01 10:13:19.447: WARN/System.err(223):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-01 10:13:19.447: WARN/System.err(223):     at dalvik.system.NativeStart.main(Native Method)
08-01 10:13:19.456: INFO/------------(223): RLOUT EXISTS AGA
08-01 10:13:19.456: INFO/rest buttons(223): XXXXX2130837516
08-01 10:13:19.456: INFO/BUTTON NAME(223): rest1
08-01 10:13:19.566: INFO/------------(223): GV EXISTS AGA
08-01 10:13:19.576: WARN/System.err(223): java.lang.UnsupportedOperationException: addView(View, int) is not supported in AdapterView
08-01 10:13:19.586: WARN/System.err(223):     at android.widget.AdapterView.addView(AdapterView.java:448)
08-01 10:13:19.586: WARN/System.err(223):     at avm.system.RestListActivity.onCreate(RestListActivity.java:116)
08-01 10:13:19.596: WARN/System.err(223):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-01 10:13:19.596: WARN/System.err(223):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-01 10:13:19.596: WARN/System.err(223):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-01 10:13:19.606: WARN/System.err(223):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-01 10:13:19.606: WARN/System.err(223):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-01 10:13:19.606: WARN/System.err(223):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 10:13:19.606: WARN/System.err(223):     at android.os.Looper.loop(Looper.java:123)
08-01 10:13:19.606: WARN/System.err(223):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-01 10:13:19.606: WARN/System.err(223):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 10:13:19.606: WARN/System.err(223):     at java.lang.reflect.Method.invoke(Method.java:521)
08-01 10:13:19.606: WARN/System.err(223):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-01 10:13:19.606: WARN/System.err(223):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-01 10:13:19.606: WARN/System.err(223):     at dalvik.system.NativeStart.main(Native Method)
08-01 10:13:19.627: INFO/update(223): lan
08-01 10:13:20.016: INFO/ActivityManager(51): Displayed activity avm.system/.RestListActivity: 5954 ms (total 5954 ms)

等待你的帮助!!!谢谢!!!

2 个答案:

答案 0 :(得分:0)

第一个异常会告诉您R文件中提到的但res文件夹中没有的资源。这意味着您应该清理并重建项目以重新生成R文件。希望这有点帮助。

答案 1 :(得分:0)

是的,首先要清理你的项目。此代码的for (i=0x7f020000 ;done&&i<0x7f020040;i++)部分也非常危险,因为您的资源ID将在每次构建后更改,并且在项目中添加另一个ID后,大多数情况下肯定会失败。

您可能还想重新考虑您的设计。为什么在ListView中使用按钮?您应该只放置图像并在Activity中实现onItemClickListener界面,并在用户单击ListView中的项目时拦截`onItemClick(...)方法。