Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Implementing Designs for Android Updating Other GridViews Selecting GridView Items

Julio Martin Hernandez
Julio Martin Hernandez
6,355 Points

OnClickListener stuck

When I'm creating a new variable for OnClickListener, and error shows that I should import a class and put AdapterView.OnItemClickListener instead of the ordinary OnItemClickListener. I was also asked to cast a parameter to 'android.view.View.OnClickListener'. There are no more error, but it crashes when I try to run the Edit Friends. Please, help!

45 mGridView = (GridView) findViewById(R.id.friendsGrid);
46 mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
47 mGridView.setOnClickListener( mOnItemClickListener);
142    protected AdapterView.OnItemClickListener mOnItemClickListener =   143 new AdapterView.OnItemClickListener() {
144        @Override
145        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
146        }
 147   };

The errors in the log are:

06-27 21:10:13.577  19920-19920/com.martyawesome.ribbit.app E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.martyawesome.ribbit.app/com.martyawesome.smarty.app.ui.EditFriendsActivity}: java.lang.ClassCastException: com.martyawesome.smarty.app.ui.EditFriendsActivity$3 cannot be cast to android.view.View$OnClickListener
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: com.martyawesome.smarty.app.ui.EditFriendsActivity$3 cannot be cast to android.view.View$OnClickListener
            at com.martyawesome.smarty.app.ui.EditFriendsActivity.onCreate(EditFriendsActivity.java:47)
            at android.app.Activity.performCreate(Activity.java:5372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)

1 Answer

Julio Martin Hernandez
Julio Martin Hernandez
6,355 Points

Nevermind I figured out the answer. 47 mGridView.setOnClickListener( mOnItemClickListener); should be mGridView.setOnItemClickListener( mOnItemClickListener);