我在我的项目中使用 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 片段设置了 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)
请问我该如何解决!任何人都可以帮助meeeee ...