Reputation: 1386
I am implementing an app that uses some horizontal swipe paging (to the left or to the right - Horizontal View Paging). I downloaded “android.support.v4.view.ViewPager” and found the way to implement the swiping part mostly thanks to this tutorial here: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
My problem is to view buttons and set an OnClickListenr in my swiping views. What is strange is that while I can do it within my main activity, it crashes when I try to do it from an other activity that is launched and brought to front.
While the code:
public void Settings(){
setContentView(R.layout.settings);
SettingsAdapter adapter = new SettingsAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.settingspager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
View BackButton = findViewById(R.id.back);
BackButton.setOnClickListener(this);
}
Works fine in my main activity, the exact same lines crash in “BackButton.setOnClickListener” when used in another activity.
From log I get:
02-20 09:59:50.756: E/AndroidRuntime(489): at lil.kayak.tabs.Settings$SettingsAdapter.instantiateItem(Settings.java:88)
02-20 09:59:50.756: E/AndroidRuntime(489): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
02-20 09:59:50.756: E/AndroidRuntime(489): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
02-20 09:59:50.756: E/AndroidRuntime(489): at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
02-20 09:59:50.756: E/AndroidRuntime(489): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
My pager adapter is in both cases:
private class SettingsAdapter extends PagerAdapter {
public int getCount() {
return 2;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.settings1;
break;
case 1:
resId = R.layout.settings2;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
settings.xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/settingspager"/>
</LinearLayout>
So what Is the problem? And what should I do to get it work? Thank you in advance!
Edit: I currently worked around it using the android:onClick="Function" in my xml and then
public void Function(View v){}
in my class
It works now but I do not think this is optimal so any proposals are welcome!
Upvotes: 0
Views: 964
Reputation: 1617
maybe the collection isnt a valid view or is simply "null". Better use "getApplicationContext()" or implement a constructor to the adapter that can take the Context from your application.
And, to make sure you have all needed things valid: check the "inflater" against "null", if you dont have a LayoutInflater object, things can crash.
Another reason would be your code outside from this Adapater, this we cant see here ;)
Upvotes: 1