android gridview problem
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!!!
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.
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.
精彩评论