android - java.lang.InstantiationException: can't instantiate class ... no empty constructor -
i have found topics similar questions mine cant find answer looking far. application consists of fragmentactivity
hosts viewpageradapter
(child of fragmentpageradapter) fragment in each tab. viewpageradapter instantiated in oncreateview function of parent activity
_adapter = new viewpageradapter(getapplicationcontext() , getsupportfragmentmanager() , numoftabs , status);
the viewpageradapter implements minimum required methods getitem
, getcount
, getitemposition
my getitem
initializes different fragment each position:
@override public fragment getitem(int position) { fragment f = new fragment(); log.d("adbox",string.format("inside viewpageradapter.getitem(%s)",position)); switch(position) { case 0: log.d("adbox","all offers =="); f=fragmentalloffers.newinstance(_context); f.setretaininstance(true); break; case 1: log.d("adbox","nearby offers =="); f=fragmentnearbyoffers.newinstance(_context); //f.setretaininstance(true); break; case 2: log.d("adbox","my coupons =="); f=fragmentcoupons.newinstance(_context); f.setretaininstance(true); break; case 3: log.d("adbox","account =="); f=fragmentaccount.newinstance(_context); f.setretaininstance(true); //f=layoutlocal.newinstance(_context); break; case 4: log.d("adbox","preferences =="); f=fragmentpreferences.newinstance(_context); f.setretaininstance(true); break; default: break; } return f; }
the call setretaininstance(true)
added in effort resolve problem facing hasn't helped either.
finally each of fragments above implement public static newinstance() function application context argument. example fragmentnearbyoffers contains following:
public static android.support.v4.app.fragment newinstance(context ctx) { fragmentnearbyoffers f = new fragmentnearbyoffers(); ctx = context; //bundle bdl = new bundle(); return f; }
one more important information parent activity declared singleinstance , keep reasons.
everything works fine @ point when activity in background time , try return either via taskmanager or clicking on application icon exception
android.support.v4.app.fragment$instantiationexception: unable instantiate fragment com.advisor.fragmentnearbyoffers$1: make sure class name exists, public, , has empty constructor public
the class name exists, it's public , doesn't have constructor having empty one.. added empty constructor explicitly didn't either, although verified called.
from understood various posts here android when resuming application, placing in fragmentpageradapter new instances of fragments not linked original activity.. verified because when calling getactivity inside fragment receive null.. don't understand why getting exception since there empty constructor... don't know fix this, since execution enters oncreate of activity, goes empty constructors of fragments , exception.. other methods of fragments i.e. onattach, oncreate etc not called @ all..so seems it's crashing when constructing fragments..
i attaching whole stacktrace getting in case helps:
java.lang.runtimeexception: unable start activity componentinfo{com.advisor/com.advisor.adboxwidgetconfigurationfragment}: android.support.v4.app.fragment$instantiationexception: unable instantiate fragment com.advisor.fragmentnearbyoffers$1: make sure class name exists, public, , has empty constructor public @ android.app.activitythread.performlaunchactivity(activitythread.java:2110) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2135) @ android.app.activitythread.access$700(activitythread.java:140) @ android.app.activitythread$h.handlemessage(activitythread.java:1237) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4921) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) @ dalvik.system.nativestart.main(native method) caused by: android.support.v4.app.fragment$instantiationexception: unable instantiate fragment com.advisor.fragmentnearbyoffers$1: make sure class name exists, public, , has empty constructor public @ android.support.v4.app.fragment.instantiate(fragment.java:399) @ android.support.v4.app.fragmentstate.instantiate(fragment.java:97) @ android.support.v4.app.fragmentmanagerimpl.restoreallstate(fragmentmanager.java:1760) @ android.support.v4.app.fragmentactivity.oncreate(fragmentactivity.java:200) @ com.advisor.adboxwidgetconfigurationfragment.oncreate(adboxwidgetconfigurationfragment.java:60) @ android.app.activity.performcreate(activity.java:5206) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1094) @ android.app.activitythread.performlaunchactivity(activitythread.java:2074) ... 11 more caused by: java.lang.instantiationexception: can't instantiate class com.advisor.fragmentnearbyoffers$1; no empty constructor @ java.lang.class.newinstanceimpl(native method) @ java.lang.class.newinstance(class.java:1319) @ android.support.v4.app.fragment.instantiate(fragment.java:388)
notice $1
@ end of error. reference anonymous class, not fragment named fragmentnearbyoffers
:
unable instantiate fragment com.advisor.fragmentnearbyoffers$1
since fragments require default constructor, , anonymous classes can never provide one, fragments must named class. java language specification, section 15.9.5.1 states:
an anonymous class cannot have explicitly declared constructor.
this section explains constructors generated automatically, according context in anonymous class declared. of these constructors have parameters, have different signature default constructor. combined effect anonymous classes can never have constructor matches default constructor's signature.
you can either declare fragment class in it's own file or declare static nested class:
public static class nestedfragment extends basefragment { ...
both of methods should work fine.
Comments
Post a Comment