java - Android - Create a instance of a class and calling from String -


i trying create instance of class fragment activity in android.

the main fragment (what calling adapter) this-

    import android.os.bundle;     import android.support.annotation.nullable;     import android.support.v4.app.fragment;     import android.view.layoutinflater;     import android.view.view;     import android.view.viewgroup;      public class tab3 extends fragment {          @override         public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {             view v = inflater.inflate(r.layout.tab_3,container,false);             return v;         }     } 

and doing in fragment adapter calling class.

so if calling it-

tab3 tab3 = new tab3(); return (fragment) tab3; 

and working fine. need dynamic, because need have dynamic tabs names stored in string.

so, found solution question answer-

creating instance string in java

and trying this-

            string classname = "tab3";             object tab3 = null;             try {                 tab3 = class.forname(classname).newinstance();             } catch (instantiationexception e) {                 e.printstacktrace();             } catch (illegalaccessexception e) {                 e.printstacktrace();             } catch (classnotfoundexception e) {                 e.printstacktrace();             }             return (fragment) tab3; 

it showing error when going tab/fragment. error log is-

07-14 16:13:10.952    1069-1069/com.appnucleus.slidingtabbedview e/androidruntime? fatal exception: main java.lang.nullpointerexception         @ android.support.v4.app.fragmentstatepageradapter.instantiateitem(fragmentstatepageradapter.java:116)         @ android.support.v4.view.viewpager.addnewitem(viewpager.java:869)         @ android.support.v4.view.viewpager.populate(viewpager.java:1085)         @ android.support.v4.view.viewpager.populate(viewpager.java:951)         @ android.support.v4.view.viewpager$3.run(viewpager.java:250)         @ android.os.handler.handlecallback(handler.java:605)         @ android.os.handler.dispatchmessage(handler.java:92)         @ android.os.looper.loop(looper.java:137)         @ android.app.activitythread.main(activitythread.java:4424)         @ java.lang.reflect.method.invokenative(native method)         @ java.lang.reflect.method.invoke(method.java:511)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551)         @ dalvik.system.nativestart.main(native method) 

can me please?

class.forname() needs qualified name - is, name of package class contained in, plus simple name of class itself.

assuming package containing class called com.your.package, code have be:

string classname = "com.your.package.tab3";  // change here object tab3 = null; try {     tab3 = class.forname(classname).newinstance(); } catch (instantiationexception e) {     e.printstacktrace(); } catch (illegalaccessexception e) {     e.printstacktrace(); } catch (classnotfoundexception e) {     e.printstacktrace(); } return (fragment) tab3; 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -