Mapfragment InflateException和重复ID

时间:2014-04-26 22:10:29

标签: android android-fragments duplicates android-inflate mapfragment

我有一个抽屉布局,显示不同的片段。当我更改片段时一切正常,但如果我尝试访问我的mapfragment两次,则会显示以下错误:

  

04-26 22:55:28.816:E / AndroidRuntime(27576):致命异常:主要   04-26 22:55:28.816:E / AndroidRuntime(27576):   android.view.InflateException:二进制XML文件行#6:错误   inflating class fragment 04-26 22:55:28.816:E / AndroidRuntime(27576):     在   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)   04-26 22:55:28.816:E / AndroidRuntime(27576):at   android.view.LayoutInflater.rInflate(LayoutInflater.java:761)04-26   22:55:28.816:E / AndroidRuntime(27576):at   android.view.LayoutInflater.inflate(LayoutInflater.java:498)04-26   22:55:28.816:E / AndroidRuntime(27576):at   android.view.LayoutInflater.inflate(LayoutInflater.java:398)04-26   22:55:28.816:E / AndroidRuntime(27576):at   com.proj.DisMap.LocMap.onCreateView(LocMap.java:121)04-26   22:55:28.816:E / AndroidRuntime(27576):at   android.app.Fragment.performCreateView(Fragment.java:1699)04-26   22:55:28.816:E / AndroidRuntime(27576):at   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)   04-26 22:55:28.816:E / AndroidRuntime(27576):at   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)   04-26 22:55:28.816:E / AndroidRuntime(27576):at   android.app.BackStackRecord.run(BackStackRecord.java:682)04-26   22:55:28.816:E / AndroidRuntime(27576):at   android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)   04-26 22:55:28.816:E / AndroidRuntime(27576):at   android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:441)04-26   22:55:28.816:E / AndroidRuntime(27576):at   android.os.Handler.handleCallback(Handler.java:730)04-26   22:55:28.816:E / AndroidRuntime(27576):at   android.os.Handler.dispatchMessage(Handler.java:92)04-26   22:55:28.816:E / AndroidRuntime(27576):at   android.os.Looper.loop(Looper.java:176)04-26 22:55:28.816:   E / AndroidRuntime(27576):at   android.app.ActivityThread.main(ActivityThread.java:5419)04-26   22:55:28.816:E / AndroidRuntime(27576):at   java.lang.reflect.Method.invokeNative(Native Method)04-26   22:55:28.816:E / AndroidRuntime(27576):at   java.lang.reflect.Method.invoke(Method.java:525)04-26 22:55:28.816:   E / AndroidRuntime(27576):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)   04-26 22:55:28.816:E / AndroidRuntime(27576):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)04-26   22:55:28.816:E / AndroidRuntime(27576):at   dalvik.system.NativeStart.main(Native Method)04-26 22:55:28.816:   E / AndroidRuntime(27576):引起:   java.lang.IllegalArgumentException:二进制XML文件行#6:重复   id为0x7f05000e,标记为null或父ID为0xffffffff,其他片段   for com.google.android.gms.maps.MapFragment 04-26 22:55:28.816:   E / AndroidRuntime(27576):at   android.app.Activity.onCreateView(Activity.java:4971)04-26   22:55:28.816:E / AndroidRuntime(27576):at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)

这是我的抽屉布局:

public class HomeSM extends FragmentActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    Fragment fragment = null;

    public Boolean flag =true;
.....
private void displayView(int position) {
        // update the main content by replacing fragments

        switch (position) {
        case 0:         
            fragment = new HomeFragment();          
            break;
        case 1:         
            fragment = new detailfrag();

            break;
        case 2:
            fragment = new LocationListView();          
            break;
        case 3:                     

            fragment = new LocMap(); // here is the mapfragment !!!

            break;
        case 4:
            fragment = new PagesFragment();
            break;
        case 5:

            DatabaseHandler db = new DatabaseHandler(getApplicationContext());
            db.resetTables();
            Intent i = new Intent(getApplicationContext(), Login.class);

            startActivity(i);

            // closing this screen
            finish();
            break;

        default:
            break;
        }

        if (fragment != null) {

            FragmentManager fragmentManager = getFragmentManager();
            //fragmentManager.beginTransaction().hide(fragment);

            fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "No activity instantiated");
        }
    }

这是mapfragment布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

这里的mapfragment是teh class:

public class LocMap extends Fragment{

    List<HashMap<String, String>> GlobMarkers;
    private GoogleMap googleMap;
    List<HashMap<String, String>> TempmarkersList;  
    Marker marker;
    private LocationManager locationManager;
    private static final long MIN_TIME = 400;
    private static final float MIN_DISTANCE = 1000;
    ProgressDialog pDialog;
    MarkerOptions markerOptions = null;
    Context context;
    //PolygonOptions pOptions;
    Polygon polygon;
    private LatLng crd;
    private float rtn;
    public String TAG;
    LatLng latlng;
    private LocationRequest lr;
    private LocationClient lc;
    public static View inst = null;
    LatLng zoom;

    ArrayList<Circle> Circles;

    BluetoothAdapter bluetoothAdapter;
    TGDevice tgDevice;

    static int cind=0;

    Fragment frg;

    /*@Override
    public void onCreate(Bundle savedInstanceState) {
        setRetainInstance(true);

    }*/

    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            context = getActivity();

            View myFragmentView = inflater.inflate(R.layout.activity_main, container, false);

            try {
                 initilizeMap();
                } catch (Exception e) {
                Log.e("ERROR", "ERROR IN CODE: " + e.toString());
                e.printStackTrace();
            }

            return myFragmentView;

     }
....
@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        }
....

    @Override   
    public void onDestroyView() 
     {
             super.onDestroyView();

             Fragment fragment = (getFragmentManager().findFragmentById(R.id.map)); 
             if (fragment != null){ 
                 getFragmentManager().beginTransaction().remove(fragment).commit();
                 FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
                 ft.remove(fragment);
                 ft.commit();
             }
}

我在网上尝试了不同的解决方案,比如切换到supportmapfragment,但它给了我相同的错误(我的清单包含所有权限和依赖关系。我已经导入并正确添加了googlep play库)。此外,我尝试通过检查当前片段是否为空而在删除之前操作ondestroy但没有运气。我一整天都在苦苦挣扎,我很绝望,任何人都可以帮我解决这个问题。

0 个答案:

没有答案