2014-06-13 54 views
1

我已經開始在Fragments的優點上工作。但總是我在fragmentManager得到一個NullPointer異常。總是得到空指針在fragmentManager

MainFragment.Java

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) 

我已經開始在片段其advantages.But工作總是我得到在空指針異常fragmentManager。

回答

1

MainFragment OnCreate從來沒有被稱爲你的情況,所以fManager爲空。

你應該初始化它,如果你調用startFragment靜態

+0

Ÿ已經從來沒有所謂 – Anuj

+0

因爲你不叫它 – nandeesh

+0

三江源我得到它。 還有一件事我想從我的personalInfo片段中的MainActivity訪問TextView,我該如何實現這一目標? – Anuj