Umang
Umang

Reputation: 593

Android - Intent within ListView

I am not able to create an intent in my Listview. It gives an error "unable to instantiate activity ComponentInfo" and caused by Classcastexception. here's the snippet -

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

Intent myIntent = new Intent(view.getContext(), Vol.class);
startActivityForResult(myIntent, 0);

        }});

this is the portion of the code creating trouble -

Intent myIntent = new Intent(view.getContext(), Vol.class);
startActivityForResult(myIntent, 0);

I cant understand why its giving ClassCastException. Please help. Thanks!

Upvotes: 1

Views: 2052

Answers (4)

Aeefire
Aeefire

Reputation: 89

simply

this.startActivityForResult(myIntent, 0);

?

and don't forget to register your activity within the AndroidManifest.xml in the tag..

like :

<activity: android:name=".ExampleActivity" android:label="My ExampleActivity!" />

Upvotes: 0

Avi Kumar
Avi Kumar

Reputation: 4433

Try to give event like this

@Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
         if(position==0){
             Intent i = new Intent(this, abc.class);
             startActivity(i);

        } else if(position==1){
             Intent i = new Intent(this, xyz.class);
             startActivity(i);

        }

    }

Upvotes: 1

Andro Selva
Andro Selva

Reputation: 54330

Try passing it like this,

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

Intent myIntent = new Intent(view.getContext(), Vol.class);
view.getContext().startActivityForResult(myIntent, 0);

        }});

This might be because of context problem. If

 view.getContext().startActivityForResult(myIntent, 0);

is not working, try passing different contexts for startActivityForResult(myIntent, 0);.

Upvotes: 0

Rasel
Rasel

Reputation: 15477

Intent myIntent = new Intent(yourActivity.this, Vol.class);
startActivityForResult(myIntent, 0);

Upvotes: 0

Related Questions