安卓gridview问题

发布于 2024-11-27 16:03:41 字数 10142 浏览 2 评论 0原文

我的以下代码有问题:

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)

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

I have a problem with my following code:

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)

WAITING FOR YOUR HELPS!!! THANK YOU!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2024-12-04 16:03:41

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

First exception tells you about a resource, that is mentioned in the R file, but not present in the res folder. It means that you should clean and rebuild your project to regenerate the R file. Hope this helps a bit.

凉墨 2024-12-04 16:03:41

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

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

Yes, first of all clean your project. Also this for (i=0x7f020000 ;done&&i<0x7f020040;i++) section of your code is very dangerous since your resource ids will change after each build and will mostly sure fail after you add another ID in your project.

You may also want to reconsider your design. Why use buttons in a ListView? You should place just images and implement the onItemClickListener interface in your Activity and intercept in the `onItemClick(...) method when the users clicked an item in the ListView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文