将新片段添加到ViewPager时出现NullPointerException

时间:2014-05-07 08:49:23

标签: android android-fragments android-viewpager fragmentstatepageradapter

我愿意在我的Android应用程序中向ViewPager添加动态数量的片段,以便我使用以下方式:

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);
     }

}

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();

    }

}

片段

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;
      }

}

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)

2 个答案:

答案 0 :(得分:2)

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

Fragment.instantiate行相同。

只有在您对片段没有源代码依赖时,才应使用

Instantiate。即,您只有该类的String名称,而不能访问该类本身。

原因尚不清楚,但可能是MyFragment是v11片段,您尝试使用v4 Fragment.instantiate创建它。

答案 1 :(得分:1)

您的Lighting活动未正确实例化。你可能在某处用new创建了它。永远不要使用new实例化活动 - 如果确实需要,请使用Intent进行实例化。

具体而言,例外是因为未设置活动的基本上下文。在使用onCreate()进行实例化时,框架会在调用活动Intent之前为您完成。