总是在fragmentManager上

总是在fragmentManager上

我已经开始研究Fragments的优点。但是总是在fragmentManager上收到NullPointer异常。

主片段

public class MainFragment extends Activity {

    Context mContext;
    public static FragmentManager fManager;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initialize();



    }


    private void initialize() {
        mContext = getApplicationContext();
        fManager = getFragmentManager ();
    }

    public static void startFragment(Fragment fragment, boolean isToBackStack) {

        /*
         * Restricting Fragment transition if same fragment available in top of
         * BackStack.
         *
         * If tempFragment is Null it means there is no fragment was added yet.
         */

        Fragment tempFragment = fManager.findFragmentById(R.id.container);
        if (tempFragment != null && tempFragment.getClass().getName().equals(fragment.getClass().getName())) {
            return;
        }

        /*
         * Actual transition of Fragment
         */

        FragmentTransaction fragt = fManager.beginTransaction();
        fragt.setCustomAnimations(android.R.anim.slide_out_right,android.R.anim.slide_in_left);
        fragt.replace(R.id.container, fragment);

        // hide background fragment
        fragt.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        if (isToBackStack)
            fragt.addToBackStack(fragment.getClass().getName());
        fragt.commit();

    }
}


MainActivity.Java

public class MainActivity extends Activity {

    private FrameLayout container;
    private TextView headerid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);
        initialize ();
        MainFragment.startFragment (new PersonalInfo (),true);
    }


    private void initialize() {

        container = (FrameLayout) findViewById (R.id.container);
        headerid = (TextView) findViewById (R.id.header_id);
    }
}


Logcat

06-13 12:52:30.112  31338-31338/com.jobs_on_call_fragments E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jobs_on_call_fragments/com.jobs_on_call_fragments.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            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:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.jobs_on_call_fragments.Fragments_Details.MainFragment.startFragment(MainFragment.java:46)
            at com.jobs_on_call_fragments.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            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:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)


我已经开始研究Fragments的优点。但是总是在fragmentManager上收到NullPointer异常。

最佳答案

在您的情况下,从未调用MainFragment OnCreate,因此fManager为null。

如果要静态调用startFragment,则应初始化它

关于java - 总是在fragmentManager上获取空指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24199947/

10-10 09:27