So I'm trying to use a viewpager to swipe between screens. I'm trying to do this without using a fragment but I cannot get it to work. I'm getting an error on line 59 which is mViewPager.setAdapter(mCustomPagerAdapter); for a null pointer exception when I try to set my adapter. I was going off of an example I found on here. I think I'm in over my head with this, this is my first time trying to use a view pager.
heres my Route details activity where I'm trying to set the adapter
public class RouteDetails extends AppCompatActivity {
CustomPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
dbHandler = new MyDBHandler(this, null, null, 1);
//route detail xml array info
final CharSequence[] routeDetail= getResources().getTextArray(R.array.routeDetail);
///array of route images
final TypedArray image = getResources().obtainTypedArray(R.array.routeImages);
///////
mCustomPagerAdapter = new CustomPagerAdapter(this, routeDetail, image);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
///////
}
and heres my CustomPagerAdapter
class CustomPagerAdapter extends PagerAdapter {
CharSequence[] routeDetail;
TypedArray routeImageId;
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context, CharSequence[] routeDetail, TypedArray routeImageId) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.routeDetail = routeDetail;
this.routeImageId = routeImageId;
}
@Override
public int getCount() {
return routeDetail.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.activity_route_details, container, false);
TextView routeText = (TextView) itemView.findViewById(R.id.routeDetailsView);
routeText.setText(routeDetail[position]);
final int imageId = routeImageId.getResourceId(position, 0);
ImageView imageView = (ImageView) itemView.findViewById(R.id.routeImage);
imageView.setImageResource(imageId);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
Thanks for any help!
edit: adding relvant xml files
activity_route_details.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_route_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zach.BirdsboroClimbing.RouteDetails">
<CheckBox
android:text="Route Climbed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/routeCheckBox"
android:gravity="center" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/routeDetailsView"
android:textSize="18sp"
android:textAlignment="center"
android:textColor="@android:color/black"
android:layout_below="@id/routeCheckBox"/>
<ImageView
android:layout_below="@id/routeDetailsView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/routeImage"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="true" />
</RelativeLayout>
</ScrollView>
pager_route_details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and routeListViewItems.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="routeDetail">
<item><b>Shark Bait - 5.9</b>\n\n \u25BA 2 draws + 2 for anchor.\n</item>
<item><b>Rain Check - 5.8</b>\n\n \u25BA 3 draws + 2 for anchor.\n</item>
<item><b>Rain Check Direct - 5.8</b>\n\n \u25BA 4 draws + 2 for anchor.\n</item>
<item><b>Arocknophobia - 5.7</b>\n\n \u25BA 3 draws + 2 for anchor.\n</item>
<item><b>Balls Deep - 5.9+</b>\n\n \u25BA 3 draws + 2 for anchor.\n</item>
<item><b>Jingle Bells - 5.9</b>\n\n \u25BA 4 draws + 2 for anchor.\n</item>
<item><b>Itching to Climb - 5.8</b>\n\n \u25BA 5 draws + 2 for anchor.\n</item>
</string-array>
<integer-array name="routeImages">
<item>@drawable/sharkbait</item>
<item>@drawable/raincheck</item>
<item>@drawable/raincheckdirect</item>
<item>@drawable/arocknophobia</item>
<item>@drawable/ballsdeep</item>
<item>@drawable/jinglebells</item>
<item>@drawable/itchingtoclimb</item>
</integer-array>
</resources>
The way I want it to work is, I have a listview main activity, when you select an item in the list view it opens up the corresponding position of the route detail view pager activity, from there I want to be able to swipe to the previous or next item.
Thank you!
You are setting ur activity layout as activity_route_details.xml which does not contain viewpager item but pager_route_details
In RouteDetails.java change this line
setContentView(R.layout.activity_route_details);
with
setContentView(R.layout.route_details);