8

Googleマップv2アンドロイドが最初にロードされ、次にマップフラグメントに戻ったときに動作する他のフラグメントに切り替わります。次のコードを添付しました:本当にありがとう助けてください。アドバンスでおめでとう。 fragmentMainでマップのフラグメントで再オープンするとアンドロイドマップv2がクラッシュする

コード: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; 
    } 
} 

コード:

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); 
        } 

エラー:

 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

これはApi> 11の問題です。ここで見ることができますhttp://stackoverflow.com/questions/7469082/getting-exception-illegalstateexception-can-not-perform-this-action-after-onsa – Hardik

5

onCreateViewマップフラグメントの最初にこれを試してみてください
if (view != null) { 
     ViewGroup parent = (ViewGroup) view.getParent(); 
     if (parent != null) { 
      parent.removeView(view); 
     } 
    } 
    try { 
     view = inflater.inflate(R.layout.fragment_main, container, false); 
    } catch (InflateException e) { 

    } 
+0

をリロードしながらクラッシュを防止するためにスタックから現在のマップ断片を除去iがyours.iの上記のコード内の任意のコードを追加する必要があります=このコードマップ上に上記のコードを貼り付けることによりそれを試し((SupportMapFragment)getFragmentManager()。findFragmentById(R.id.map))。getMap();私はちょうど貼り付けたコードで動作しません。 – jason

+0

あなたのビューは、静的 – Daryn

+0

ありがとうBreadbinとして宣言されなければなりません。 – 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:グローバルは、これらの静的変数を持つパブリッククラスです。 この方法では、1回だけ膨張させ、他の時間に回復するための状態を保存します。

これは良いpraticであるかどうかわかりませんが、私の問題を解決しました。

私の恐ろしい文法のためにうそをつく。

2

私はそのようなことをしました

View 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

私の場合、私は同じアクティビティで異なるフレグマントを持っていたので、このコードをonPause()に入れました。

@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(); 
} 
関連する問題