Unable to instantiate fragment com. viewpager. demo. FirstFragment: make sure class name exists, is pub

Source: Internet
Author: User

Respect originality:Http://blog.csdn.net/yuanzeyao/article/details/40922875


Recently, the project often encounters rotating the screen or the home key to the background, and then restoring the foreground, causing the application to crash. The Crash log is shown in the title. From the log information, the cause of the crash is that the Fragment does not provide a default constructor (no parameter constructor). After in-depth analysis, it is found that Fragment exists in the Activity and Fragment does not have a parameter constructor, this crash is very likely to occur because the Activity will be destroyed and rebuilt during screen rotation, and the Fragment on Actiivty will be destroyed at this time, however, when the Activity is restored, Fragment will also be restored, and Fragment will be called at this time. instantiate function. This function calls the default constructor of Fragment through reflection. Therefore, once our Fragment does not provide the default constructor, the application will crash.

In fact, on Google's official website, Fragment is recommended not to use constructors with bleak values. Instead, use a static newInstance method to pass the value to be initialized in Fragment through the newInstance method.

Public class FirstFragment extends Fragment {private static final String TAG = "FirstFragment"; private int tmpValue; private MyViewPager parent; /*** static engineering method ** @ return */public static FirstFragment newInstance (Serializable parent) {FirstFragment fragment = new FirstFragment (); Bundle bundle = new Bundle (); bundle. putSerializable ("parent", parent); fragment. setArguments (bundle); return fragment;} @ O Verridepublic void onSaveInstanceState (Bundle outState) {// TODO Auto-generated method stubsuper. onSaveInstanceState (outState); Log. d ("yzy", "FirstFragment --> onSaveInstanceState"); outState. putSerializable ("parent", this. parent); outState. putSerializable ("tmp", tmpValue) ;}@ Override public void onCreate (Bundle savedInstanceState) {Log. d ("yzy", "FirstFragment --> onCreate and saveDInstancesState is null- > "+ (SavedInstanceState = null); super. onCreate (savedInstanceState); Bundle bundle = this. getArguments (); Log. d ("yzy", "bundle -->" + bundle); Log. d ("yzy", "getArguments is null -->" + (bundle = null); this. parent = (MyViewPager) bundle. getSerializable ("parent"); if (savedInstanceState! = Null) {Log. d ("yzy", "parent is null and get from savedInstanceState"); this. tmpValue = (MyViewPager) savedInstanceState. getSerializable ("tmp") ;}@ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view View = inflater. inflate (R. layout. fragment_first, container, false); return view ;}}

In the newInstance function, the setArgument method is used to pass the parameter to Fragment. This parameter is taken out in the onCreate method of Fragment and assigned to the member variable. When Fragmetn is restored, in onCreate, we can still get the value from getArgument and assign values to the attribute. However, for some properties not initialized in the constructor, such as tmpValue in this example, if Fragment is destroyed, we can save this value in onSaveInstanceState and restore it in onCreate. This prevents the loss of important attributes when Fragment is destroyed...

Unable to instantiate fragment com. viewpager. demo. FirstFragment: make sure class name exists, is pub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.