8

गूगल मैप्स वी 2 में एंड्रॉयड लोड पहली बार फिर से खोल दी और उसके बाद मैं दूसरे टुकड़े जो काम करता है जब मैं वापस आ टुकड़ा मैप करने के लिए यह नीचे दिए गए कोड संलग्न crashes.I का उपयोग करने जा रहा हूँ: मैं वास्तव में किसी भी सराहना मदद। अग्रिम में धन्यवाद। fragmentMain में नक्शे के लिएएंड्रॉयड नक्शे v2 क्रैश टुकड़ा

कोड:

FragmentManager fm = MainActivity.this.getSupportFragmentManager(); 
        FragmentTransaction ft = fm.beginTransaction(); 
        Fragment fragment = null; 


        if(selectedItem.compareTo("second") == 0) { 
         fragment = new FragmentMain(); 
        } else if(selectedItem.compareTo("third") == 0) { 
         fragment = new FragmentButton(); 
        } else if(selectedItem.compareTo("first") == 0) { 
         fragment = new FragmentCheckBox(); 
        } 


        if(fragment != null) { 
         // Replace current fragment by this new one 
         ft.replace(R.id.activity_main_content_fragment, fragment); 


         ft.commit(); 
         // Set title accordingly 
         tvTitle.setText(selectedItem); 
        } 

त्रुटि:: MainActivity के लिए

public class FragmentMain extends Fragment { 
    TextView textView; 

    static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
    static final LatLng test = new LatLng(53.551, 9.993); 
    private GoogleMap map; 

    public FragmentMain() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_main, null); 
     map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

       Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG) 
        .title("Hamburg")); 

       Marker test = map.addMarker(new MarkerOptions() 
        .position(test) 
        .title("test") 
        .snippet("test") 
        .icon(BitmapDescriptorFactory 
         .fromResource(R.drawable.ic_launcher))); 

       // Move the camera instantly to hamburg with a zoom of 15. 
       map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15)); 

       // Zoom in, animating the camera. 
       map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

     return view; 
    } 
} 

कोड

 FATAL EXCEPTION: main 
android.view.InflateException: Binary XML file line #83: Error inflating class fragment 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
    at com.mapsfragment.maps.FragmentMain.onCreateView(FragmentMain.java:32) 

उत्तर

11

बस OnDestroyView() पर इस कोड

public void onDestroyView() 
{ 
    super.onDestroyView(); 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map)); 
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
    ft.remove(fragment); 
    ft.commit(); 
} 
+0

इस रूप में हम onSaveINstance के बाद टुकड़ा के राज्य() बदलने के लिए कहा जाता है की कोशिश कर रहा हो जाएगा IllegalSTateException का कारण बनता है! –

+0

इस ft.commitAllowingStateLoss() का प्रयास करें; – Hardik

+0

इस एपीआई की समस्या> 11 तुम यहाँ http://stackoverflow.com/questions/7469082/getting-exception-illegalstateexception-can-not-perform-this-action-after-onsa देख सकते है – Hardik

5

अपने नक्शे टुकड़ा की शुरुआत में इस प्रयास करें onCreateView

के बाद यह वहाँ (भी एक सार्वजनिक स्थैतिक ध्वज के साथ) एक सार्वजनिक स्थिर दृश्य के साथ वर्ग बनाने
+0

पुन: लोड मैं ऊपर कोड के भीतर किसी भी कोड जोड़ने की जरूरत की तुम्हारी यह इस कोड को नक्शे के ऊपर ऊपर कोड को पेस्ट करके की कोशिश की कर = ((SupportMapFragment) getFragmentManager()। FindFragmentById (R.id.map))। GetMap(); यह कोड के साथ काम नहीं कर रहा है जिसे मैंने अभी चिपकाया था। – jason

+0

यहां आपका विचार स्थिर – Daryn

+0

धन्यवाद ब्रेडबिन के रूप में घोषित किया जाना चाहिए। – user3819810

2

एक ही समस्या समझे शब्दों में कहें, लेकिन तय .... और अपने टुकड़े के अंदर, इस कोड को रखें:

View view; 
    if(Global.flag==0){ 
      view = inflater.inflate(R.layout.fragment_main, null); 
      Global.v = view; 
      Global.flag=1; 
    } 
    else{ 
     view = Global.v; 
    } 

ps: वैश्विक स्थिर वर्गों के साथ वैश्विक वर्ग है। इस तरह आप केवल एक बार फुलाएंगे और राज्य को अन्य समय में वसूली के लिए बचाएंगे।

मुझे नहीं पता कि यह एक अच्छा प्रैक्टिक है, लेकिन मेरी समस्याओं का समाधान किया।

मेरे भयानक व्याकरण के लिए सूखी।

2

मैंने ऐसा कुछ किया था

देखें v;

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    if (v != null) { 
     ViewGroup parent = (ViewGroup) v.getParent(); 
     if (parent != null) { 
      parent.removeView(v); 
     } 
    } 

    try { 
     v = inflater.inflate(R.layout.frag_admin_tab1, container, false); 

     // Getting Map for the SupportMapFragment 
     MapFragment mf = (MapFragment) this.getFragmentManager() 
       .findFragmentById(R.id.google_map); 
     GoogleMap map = mf.getMap(); 
     map.setMyLocationEnabled(true); 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

     TextView tv = (TextView) v.findViewById(R.id.textView_Road); 
     tv.setText("Road"); 

     LatLng pnts[] = new LatLng[4]; 

     pnts[0] = new LatLng(0, 0); 
     pnts[1] = new LatLng(10, 0); 
     pnts[2] = new LatLng(10, 10); 
     pnts[3] = new LatLng(0, 10); 

     PolygonOptions poly = new PolygonOptions().add(pnts) 
       .fillColor(Color.argb(100, 255, 50, 50)) 
       .strokeColor(Color.GRAY).strokeWidth(0.5f); 
     map.addPolygon(poly); 
    } catch (InflateException e) { 
    } 

    v.setBackgroundColor(Color.WHITE); 

    return v; 
} 
+0

यहाँ आपके विचार घोषित किया जाना चाहिए एक स्थिर के रूप में – Daryn

4

मेरे मामले में, मैंने इस कोड को ऑन पॉज़() में रखा है, क्योंकि मेरे पास एक ही गतिविधि में अलग-अलग टुकड़े थे।

@Override 
public void onPause() { 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_tab)); 
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
    ft.remove(fragment); 
    ft.commit(); 
    super.onPause(); 
} 
0

खंड के लिए मेरा समाधान।

// ढेर से वर्तमान मानचित्र टुकड़ा दुर्घटना को रोकने के हटाने जबकि

@Override 
public void onDetach() { 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.mapView)); 
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
    ft.remove(fragment); 
    ft.commit(); 
    super.onDetach(); 
} 
संबंधित मुद्दे