使用OnBackPressed()删除片段会从NullPointerException崩溃应用程序

时间:2014-02-06 18:10:15

标签: android stack fragment back

我有一个MainActivity并使用OnClickListener启动一个名为FragmentA的片段。在FragmentA内部,我使用FragmentA内部的OnArticleSelectedListener接口监听器从MainActivity启动FragmentB。

现在,我还有一个OnBackPressed(),它根据tagID删除每个片段。当我在FragmentA中时它工作正常,但是,当我启动FragmentA时,然后使用FragmentA内部的OnArticleSelected启动FragmentB以从MainActivity启动它并点击退格而我在fragmentB内部时出现nullpointexception错误:

   FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:651)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:487)
        at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:179)
        at com.example.MainActivity.onBackPressed(MainActivity.java:247)
        at android.app.Activity.onKeyUp(Activity.java:2159)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2647)
        at android.app.Activity.dispatchKeyEvent(Activity.java:2389)

这就是我启动片段A的方式:

@TargetApi(16)
public class MainActivity  extends FragmentActivity implements FragmentA.OnArticleSelectedListener {
RelativeLayout rl;
FrameLayout fl;
Button btn;
FragmentManager fragmentManager;
FragmentTransaction ft;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    fragmentManager = getSupportFragmentManager();


    setContentView(R.layout.main);


    btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {

  ...
  btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    FragmentA newFragmentA = new FragmentA();
    ft = fragmentManager.beginTransaction();
    if (newFragmentA != null) {
        ft.remove(newFragmentA);
    }
    ft.replace(R.id.layoutholder, newFragmentA, "fragA");
    ft.addToBackStack("fragA");
    ft.commit();
}   
   ...

这就是我使用FragmentA中的接口侦听器通过MainActivity启动FragmentB的方法:

  ...
   public void onArticleSelected(Uri uri) {
    FragmentB newFragmentB = new FragmentB();
    Bundle args = new Bundle();
    args.putParcelable("uri", uri);
    FragmentB.setArguments(args);
    ft = fragmentManager.beginTransaction();
    if ( newFragmentB != null) {
        ft.remove( newFragmentB );
    }
   ft.replace(R.id.layoutholder, newFragmentB , "fragB");
   //Tried ft.add and it did not work either
   //ft.add(R.id.layoutholder, newFragmentB, "fragB");
   ft.addToBackStack("fragB");
   ft.commit();

};

现在我实现了一个OnBackPressed(),它将删除每个片段,具体取决于它的片段Tag。这就是我正在做的事情,我在启动fragmentA,然后是fragmentB并点击fragmentB上的后退键后得到一个NullPointExceptionError。

OnBackPressed()代码,用于删除fragmentB:

     ...
     public void onBackPressed() {
        getCurrentFragmentName();
             if (getCurrentFragmentName() == "fragB") {
                ft =  fragmentManager.beginTransaction();
                FragmentB fragmentB = (FragmentB)fragmentManager.findFragmentByTag("fragB");
                ft.remove("fragB");
                ft.commit();

    }
   super.onBackPressed();
}

0 个答案:

没有答案