I would like to call one of the activities that I have written in my android app, when I press an item in a ListView. The code that I want to use is this:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
.
开发者_JAVA技巧 .
.
Intent lp = new Intent(this, myFirst.class);
startActivity(lp);
}
});
However it I get error related to the following two lines:
Intent lp = new Intent(this, myFirst.class);
startActivity(lp);
basically I want to call "myFirst" activity when an item on my list view is pressed. What is the correct way of calling "myFirst" activity here?
Thanks for the help.
TJ
You are getting this error because you are passing an instance of OnItemClickListener
to your intent.
Try to pass the instance of Actiity
Intent lp = new Intent(YourActivity.this, myFirst.class);
startActivity(lp);
I think I can help you in this. 'this' may not be refering to your current activity.
Instead of 'this', use the following.
NameOfTheActivityThatIsCurrentlyOpen.this(i.e., the filename.this). This always works.
you can do like this way
Intent lp = new Intent(<filename>.this,myFirst.class);
or
Intent lp = new Intent(getApplicationContext(),myFirst.class);
lp.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(lp);
精彩评论