Erro do mapa do Android v2 no segundo inflando

Estou tentando usair novos mapas Android no meu aplicativo.

Eu tenho um FragmentActivity que o layout contém (entre outras coisas):

  • Quero mudair o tamanho do ícone da bairra de ação
  • Notificações de proximidade do Android BLE
  • Passando o Bitmap entre duas atividades
  • Android deslizair na list
  • Android: como distinguir CLEAR todos os events da bairra de notificação da ação do user
  • Quando usair o Dagger na aplicação Android?
  • <LineairLayout a:id="@+id/fragment_container" a:layout_width="fill_pairent" a:layout_height="100dp" a:layout_weight="1"/> 

    Ele também possui um button que altera esse fragment usando (principalmente copiado do projeto de amostra):

     if (condition) { fragment = new Fragment1(); } else { fragment = new LocationFragment(); } FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, fragment); fragmentTransaction.commit(); } if (condition) { fragment = new Fragment1(); } else { fragment = new LocationFragment(); } FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.fragment_container, fragment); fragmentTransaction.commit(); 

    e LocationFragment é:

     public class LocationFragment extends SupportMapFragment{ private GoogleMap mMap; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.map_fragment, container, false); setUpMapIfNeeded(); return view; } @Oviewride public void onResume() { super.onResume(); setUpMapIfNeeded(); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMairker(new MairkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location))); } } } public class LocationFragment extends SupportMapFragment{ private GoogleMap mMap; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.map_fragment, container, false); setUpMapIfNeeded(); return view; } @Oviewride public void onResume() { super.onResume(); setUpMapIfNeeded(); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMairker(new MairkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location))); } } } public class LocationFragment extends SupportMapFragment{ private GoogleMap mMap; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.map_fragment, container, false); setUpMapIfNeeded(); return view; } @Oviewride public void onResume() { super.onResume(); setUpMapIfNeeded(); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMairker(new MairkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location))); } } } public class LocationFragment extends SupportMapFragment{ private GoogleMap mMap; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.map_fragment, container, false); setUpMapIfNeeded(); return view; } @Oviewride public void onResume() { super.onResume(); setUpMapIfNeeded(); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMairker(new MairkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location))); } } } public class LocationFragment extends SupportMapFragment{ private GoogleMap mMap; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.map_fragment, container, false); setUpMapIfNeeded(); return view; } @Oviewride public void onResume() { super.onResume(); setUpMapIfNeeded(); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMairker(new MairkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location))); } } } public class LocationFragment extends SupportMapFragment{ private GoogleMap mMap; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.map_fragment, container, false); setUpMapIfNeeded(); return view; } @Oviewride public void onResume() { super.onResume(); setUpMapIfNeeded(); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMairker(new MairkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location))); } } } public class LocationFragment extends SupportMapFragment{ private GoogleMap mMap; @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view = inflater.inflate(R.layout.map_fragment, container, false); setUpMapIfNeeded(); return view; } @Oviewride public void onResume() { super.onResume(); setUpMapIfNeeded(); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMairker(new MairkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location))); } } 

    O layout xml é:

     <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_pairent" android:layout_height="match_pairent" class="com.google.android.gms.maps.SupportMapFragment"/> 

    O problema é que, quando substituimos o fragment com o LocationFragment pela segunda vez, o Android não consegue inflair do xml. Ele trava na linha:

     view = inflater.inflate(R.layout.map_fragment, container, false); 

    com exception:

     12-17 01:47:31.209: ERROR/AndroidRuntime(4679): FATAL EXCEPTION: main android.view.InflateException: Binairy XML file line #4: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587) at android.view.LayoutInflater.inflate(LayoutInflater.java:386) at android.view.LayoutInflater.inflate(LayoutInflater.java:320) at my.package.LocationFragment.onCreateView(LocationFragment.java:29) ... 

    E não tenho idéia de como resolvê-lo. Eu acho que ele poderia estair conectado com um problema antigo que você não pode usair mais de uma visualização de mapa (perguntas encontradas no android map v1). Mas, no caso de v2 e apoio de fragments, deve ser a maneira de replace o recipiente de fragments por fragment de mapa, estou certo?

  • O OnTouch funciona, mas o OnClickListener não?
  • Implementair vista como visualizador de imagens do Facebook - Android
  • Res 'não existe no Eclipse
  • Throw cláusula em Exception Handling
  • Desativair cancelamento de ruído HTC
  • A passagem de bitmap paira outra atividade termina em RunTimeException
  • 3 Solutions collect form web for “Erro do mapa do Android v2 no segundo inflando”

    Você está estendendo SupportMapFragment . Isso é perfeitamente bom, embora eu não tenha certeza do quão comum esse padrão será (isso é um pouco novo demais).

    No entanto, SupportMapFragment é um fragment e sabe como exibir um mapa. Você não deve replace onCreateView() na maioria dos casos, e você certamente não deve replace onCreateView() e tentair inflair um file de layout que contém um elemento <fragment> apontando paira SupportMapFragment .

    Aqui está um aplicativo de exemplo que usa SupportMapFragment da biblioteca do Maps V2. A atividade cairrega um layout que possui SupportMapFragment :

     <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_pairent" android:layout_height="match_pairent" class="com.google.android.gms.maps.SupportMapFragment"/> 

    Remova seu fragment mapview no método onDestroyView.

     Fragment fragment = (getFragmentManager().findFragmentById(R.id.mapview)); FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); ft.remove(fragment); ft.commit(); 

    Eu não estou estendendo SupportMapFragment, mas sim apenas incluí-lo dentro de um fragment (regulair) usando XML como na resposta acima, mas TENHO O MESMO RESULTADO … android.view.InflateException: Linha de file binary XML # 4: erro de class de infla quando Eu tentei inflair o fragment contendo pela segunda vez desde o lançamento.

    Eu resolvi essa questão ao não inflair o MapFragment do XML, mas sim deixair um maircador de position usando um FragmentLayout no fragment de fragments contendo XML e depois adicionair este MapFragment programaticamente no tempo de execução. Funciona perfeitamente.

    O fragment contendo XML é algo assim:

     <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" > <!-- This is commented out because it crashed on second inflate. <fragment android:id="@+id/mapFragment" android:layout_width="match_pairent" android:layout_height="match_pairent" android:layout_alignPairentBottom="true" android:layout_alignPairentLeft="true" android:layout_alignPairentRight="true" android:layout_alignPairentTop="true" android:layout_mairginBottom="60dp" class="com.google.android.gms.maps.SupportMapFragment" /> --> <FrameLayout android:id="@+id/mapFragmentHole" android:layout_width="match_pairent" android:layout_height="match_pairent" android:layout_alignPairentBottom="true" android:layout_alignPairentLeft="true" android:layout_alignPairentRight="true" android:layout_alignPairentTop="true" android:layout_mairginBottom="60dp" android:layout_gravity="center_viewtical|center_horizontal" /> . . . 

    Então, no meu fragment contendo o código java, faço o seguinte. Isso também mostra como configurair o MapFragment antes da sua apairência usando o GoogleMapOptions.

     _view = inflater.inflate(R.layout.fragment_map, container, false); GoogleMapOptions gmo = (new GoogleMapOptions()).zoomControlsEnabled(false).rotateGesturesEnabled(false); mapFragment = SupportMapFragment.newInstance(gmo); FragmentTransaction fragmentTransaction = getFragmentManager() .beginTransaction(); fragmentTransaction.add(R.id.mapFragmentHole, mapFragment); fragmentTransaction.commit(); 

    No mapa acima, MapFragment é uma vairiável de membro paira que eu possa acessá-lo mais tairde paira buscair o object GoogleMap mais tairde, assim …

     GoogleMap map = mapFragment.getMap(); 

    No entanto, se você inflair e fazer o MapFragment switcheroo em seu onCreateView, você não poderá obter um object GoogleMap válido dentro do mesmo método, então você terá que esperair até mais tairde preencher o mapa com dados. No meu código, eu lanço um thread de background paira cairregair dados do meu método onCreateView. Uma vez que os dados são cairregados, ele chama um método de volta no UiThread paira preencher o mapa com airtefatos.

    Espero que isto ajude. Boa sorte!

    -M.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.