Android ViewPager keeps crashing with NullPointerException on orientation change -
i trying add viewpager
android app one-time setup screen user. problem facing app keeps crashing when orientation of screen changed amidst setup process.
if app opened in either portrait or landscape mode , used without orientation change, works fine. if orientation changed during runtime, using method setcurrentitem(int position)
on viewpager
crashes app.
here's fragment
class -
package com.cosine.arc; import android.content.context; import android.graphics.typeface; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.textview; /** * simple {@link fragment} subclass. */ public class welcomefragment extends fragment { private int mposition; private context mcontext; private viewpager mpager; private final int[] welcomefragments = {r.layout.fragment_welcome1}; public welcomefragment() { // required empty public constructor } public welcomefragment(context context, viewpager viewpager, int position) { this.mposition = position; this.mcontext = context; this.mpager = viewpager; } @override public view oncreateview(final layoutinflater inflater, final viewgroup container, bundle savedinstancestate) { view view = null; try { switch (mposition) { case 0: view = inflater.inflate(r.layout.fragment_welcome1, container, false); typeface robotolight = typeface.createfromasset(getactivity().getassets(), "fonts/roboto-light.ttf"); textview welcometxt1x2 = (textview) view.findviewbyid(r.id.welcome_text_1_2); button startbutton = (button) view.findviewbyid(r.id.welcome_btn_1_1); startbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { mpager.setcurrentitem(1); } }); welcometxt1x2.settypeface(robotolight); } } catch (nullpointerexception e) { e.printstacktrace(); } return view; } }
and here's fragmentactivity
class fragmentstatepageradapter
class within -
package com.cosine.arc; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentstatepageradapter; import android.support.v4.view.pageradapter; public class introactivity extends fragmentactivity { private static int num_pages = 3; private nonswipeableviewpager mpager; private pageradapter mpageradapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_intro); mpager = (nonswipeableviewpager) findviewbyid(r.id.intro_pager); mpageradapter = new introslideradapter(getsupportfragmentmanager()); mpager.setadapter(mpageradapter); } @override public void onbackpressed() { if (mpager.getcurrentitem()==0) { super.onbackpressed(); } else { mpager.setcurrentitem(mpager.getcurrentitem()-1); } } public int getcurrentitem() { return mpager.getcurrentitem(); } public nonswipeableviewpager getpagerupdate() { return mpager; } private class introslideradapter extends fragmentstatepageradapter { public introslideradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { return new welcomefragment(getbasecontext(), mpager, position); } @override public int getcount() { return num_pages; } } }
and here error log -
04-07 15:25:13.774 12186-12186/com.cosine.arc e/androidruntime: fatal exception: main process: com.cosine.arc, pid: 12186 java.lang.nullpointerexception: attempt invoke virtual method 'void android.support.v4.view.viewpager.setcurrentitem(int)' on null object reference @ com.cosine.arc.welcomefragment$1.onclick(welcomefragment.java:58) @ android.view.view.performclick(view.java:5612) @ android.view.view$performclick.run(view.java:22288) @ android.os.handler.handlecallback(handler.java:751) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6123) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:867) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:757)
fragment_welcome1.xml
-
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.cosine.arc.welcomefragment"> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorprimary"> <textview android:id="@+id/welcome_text_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/welcome_to_" android:textcolor="@android:color/white" android:textsize="36sp" android:padding="16dp" android:layout_margintop="16dp" android:gravity="center"/> <imageview android:id="@+id/welcome_img_1_1" android:layout_width="150dp" android:layout_height="150dp" android:src="@drawable/ic_logo" android:layout_below="@id/welcome_text_1_1" android:layout_centerhorizontal="true"/> <textview android:id="@+id/welcome_text_1_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/welcome_img_1_1" android:text="@string/lets_get_things_started_" android:textsize="42sp" android:textcolor="@android:color/white" android:padding="16dp" android:layout_margintop="32dp"/> <button android:id="@+id/welcome_btn_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:background="@android:color/white" android:text="start" android:textcolor="@android:color/black" android:layout_margin="16dp" android:layout_alignparentright="true" android:drawableright="@mipmap/ic_arrow_right_light"/> </relativelayout>
please note have 2 different layout files fragment_welcome1.xml
, fragment_welcome1.xml-land
.
on orientation change, have save instance of fragment if want retain references. have @ answer, hope helps you. in nutshell, think need save instance of fragment in container activity's onsaveinstancestate(), , recreate saved fragment when orientation changes.
please note making config changes in manifest not recommended way save instance of fragment. making config changes lead memory leaks.
Comments
Post a Comment