I have a Sliding Drawer
in my app and have fragments like HomeFragment
,FriendFragment
etc.Now the HomeFragment
has a map
.So when i am in HomeFragment
i.e it is pressed and then if i click on the same button again it crashes.I had below code in my onCreateView()
View v = inflater.inflate(R.layout.home_map_activity, null, false);
The errors in logcat is:
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)
So i have a check in the onCreateView
of 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
}
But giving this check it happened that app is not crashing but the map is not appearing in the HomeFragment
.I debugged and saw that it is not inflating the layout i.e it is throwing InflateException
.Then i tried to add this portion in my HomeFragment
but again crashed.
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;
}
}
I have tried to disable my that listview option but it didn't worked.what i tried is
if(listview.getChildAt(selectedPosition).isEnabled())
{
listview.getChildAt(selectedPosition).setEnabled(false);
}
the error in logcat:
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)
My listview onclick implementation code:
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");
}
}
I am out of idea how to prevent twice clicking on the same item in listview of slidingdrawer.Can anyone show me any path??Thanks in advance.
FragmentManager fm= getFragmentManager();
int count = fm.getBackStackEntryCount();
for (int i = 0; i < count; ++i) {
fm.popBackStackImmediate();
}
you can try with adding above code before this FragmentTransaction transaction = getFragmentManager().beginTransaction() line