1

I am trying to create a instance of a class from a fragment activity in android.

The main fragment (what I am calling from adapter) is like 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 what I am doing in my Fragment Adapter is just calling the class.

So if I am calling like it-

Tab3 tab3 = new Tab3();
return (Fragment) tab3;

And it is working just fine. But I need it to be dynamic, because I need to have dynamic tabs with names stored in a String.

So, I found a solution to do that from this question answer-

Creating an instance from String in Java

And I am trying something like 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 is showing Error when I am going to that tab/fragment. Error log is-

07-14 16:13:10.952    1069-1069/com.appnucleus.slidingtabbedview E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116)
        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:869)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
        at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4424)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

Can anyone help me please?

Community
  • 1
  • 1
Piash
  • 23
  • 1
  • 5

2 Answers2

2
Object o = Class.forName(className).newInstance(); 

But your className should be fully-qualified - i.e. com.package.MyClass

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
1

Class.forName() needs the fully qualified name - that is, the name of the package the class is contained in, plus the simple name of the class itself.

Assuming the package containing the class is called com.your.package, the code would have to 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;
FD_
  • 12,947
  • 4
  • 35
  • 62