0

I'm willing to add a dynamic number of fragments to a ViewPager in my android app, so that I'm using the following way:

The FragmentActivity:

public class Lighting extends FragmentActivity {

     private LightPageAdapter TabAdapter;
     private VerticalViewPager Tab;
     private List<Fragment> fragments = new Vector<Fragment>();

     // to add a new Fragment
     public void fragmenting() {
        Bundle page = new Bundle();
        page.putString("somedata", "somedata");
        Fragment frag = Fragment.instantiate(this,MyFragment.class.getName(),page); // this is where I got the Exception
        fragments.add(frag);
     }

     //to init the Page adapter
     public void init_page_adapter(){
        this.TabAdapter  = new LightPageAdapter(super.getSupportFragmentManager(), fragments);
        Tab = (ViewPager) findViewById(R.id.lighting_pager);
        Tab.setAdapter(this.TabAdapter);
     }

}

The PageAdapter:

public class LightPageAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> myFragments;

    public LightPageAdapter (FragmentManager fragmentManager, List<Fragment> fragments) {
        super(fragmentManager);

        myFragments = fragments;

    }

    public Fragment getItem(int i) {

        return myFragments.get(i);

    }

    @Override
    public int getCount() {

        return myFragments.size();

    }

}

The Fragment:

public class MyFragment extends Fragment {

  @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View lighting_fragment = inflater.inflate(R.layout.lighting_fragment, container, false);
          return lighting_fragment;
      }

}

The ErrorLog:

05-07 08:17:45.216: E/AndroidRuntime(1533): FATAL EXCEPTION: main
05-07 08:17:45.216: E/AndroidRuntime(1533): java.lang.NullPointerException
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.content.ContextWrapper.getClassLoader(ContextWrapper.java:122)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at com.automation.isolace.Lighting.fragmenting(Lighting.java:1301)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at com.automation.fragments.Light_Center.onViewCreated(Light_Center.java:46)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15172)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15172)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15172)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15172)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15172)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.View.measure(View.java:15172)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.Choreographer.doFrame(Choreographer.java:525)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.os.Handler.handleCallback(Handler.java:615)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.os.Looper.loop(Looper.java:137)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-07 08:17:45.216: E/AndroidRuntime(1533):     at dalvik.system.NativeStart.main(Native Method)
9
  • Please paste the error log as well. Commented May 7, 2014 at 8:51
  • Do you still get error calling constructor? new MyFragment()? That's all instantiate does really, plus it calls set args with that bundle, but personally I'd do that over the instantiate which as it takes a string name is clearly intended for loading fragments without source code dependency. Commented May 7, 2014 at 8:56
  • @Kedarnath I forgot to post it, now the question updated with it Commented May 7, 2014 at 8:59
  • @weston No, I tried to separate it , so that I'm getting all of it's parameters each one alone before calling that line and everything goes well with each parameter alone but when calling instantiate it crashes Commented May 7, 2014 at 9:00
  • 1
    Yeah, Fragment frag = new MyFragment(); frag.setArguments(page); will do the same as the Fragment.instantiate line. Commented May 7, 2014 at 9:23

2 Answers 2

2
Fragment frag = new MyFragment();
frag.setArguments(page);

Will do the same as the Fragment.instantiate line.

Instantiate should only be used when you don't have a source code dependency to the fragment. i.e. you only have a String name of the class, not access to the class itself.

The cause is unclear, but it could be that MyFragment is a v11 fragment, and you try to create it with a v4 Fragment.instantiate.

Sign up to request clarification or add additional context in comments.

Comments

1

Your Lighting activity is not properly instantiated. You probably created it with new somewhere. Never instantiate activities with new - use an Intent to instantiate if you really need to.

Specifically, the exception is because the base context of the activity is not set up. The framework would do it for you before calling activity onCreate() when instantiating with an Intent.

1 Comment

that gave me another way to solve my problem, I'm already do use Intent not new but when trying to add these Fragments I'm in a Fragmweent inside the Activity, So, using the Context of the Fragment not the Activity solved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.