本文介绍了为什么我的 FragmentTransacton() 或 FragmentManager.beginTransaction() 会遇到空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个 nullPointerException:

I am recieving a 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()

推荐答案

把这个放在你的代码中:

Put this in your code:

FragmentManager manager = getActivity().getSupportFragmentManager()

这篇关于为什么我的 FragmentTransacton() 或 FragmentManager.beginTransaction() 会遇到空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:35