The context in which you're calling findViewById()
is within the Activity. This checks the main layout that you set in setContent()
via the onCreate()
method of your Activity. If the TextView R.id.clickmap
is not part of the view that you passed to the Activity, then it won't be found.
The Activity class itself is not aware of the views being created/destroyed in the PagerAdapter, so it can't find them. In order for findViewById()
to work, you need to call it on the reference to the parent view that contains the class. It can be any parent in the View hierarchy, but it must be a parent or the view itself.
EDIT:
I don't know how you're calling showHotelMap()
, but you need to use the parent view that contains the TextView that you're looking for:
View mainView = getMainView()
TextView clickOnMap = (TextView) mainView.findViewById(R.id.clickmap);
clickOnMap.setOnClickListener(......);
Where, getMainView()
is some method in which you retrieve the reference to the view you need. How you do that is dependent on your implementation.
EDIT 2:
private class MyPagerAdapter extends PagerAdapter {
...
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View returnView = null;
switch (position) {
case 0:
returnView = showHotelContact(inflater);
break;
...
}
return returnView;
}
...
public View showHotelMap(Inflater inflater)
{
View returnView = inflater.inflate(R.layout.clickformap, null);
TextView clickOnMap = (TextView)returnView.findViewById(R.id.clickmap);
clickOnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (getApplicationContext(),ShowMap.class);
startActivity(intent);
}
});
return returnView;
}
}
In this implementation, you're inflating the View
within your method, setting it up, then returning it to the PagerAdapter
. You don't need to add it to the ViewPager because the ViewPager itself will call instantiateItem()
and add it to itself.