6

私はタブでアプリケーションを作成していますが、マップを開いてタブを開いたときにうまく動作し、別のタブにアクセスしたときに正常に動作しますしかし、私が地図のタブに戻ると、このエラーでアプリがクラッシュします。ここでGoogle Map V2 android errorインフラストラクチャのクラスフラグメントエラー

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) 

私のコードです:

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

チェックライン42。ここであなたのXMLを投稿してください。 –

答えて

-1

私はあなたが提示したコードをここで見る最初の問題は、この行です:

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

あなたはSupportMapFragmentオブジェクトを取得しようとしています通常のFragmentManagerを使用します。これは間違っています。 は、あなたが持っているSupportMapFragmentオブジェクトは、このようなSupportFragmentManagerを使用して取得するには、次の

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

しかし、私の推測では、あなたが今実務経験の問題はまだこの間違いに由来するものではないということです。 google-support-v4ライブラリのgoogle-play-servicesライブラリを間違った方法で参照していると思います。

あなたは私があなたのアプリケーションにGoogle Map API V2を追加する方法について書いたこのブログの記事を見てみることができます:

Google Map API V2

はUPDATE:

あなた分SDKは、あなたが缶11の場合フラグメントを次のコードに変更してください:

<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

Emilここで私は断片を拡張していますので、getSupportFragmentManager()を使用することはできません。 –

+0

次に、SupportMapFragmentの代わりにMapFragmentのインスタンスを取得します。 –

+0

あなたはアプリケーションを書いている分sdkは何ですか? –

0

あなたのアクティビティexte NDS断片の活性と

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

を交換し、これを "android:layout_below="@+id/tv_location" because it is inside relative layout"

Go]を削除し、私に知らせてください。

18

あなたの問題は、XMLでGoogleマップのv2フラグメントを宣言していることです。そのため、XMLを拡張するたびに新しいフラグメントが作成されます。ただし、フラグメントのIDはXMLでハードコードされているため、すでに実行されているものと同じIDを持つ新しいフラグメントを作成しようとします。実行中の2つのフラグメントは同じIDを持たないので、アプリケーションがクラッシュします。

これを修正する最も良い方法は、マップフラグメントをプログラムで作成することです。これの良い例は、GoogleマップのサンプルプロジェクトのProgrammaticDemoActivity.javaにあります。

ただし、何らかの理由でXMLでフラグメントを宣言する必要がある場合は、ビューを終了するときに古いマップフラグメントを強制終了して、新しいフラグメントを作成することができます。

あなたのような何かを行うことができます。

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

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

} 

をし、古いマップが殺されていることを確認するためにonDetach()onDestroyView()からそれを呼び出します。

ご覧のとおり、これはハックのようなもので、XMLを使用せずにプログラムを作成するだけの方がよいでしょう。

+0

は私のためには機能しません – tsil

+0

ありがとうございました!私はこれに数時間を費やしました。 – anil

+2

これは、onSaveInstance()が呼び出された後にフラグメントの状態を変更しようとすると、IllegalStateExceptionが発生します。ハックは機能しません! –

2

フラグメントトランザクションを存在殺すために、このonDetachから...

呼び出し、この関数を()とonDestroyViewを()してください。あなたがコミット場合():「onSaveInstanceState後にこのアクションを実行することはできませんIllegalStateExceptionが」地図トランザクションを存在

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

時にはあなたが得られます。この例外を回避するには、存在するトランザクションを削除するときにcommitAllowingStateLoss()を使用する必要があります。 commit() and commitAllowingStateLoss()

0

1つのマップフラグメント間

違いは、すでに作成され、あなたが戻ってマップページに取得しようとしているとき、そのは、その上に別のフラグメントを作成しようとしています。コードの下には最終的には私のために正常に動作している:

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"/>

私はSupportMapFragment代わりのMapFragmentを使用していることに注意してください。

JAVA:あなたのxmlで

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.map); 
    if (f != null) 
     getFragmentManager().beginTransaction().remove(f).commit(); 
} 
関連する問題