6

Tworzę aplikację z zakładką i mam mapę na jednej z kart, gdy otwieram mapę z niej działa dobrze i kiedy odwiedzam inną zakładkę, to czas też działa dobrze , ale kiedy wrócę do karty mapy, aplikacja zawiesza się z tym błędem.Google Map V2 błąd Androida Nadpisanie błędu fragmentu klasy

04-09 14:10:43.866: E/AndroidRuntime(28184): android.view.InflateException: Binary XML file line #42: Error inflating class fragment 
04-09 14:10:43.866: E/AndroidRuntime(28184): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) 
04-09 14:10:43.866: E/AndroidRuntime(28184): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 
04-09 14:10:43.866: E/AndroidRuntime(28184): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742) 
04-09 14:10:43.866: E/AndroidRuntime(28184): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
04-09 14:10:43.866: E/AndroidRuntime(28184): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
04-09 14:10:43.866: E/AndroidRuntime(28184): at com.research.fragmenttabstudy.tabA.TodaysDealLocation.onCreateView(TodaysDealLocation.java:43) 

Oto mój kod:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     Log.d("err","todaysdeal location"); 
     Log.d("err","todaysdeal location"+inflater.toString()+" "+container.toString()+" "); 
     super.onCreateView(inflater, container, savedInstanceState); 
//  map.clear(); 
     View view = inflater.inflate(R.layout.todays_deal_location, container, 
       false); 

     Log.d("err",view.toString()); 
     back = (ImageView) view.findViewById(R.id.list); 
     back.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // TODO Auto-generated method stub 
       // Constants.passcode_chk = 0; 
       // finish(); 
       mActivity.popFragments(); 
      } 
     }); 
     return view; 
    } 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     map = ((SupportMapFragment) getFragmentManager().findFragmentById(
       R.id.mapp)).getMap(); 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

    } 

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/topbar" /> 

    <!-- <ImageView --> 
    <!-- android:id="@+id/deallistmenu" --> 
    <!-- android:layout_width="wrap_content" --> 
    <!-- android:layout_height="wrap_content" --> 
    <!-- android:layout_marginLeft="5dp" --> 
    <!-- android:layout_marginTop="9dp" --> 
    <!-- android:src="@drawable/deallist_menubtn" /> --> 

    <ImageView 
     android:id="@+id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="7dp" 
     android:layout_marginTop="7dp" 
     android:src="@drawable/map_list" /> 

    <RelativeLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/imageView1" 
     android:layout_marginTop="-4.8dp" > 

     <!-- box layout.................................................................... --> 


     <!-- box layout.................................................................... --> 

     <fragment 
      android:id="@+id/mapp" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/tv_location" 
      android:name="com.google.android.gms.maps.SupportMapFragment" /> 

     <!-- <ZoomControls --> 
     <!-- android:id="@+id/zoomControls" --> 
     <!-- android:layout_width="wrap_content" --> 
     <!-- android:layout_height="wrap_content" --> 
     <!-- android:layout_alignParentBottom="true" --> 
     <!-- android:layout_alignParentLeft="true" --> 
     <!-- android:layout_marginBottom="30dp" --> 
     <!-- android:layout_marginLeft="100dp" /> --> 

    </RelativeLayout> 

    <TextView 
     android:id="@+id/barTitle" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:gravity="center" 
     android:lines="1" 
     android:maxLines="1" 
     android:paddingLeft="50dip" 
     android:paddingRight="55dip" 
     android:singleLine="true" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@android:color/white" 
     android:textSize="18dp" /> 

</RelativeLayout> 
+0

linia check 42 w xml. lub zamieść tutaj swój xml. –

Odpowiedz

-1

Pierwszy problem widzę tutaj z kodem ty prezentowane jest ta linia:

map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapp)).getMap(); 

Próbujesz uzyskać obiekt SupportMapFragment używając zwykłego FragmentManager. To jest źle. Aby uzyskać obiekt SupportMapFragment trzeba użyć SupportFragmentManager takiego:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapp)).getMap(); 

Ale domyślam się, że masz problemy z doświadczeniem w tej chwili nie są jeszcze pochodzący z tego błędu. Myślę, że odwołujesz się do biblioteki google-play-services biblioteki niewłaściwej.

Można spojrzeć na tym blogu pisałem o tym, jak dodać Google Map API V2 do ciebie aplikacji:

Google Map API V2

UPDATE:

jeśli min SDK jest 11 następnie można zmień fragment na ten kod:

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

Ale Emil tutaj rozszerzam fragment, więc nie mogę używać getSupportFragmentManager(). –

+0

następnie uzyskać wystąpienie MapFragment zamiast SupportMapFragment. –

+0

za jaki min. Sdk piszesz aplikację? –

0

Upewnij się, że Twoja działalność exte Aktywność fragment NDS i zastąpić

map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 

i usunąć "android:layout_below="@+id/tv_location" because it is inside relative layout"

iść z tym i daj mi znać.

18

Twój problem polega na tym, że zadeklarowałeś fragment mapy Google v2 w XML, więc za każdym razem, gdy nadmuchujesz XML, tworzy on nowy fragment. Jednak identyfikator fragmentu jest zakodowany w XML, więc spróbuje utworzyć nowy fragment o tym samym identyfikatorze, który już jest uruchomiony. Dwa działające fragmenty nie mogą mieć tego samego identyfikatora, więc aplikacja ulega awarii.

Najlepszym sposobem na naprawienie tego jest programowe utworzenie fragmentu mapy. Dobry przykład tego można znaleźć w ProgrammaticDemoActivity.java część przykładowego projektu mapy Google.

Jednakże, jeśli z jakiegoś powodu musisz zadeklarować swój fragment w XML, możesz sprawić, by zadziałał, zabijając stary fragment mapy po opuszczeniu widoku, aby nowy mógł zostać utworzony.

można zrobić coś takiego:

private void killOldMap() { 
    SupportMapFragment mapFragment = ((SupportMapFragment) getSherlockActivity() 
      .getSupportFragmentManager().findFragmentById(R.id.map)); 

    if(mapFragment != null) { 
     FragmentManager fM = getFragmentManager(); 
     fM.beginTransaction().remove(mapFragment).commit(); 
    } 

} 

a następnie wywołać ją z onDetach() i onDestroyView() aby upewnić się, że stara mapa zostanie zabity.

Jak widać, jest to rodzaj hackowania, a najlepiej, gdy programujesz tworzenie aplikacji zamiast XML.

+0

nie działa dla mnie – tsil

+0

Dziękuję! Spędziłem w tym kilka godzin. – anil

+2

Powoduje to IllegalStateException, ponieważ chcielibyśmy zmienić stan fragmentu po wywołaniu metody onSaveInstance()! hack nie działa! –

2

Spróbuj tego ...

wywołać tę funkcję od onDetach() i onDestroyView(), aby zabić istnieć transakcję fragmentu.

private void commitMaps() { 
    if (null != mapFragment) { 
     getFragmentManager().beginTransaction().remove(mapFragment).commitAllowingStateLoss(); 
    } 
} 

czasem dostaniesz „IllegalStateException: Nie można wykonać tę czynność po onSaveInstanceState” jeśli commit() istnieje transakcję mapy. Aby uniknąć tego wyjątku, powinieneś użyć commitAllowingStateLoss() podczas usuwania istniejącej transakcji.

Różnica między commit() and commitAllowingStateLoss()

0

jedna mapa fragmentu jest już utworzony, a kiedy próbują wrócić do widoku mapy, jego stara się stworzyć kolejny fragment na wierzchu. Poniżej kod jest wreszcie działa dobrze dla mnie:

XML:

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

Zwróć uwagę, że użyłem SupportMapFragment zamiast MapFragment.

JAVA:

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.map); 
    if (f != null) 
     getFragmentManager().beginTransaction().remove(f).commit(); 
} 
Powiązane problemy