android – 为什么我的FragmentTransacton()或FragmentManager.beginTransaction()会遇到空指针异常

我收到了nullPointerException:

java.lang.RuntimeException: Unable to start activity ComponentInfo{---private---}: 
      java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction 
      android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction 
      android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
            at com.zero. ??? .BoxFragment.newInstance(BoxFragment.java:59)
            at com.zero. ??? .MainActivity$SectionPagerAdapter.getItem(MainActivity.java:111)
            at com.zero. ??? .MainActivity.onCreate(MainActivity.java:58)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-02 17:52:00.093    2227-2227/com.zero.mytoolroom I/Process﹕ Sending signal. PID: 2227 SIG: 9

在这种方法中

/* returns a new instance of this fragment for the given section number */
public BoxFragment newInstance(int sectionNumber) {
    Log.v(LOG_TAG, "inside newInstance()");
    manage = getFragmentManager();
    BoxFragment fragment = new BoxFragment();
    Log.v(LOG_TAG, "box fragment created");
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    transaction = manage.beginTransaction();
    transaction.add(R.id.box_layout,fragment).addToBackStack(null);
    transaction.commit();
    return fragment;
}//end newInstance() 

解决方法:

把它放在你的代码中:

FragmentManager manager = getActivity().getSupportFragmentManager()
上一篇:更新地图时Android谷歌地图上的NullPointer异常


下一篇:c# – IF语句中的运算符顺序