Reputation: 2031
Hello im trying to use viewpager.
My problem is that I can't find views from my sliding views, I get NPE.
This is my code for finiding views:
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
focusedView = mPager.getChildAt(arg0);
button = (ImageButton) focusedView.findViewById(R.id.button);
button.setOnClickListener(this);
}
I get NPE on this line:
button.setOnClickListener(this);
All of my views sliding views have a button with "button" id.
I also tried this:
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
button = (ImageButton) mPager.findViewById(R.id.button);
button.setOnClickListener(this);
}
I got here NPE too.
How can I find views from views inside pageviewer?
Upvotes: 0
Views: 175
Reputation: 43023
You probably have this code in an activity or fragment.
Declare a final
variable for your button on activity or fragment, get the reference to it in onCreate
or onResume
and then you will be able to use it in onPageSelected
.
Sample (not complete) code:
public class YourActivity extends Activity {
private final Button button;
@Override
protected void onResume() {
button = (Button) findViewById(R.id.button);
}
// view pager
@Override
public void onPageSelected(int arg0) {
focusedView = mPager.getChildAt(arg0);
button.setOnClickListener(this);
}
}
Upvotes: 0
Reputation: 1058
Handle the click events inside the fragments instead, you can do it in the method public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
for example.
Upvotes: 1