I have used ViewPager in FragmentActivity. I am getting a crash when orientation changes.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
context = this;
loadInitialData();
setActionBar();
initializeUiComponents();
}
private void initializeUiComponents() {
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
}
My data is coming dynamically from the database, and I have to call the Sysnctask which is getting the datat from database in onResume(). Here is onPostExecute() of the Asynctask.
protected void onPostExecute(Void result) {
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
}
mSectionsPagerAdapter.notifyDataSetChanged();
}
}
Adding FragmentAdapterCode:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new PayeeListSectionFragment();
Bundle args = new Bundle();
args.putInt(PayeeListSectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return string;
}
}
Adding Fragment Code:
public class PayeeListSectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public PayeeListSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = null;
\\ rootView inflated
return rootView;
}
}
The xml is used is:
<android.support.v4.view.ViewPager
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PayeeListActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
I am getting the following crash on orientation change:
06-07 11:01:57.834: E/AndroidRuntime(766): FATAL EXCEPTION: main
06-07 11:01:57.834: E/AndroidRuntime(766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.walletv2.activity/com.walletv2.activity.PayeeListActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment: make sure class name exists, is public, and has an empty constructor that is public
06-07 11:01:57.834: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.app.ActivityThread.access$700(ActivityThread.java:141)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.os.Looper.loop(Looper.java:137)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-07 11:01:57.834: E/AndroidRuntime(766): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 11:01:57.834: E/AndroidRuntime(766): at java.lang.reflect.Method.invoke(Method.java:511)
06-07 11:01:57.834: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-07 11:01:57.834: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-07 11:01:57.834: E/AndroidRuntime(766): at dalvik.system.NativeStart.main(Native Method)
06-07 11:01:57.834: E/AndroidRuntime(766): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment: make sure class name exists, is public, and has an empty constructor that is public
06-07 11:01:57.834: E/AndroidRuntime(766): at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
06-07 11:01:57.834: E/AndroidRuntime(766): at com.walletv2.activity.PayeeListActivity.onCreate(PayeeListActivity.java:78)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.app.Activity.performCreate(Activity.java:5104)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-07 11:01:57.834: E/AndroidRuntime(766): ... 12 more
06-07 11:01:57.834: E/AndroidRuntime(766): Caused by: java.lang.InstantiationException: can't instantiate class com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment; no empty constructor
06-07 11:01:57.834: E/AndroidRuntime(766): at java.lang.Class.newInstanceImpl(Native Method)
06-07 11:01:57.834: E/AndroidRuntime(766): at java.lang.Class.newInstance(Class.java:1319)
06-07 11:01:57.834: E/AndroidRuntime(766): at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
06-07 11:01:57.834: E/AndroidRuntime(766): ... 19 more
ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .
So a Viewpager is an android widget that is used to navigate from one page to another page by swiping left or right using the same activity. So when we use Viewpager, we can add different layouts in one activity and this can be done by using the fragments. Famous applications like WhatsApp, Snapchat uses Viewpager.
ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.
There's a couple of hints on the following lines:
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment: make sure class name exists, is public, and has an empty constructor that is public
And:
06-07 11:01:57.834: E/AndroidRuntime(766): Caused by: java.lang.InstantiationException: can't instantiate class com.walletv2.activity.PayeeListActivity$PayeeListSectionFragment; no empty constructor
That suggests that you've added a parameterized constructor to your PayeeListSectionFragment
inner class. Unfortunately, that won't work, as Android relies on invoking a non-parameterized constructor using reflection in order to restore a fragment's state on configuration changes (amongst other things).
If you want to supply one or more parameters to a fragment, you'll have to plug them into a Bundle
and set it as argument, using setArguments(Bundle)
. Have a look at the DetailsFragment
and CountingFragment
in the documentation for an example on how to do that.
This problem can also arise in another situation, which may be less obvious for those not too familiar with Java. If your fragment is a nested class of say some Activity
, make sure you declare the inner class static
. For example, in the context of above question, it should be a public static class PayeeListSectionFragment
(with an emphasis on the static
modifier). That way the inner class won't keep a reference to the outer class and can have its own life cycle, without any dependencies on the outer class. Without the static modifier, the nested class can't be instantiated without instantiating the outer class, which means Android will run into issues when attempting to reinstantiate your fragment class.
An alternative solution would be to move the inner class to its own .java
file. That way any outer class dependencies are automatically removed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With