开发者

Android - Intent within ListView

I am not able to create an intent in my Listview. It gives an err开发者_开发知识库or "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!


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);

        }

    }


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


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);.


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!" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜