导航抽屉如何返回我的第一个片段实例

时间:2013-08-29 00:07:17

标签: android fragment android-fragmentactivity navigation-drawer

我使用SherlockNAvigationDrawer库, 在我的项目中:SherlockFragmentActivity(MainActivity):

public class MainActivity extends SherlockFragmentActivity {
private DrawerLayout mDrawerLayout;
private ListAdapter madapetr;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private MenuBean menubean;
private ArrayList<MenuBean> itemlist;
private FragmentManager fm;
private Fragment newFragment;
private boolean doubleBackToExitPressedOnce;
public static final Acceuil acceuil= new Acceuil();
public static final EklopListView ekloplistview= new EklopListView();
public static final JeDecouvre jedecouvre= new JeDecouvre();
public static final DevenirFranchise devenirfranchise= new DevenirFranchise();
public static final Contact contact= new Contact();
public String myTag=null; 
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     //Mycode here ....
    }

    private void selectItem(int position){
    fm = getSupportFragmentManager();
    newFragment = acceuil;
    myTag=Acceuil.class.getName();
    switch(position){
    case 0:
        if (newFragment!=acceuil) {
            newFragment = acceuil;
        }
        myTag=Acceuil.class.getName();
        break;
    case 1:
        if(newFragment!= ekloplistview){
            newFragment = ekloplistview;
        }
        myTag=EklopListView.class.getName();
        break;
    case 2:
        if(newFragment!=jedecouvre){
            newFragment = jedecouvre;
        }
        myTag=JeDecouvre.class.getName();
        break;
    case 3:
        if(newFragment!=devenirfranchise){
            newFragment = devenirfranchise;
        }
        myTag=DevenirFranchise.class.getName();
        break;
    case 4:
        if(newFragment!= contact){
            newFragment =contact;
        }
        myTag=Contact.class.getName();
        break;
    }

    fm.beginTransaction()
    .replace(R.id.content_frame, newFragment,myTag)
    .commit();

    mDrawerList.setItemChecked(position, true);
    setTitle(itemlist.get(position).getTitle());
    mDrawerLayout.closeDrawer(mDrawerList);
}
    }

这是我的片段Acceuil,它是我设置为Mainactivity的默认片段是Acceuil片段,我影响了myTag = Acceuil.class.getName()作为tagName片段的

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    public class Acceuil extends Fragment {@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {

        view= inflater.inflate(R.layout.acceuil, null);

    } catch (Exception e) {
        // TODO: handle exception
    }
    return view;
}

}

使用switch case我用EklopProduit替换当前片段Acceuil,如下所示:

case R.id.btn_produits:
        newFragment= new EklopProduits();
        myTag= EklopProduits.class.getName();
        fm= getActivity().getSupportFragmentManager();
        fm.beginTransaction()
        .replace(R.id.content_frame, newFragment,myTag)
        .commit();
        break;

这是我的EklopProduits片段,当我按下后退按钮时,我尝试调用我的Acceuil片段的第一个实例:

public class EklopProduits extends Fragment implements OnClickListener  {
private View view;
private Fragment newFragment;
private FragmentManager fm;
private Button btn_retour;
private RelativeLayout btn_packs,btn_eliquides,btn_consommables,btn_accesoires;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view= inflater.inflate(R.layout.produits, null);

        btn_retour=(Button) view.findViewById(R.id.btn_retour);
        btn_retour.setOnClickListener(this);

    } catch (Exception e) {
        // TODO: handle exception
    }

    return view;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_retour:
        fm=getActivity().getSupportFragmentManager();
        newFragment= fm.findFragmentByTag(Acceuil.class.getName());
        fm.beginTransaction()
        .replace(R.id.content_frame, newFragment,Acceuil.class.getName())
        .commit();
        break;

注意:我在MainActivity示例中为Acceuil fragmet设置了tagName:myTag = Acceuil.class.getName(); fm.beginTransaction()         .replace(R.id.content_frame,newFragment,myTag)         .commit();

LogCat是:

08-29 00:57:50.279: E/AndroidRuntime(20939): FATAL EXCEPTION: main
08-29 00:57:50.279: E/AndroidRuntime(20939): java.lang.NullPointerException
08-29 00:57:50.279: E/AndroidRuntime(20939):    at         android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at          android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:429)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     com.partnet.eklopmobile.EklopProduits.onClick(EklopProduits.java:59)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     android.view.View.performClick(View.java:3534)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     android.view.View$PerformClick.run(View.java:14263)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     android.os.Handler.handleCallback(Handler.java:605)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     android.os.Handler.dispatchMessage(Handler.java:92)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     android.os.Looper.loop(Looper.java:137)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     android.app.ActivityThread.main(ActivityThread.java:4441)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     java.lang.reflect.Method.invokeNative(Native Method)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     java.lang.reflect.Method.invoke(Method.java:511)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-29 00:57:50.279: E/AndroidRuntime(20939):    at     dalvik.system.NativeStart.main(Native Method)

请问我该如何解决这个问题! anyOne可以帮助我...

1 个答案:

答案 0 :(得分:0)

执行FragmentTransaction涉及replace()电话后,已更换的Fragment不再存在,因此您在尝试搜索时会获得NullPointerException。我看到的最佳解决方案是将addToBackStack(null)调用以下代码:

case R.id.btn_produits:
        newFragment= new EklopProduits();
        myTag= EklopProduits.class.getName();
        fm= getActivity().getSupportFragmentManager();
        fm.beginTransaction()
        .replace(R.id.content_frame, newFragment,myTag)
        .addToBackStack(null)
        .commit();
        break;

然后你真的不需要任何特殊的Back按钮代码,系统只会恢复上一个事务,显示第一个Fragment