我的应用中有一个Sliding Drawer
并且有HomeFragment
,FriendFragment
等片段。现在HomeFragment
有一个map
。所以当我在HomeFragment
,即它被按下然后如果我再次点击相同的按钮它崩溃。我在我的onCreateView()
View v = inflater.inflate(R.layout.home_map_activity, null, false);
logcat中的错误是:
Caused by: java.lang.IllegalArgumentException: Binary XML file line #46: Duplicate id 0x7f0a008a, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
at android.app.Activity.onCreateView(Activity.java:4722)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.example.MAPit.MAPit.HomeFragment.onCreateView(HomeFragment.java:79)
at android.app.Fragment.performCreateView(Fragment.java:1695)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
所以我查了onCreateView
HomeFragment
if (v != null) {
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null){
parent.removeView(v);
}
}
try {
v = inflater.inflate(R.layout.home_map_activity, null, false);
} catch (InflateException e) {
//I have a toast check here and it's printing with vacant map and not crashing
}
{/ 1>}
HomeFragment
但是给这个检查发生了应用程序没有崩溃但是地图没有出现在InflateException
。我调试并发现它没有给布局膨胀,即它正在抛出HomeFragment
。然后我试图在我的 public void onDestroyView() {
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
@Override
public void onPause() {
super.onPause();
if (map != null) {
map = null;
}
}
中添加此部分但又一次崩溃。
if(listview.getChildAt(selectedPosition).isEnabled())
{
listview.getChildAt(selectedPosition).setEnabled(false);
}
我试图禁用我的listview选项,但它没有奏效。我尝试过的是
Caused by: java.lang.NullPointerException
at com.example.MAPit.MAPit.SlidingDrawerActivity.displayView(SlidingDrawerActivity.java:201)
at com.example.MAPit.MAPit.SlidingDrawerActivity.onCreate(SlidingDrawerActivity.java:138)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
logcat中的错误:
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
/**
* Displaying fragment view for selected nav drawer list item
*/
private void displayView(int position) {
switch (position) {
case 0:
fragment = new HomeFragment();
getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
break;
case 1:
fragment = new Friend_Search_Fragment();
getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
break;
case 2:
fragment = new Groups_Fragment();
getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
break;
case 3:
fragment = new Friend_Request_Fragment();
getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
Bundle frienddata = new Bundle();
frienddata.putString(Commands.Notification_job.getCommand(), Commands.Friends_Request.getCommand());
fragment.setArguments(frienddata);
break;
case 4:
fragment = new Friend_Request_Fragment();
getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
Bundle groupdata = new Bundle();
groupdata.putString(Commands.Notification_job.getCommand(), Commands.Group_Join_Group.getCommand());
fragment.setArguments(groupdata);
break;
case 5:
fragment = new MyWallFragment();
getFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
break;
case 6:
Intent intent = new Intent(SlidingDrawerActivity.this,SignIn.class);
startActivity(intent);
break;
default:
break;
}
startFragment(fragment, position);
}
private void startFragment(Fragment fragment, int position) {
mDrawerLayout.closeDrawer(mDrawerLinear);
if (fragment != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container,fragment);
transaction.addToBackStack(null);
transaction.commit();
// update selected item and title, then close the drawer
if (position != -1) {
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
}
/*if(position == 0){
if(mDrawerList.getChildAt(1)!=null)
mDrawerList.getChildAt(1).setEnabled(false);
}*/
//setTitle(navMenuTitles[position]);
//mDrawerLayout.closeDrawer(mDrawerLinear);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
我的listview onclick实现代码:
{{1}}
我不知道如何防止两次点击slidedrawer列表视图中的相同项目。任何人都可以向我显示任何路径?提前致谢。
答案 0 :(得分:2)
FragmentManager fm= getFragmentManager();
int count = fm.getBackStackEntryCount();
for (int i = 0; i < count; ++i) {
fm.popBackStackImmediate();
}
您可以尝试在此之前添加上述代码 FragmentTransaction事务= getFragmentManager()。beginTransaction()行