Duc
Duc

Reputation: 501

ListView onItemClick which implements Activity is not working

I have a listview extends Activity and use custom adapter because I want icon and text on the listview.

String[] options = res.getStringArray(R.array.menu_title);
    TypedArray icons = res.obtainTypedArray(R.array.menu_icon);
    ImageAndTextAdapter iadapter = new ImageAndTextAdapter(ctx, R.layout.row, options, icons);
    MenuList.setAdapter(iadapter);

Then I use the setOnItemClickListener to make the listview clickable but fail.

MenuList.setOnItemClickListener(new ListView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
            Intent myIntent = new Intent(Menu.this, Revision.class);    
            Menu.this.startActivity(myIntent);
            }
    }); 

Here is the logcat:

11-22 16:55:17.419: E/AndroidRuntime(2056): FATAL EXCEPTION: main 11-22 16:55:17.419: E/AndroidRuntime(2056): java.lang.ClassCastException: android.widget.LinearLayout 11-22 16:55:17.419: E/AndroidRuntime(2056): at cs.ucl.cw.Menu$1.onItemClick(Menu.java:54) 11-22 16:55:17.419: E/AndroidRuntime(2056): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 11-22 16:55:17.419: E/AndroidRuntime(2056): at android.widget.ListView.performItemClick(ListView.java:3513) 11-22 16:55:17.419: E/AndroidRuntime(2056): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1849) 11-22 16:55:17.419: E/AndroidRuntime(2056): at android.os.Handler.handleCallback(Handler.java:587) 11-22 16:55:17.419: E/AndroidRuntime(2056): at android.os.Handler.dispatchMessage(Handler.java:92) 11-22 16:55:17.419: E/AndroidRuntime(2056): at android.os.Looper.loop(Looper.java:130) 11-22 16:55:17.419: E/AndroidRuntime(2056): at android.app.ActivityThread.main(ActivityThread.java:3835) 11-22 16:55:17.419: E/AndroidRuntime(2056): at java.lang.reflect.Method.invokeNative(Native Method) 11-22 16:55:17.419: E/AndroidRuntime(2056): at java.lang.reflect.Method.invoke(Method.java:507) 11-22 16:55:17.419: E/AndroidRuntime(2056): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 11-22 16:55:17.419: E/AndroidRuntime(2056): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 11-22 16:55:17.419: E/AndroidRuntime(2056): at dalvik.system.NativeStart.main(Native Method)

AIM: clickable listview which would bring up other activities. Thanks guys

Upvotes: 0

Views: 479

Answers (1)

GrimmRanger
GrimmRanger

Reputation: 428

It appears that your problem might actually be in Revision.class. Without seeing that code, i cant say for sure, but the part of the Logcat that points to that would be:

 java.lang.ClassCastException: android.widget.LinearLayout

Upvotes: 0

Related Questions