Reputation: 593
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
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
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
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
Reputation: 15477
Intent myIntent = new Intent(yourActivity.this, Vol.class);
startActivityForResult(myIntent, 0);
Upvotes: 0