Google's Gridview tutorial, problem with OnItemClickListener

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Google's Gridview tutorial, problem with OnItemClickListener

Postby barryman9000 » Tue Feb 09, 2010 7:32 am

I pretty much copied the code from the Google GridView tutorial (http://developer.android.com/resources/ ... dview.html) but changed some variable names. The only problem I'm having is the OnItemclickListener() in the following code keeps giving me errors.

GridView _gv = (GridView) findViewById(R.id._gvPictures);
_gv.setAdapter(new ImageAdapter(this));

_gv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> _parent, View _view, int _position, long _id){
Toast.makeText(HelloGridView.this, "" + _position, Toast.LENGTH_SHORT).show();
}
});

Hovering over the error tells me to import the android.widget.AdapterView namespace, but that actually causes more errors! After importing that namespace the "View" object is underlined. I tried fully qualifying that method, but it gives me the same error as importing the namespace.

In the console output I'm seeing an error message that I just don't understand:
"[i]Attempt to include a core VM class in something other than a core library.
It is likely that you have attempted to[/i]" blah, blah, blah you probably already know what it is.

Can anyone offer a suggestion? I'm totally new to this.

Thanks!
barryman9000
Freshman
Freshman
 
Posts: 4
Joined: Tue Feb 09, 2010 7:19 am
Location: California

Top

Postby barryman9000 » Tue Feb 09, 2010 6:11 pm

Now I'm guessing there is something wrong with the way I set up my project. Is the above error indicative of a setup error?

I don't think it's the code, because its exactly the same as the tutorial example... I have a hard time believing Google messed up their sample code, or at least, I haven't heard anyone else complaining.
barryman9000
Freshman
Freshman
 
Posts: 4
Joined: Tue Feb 09, 2010 7:19 am
Location: California

Re: Google's Gridview tutorial, problem with OnItemClickList

Postby azraeal » Tue Feb 09, 2010 11:03 pm

barryman9000 wrote:
In the console output I'm seeing an error message that I just don't understand:
"Attempt to include a core VM class in something other than a core library.
It is likely that you have attempted to
" blah, blah, blah you probably already know what it is.


No, we don't really know what 'it' is. Please include the full message, verbatim.

Did you import and set the correct onItemClickListener? It's adapterView.onItemClickListener, right? I assume you already have the ImageAdapter defined.

Yes, if you want to use adapterView, you need to import it.

barryman9000 wrote:Hovering over the error tells me to import the android.widget.AdapterView namespace, but that actually causes more errors!


Really? First I've heard of includes generating errors (unless it's ambiguous reference).
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Re: Google's Gridview tutorial, problem with OnItemClickList

Postby barryman9000 » Wed Feb 10, 2010 12:44 am

Thanks for the reply. Here's the full error (which I thought was a popular one):

"Attempt to include a core VM class in something other than a core library. It is likely that you have attempted to include the core library from a desktop virtual machine into an application, which will most assuredly not work. If you really intend to build a core library -- which is only appropriate as part of creating a full virtual machine binary, as opposed to compiling an application -- then use the "--core-library" option to suppress this error message. If you go ahead and use "--core-library" but are in fact building an application, then please be aware that your build will still fail at some point; you will simply be denied the pleasure of reading this helpful error message. "

No idea what --core-library is and Google search didn't help.



[quote]Did you import and set the correct onItemClickListener? It's adapterView.onItemClickListener, right? I assume you already have the ImageAdapter defined.

Yes, if you want to use adapterView, you need to import it.

Really? First I've heard of includes generating errors (unless it's ambiguous reference).[/quote]

I first imported "android.widget.*" and tried to use onItemClickListener but kept seeing the red underline in Eclipse, and an error message saying it couldn't be resolved, and I should import "android.widget.adapterView." However importing "android.widget.adapterView" would then show errors for both the listener and the View object - even though I kept the android.widget.* reference (which I thought contained both View and adapterView).

If I want to use adapterView, can't I just import android.widget.* ?
barryman9000
Freshman
Freshman
 
Posts: 4
Joined: Tue Feb 09, 2010 7:19 am
Location: California

Postby azraeal » Wed Feb 10, 2010 1:47 am

Get rid of the blanket import android.widget.*; statement.

This is importing too much and will lead to ambiguous references. Import only what you need. You're not saving anything because the compiler will still extract only the needed packages in the binary.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Postby barryman9000 » Wed Feb 10, 2010 6:07 pm

I'll give that a try. Should I always try to avoid those blanket imports?
barryman9000
Freshman
Freshman
 
Posts: 4
Joined: Tue Feb 09, 2010 7:19 am
Location: California

Top

Postby jonw » Wed Feb 10, 2010 11:08 pm

Blanket imports just smell wrong to me, for the reasons azraeal mentions above.
In Eclipse just do Source -> Organise Imports and let it sort them out for you.
Also remember that keyboard shortcut, it's a good one to know ;)

Without seeing the whole class it's difficult to know what exactly is wrong...
User avatar
jonw
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Feb 10, 2010 9:00 pm
Location: London

Postby azraeal » Fri Feb 12, 2010 5:02 am

Yes, you should avoid doing blanket statements. Just check out how many different LayoutParam classes there are.
The blanket import you had imported 11 LayoutParam classes (out of 12, the last one was in the android.View package). Our compilers/IDE's are not smart enough to figure out which one you meant. Not yet anyways.

@jonw
My co-worker lives by the keyboard shortcut. ctrl-shift-o was it? He doesn't even know where they're being imported from most of the time.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 9 guests