2016-05-23 11 views
0

イム私は場所のクラスのビューに膨張しようとすると、私はこのエラーを取得しておくが、私のアプリでGoogleマップの断片を入れしようとしている:適切にナビゲーションドロワー活動フラグメントにGoogleマップの断片を使用する方法

android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class fragment 
Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class fragment 
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.google.android.gms.maps.SupportMapFragment that is not a Fragment 
I want to put the maps fragment in another fragment with is loaded in the main activity with the navigation drawer. 

Here is my code loading the locations fragment the map should go into: 
@Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

     Fragment fragment = new Race(); 
     toolbar.setTitle(R.string.nav_name_race); 
     FragmentManager fragmentManager = getFragmentManager(); 

     if (id == R.id.nav_race) { 
      toolbar.setTitle(R.string.nav_name_race); 
      fragment = new Race(); 
     } else if (id == R.id.nav_pilot) { 
      toolbar.setTitle(R.string.nav_name_pilot); 
      fragment = new Pilot(); 
     } else if (id == R.id.nav_locations) { 
      toolbar.setTitle(R.string.nav_name_locations); 
      fragment = new Locations(); 
     } else if (id == R.id.nav_channelpicker) { 
      toolbar.setTitle(R.string.nav_name_channelpicker); 
      fragment = new Channelpicker(); 
     } else if (id == R.id.nav_band_overview) { 
      toolbar.setTitle(R.string.nav_name_band_overview); 
      fragment = new BandOverview(); 
     } 

     fragmentManager.beginTransaction() 
       .replace(R.id.mainframelayout, fragment) 
       .commit(); 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

場所クラス:

public class Locations extends Fragment implements OnMapReadyCallback { 

    private View view; 
    private GoogleMap map; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     this.view = inflater.inflate(R.layout.content_locations, container, false); 
     SupportMapFragment mapFragment = (SupportMapFragment) (((FragmentActivity) getActivity()).getSupportFragmentManager().findFragmentById(R.id.map)); 
     mapFragment.getMapAsync(this); 


     //set fab onClickListener 
     FloatingActionButton fabtop = (FloatingActionButton) view.findViewById(R.id.fabtop); 
     assert fabtop != null; 
     fabtop.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(view.getContext(), "top", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     FloatingActionButton fabbottom = (FloatingActionButton) view.findViewById(R.id.fabbottom); 
     assert fabbottom != null; 
     fabbottom.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(view.getContext(), "bottom", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     return this.view; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) 
    { 
     map = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     map.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 

とレイアウトファイル:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingTop="?android:attr/actionBarSize" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".MainActivity"> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fabtop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|bottom" 
     android:src="@drawable/ic_menu_mylocation" 
     app:backgroundTint="#FFFFFF" 
     android:layout_above="@+id/fabbottom" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginRight="@dimen/fab_margin" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fabbottom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|bottom" 
     android:src="@mipmap/ic_add_white_48dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_margin="@dimen/fab_margin" /> 

    <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" 
     tools:context=".MapsActivity" 
     android:name="com.google.android.gms.maps.SupportMapFragment" /> 

</RelativeLayout> 

私はマップ内の新しいアクティビティを作成するとすべて問題なく動作するため、問題はフラグメント内にフラグメントがあると思います。私はまだ断片を扱う初心者で、マップフラグメントをアプリケーションに挿入する適切な方法を知らない。

すべてのお楽しみです。

答えて

0

FragmentgetFragmentManager()があるので、フラグメント内で(SupportMapFragment) (((FragmentActivity) getActivity()).getSupportFragmentManager()を呼び出す必要はありません。ただし、ネストされたフラグメントを使用する場合は、getFragmentManager()documentationを参照)の代わりにgetChildFragmentManager()を使用する必要があります。

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
if (mapFragment != null) { 
    mapFragment.getMapAsync(this); 
} 

を、ケースには、あなたは重複ID、com.googleのための別のフラグメントとタグヌル、または親IDのような問題に直面します。

あなたの質問に来て、あなたは、単にこのようにコードを更新することができます。 android.gms.maps.MapFragmenthereで解説されているソリューションを試すことができます。それともこれを試してみてください:あなたはcom.google.android.gms.maps.MapFragmentの問題のための別の断片と重複するID、タグヌル、または親IDに直面することはありませんので、それは、レイアウトを毎回膨らまないだろう

private View mContentView; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    if (mContentView == null) { 
     mContentView = inflater.inflate(R.layout.content_locations, container, false); 
    } 
    return mContentView; 
} 

。 しかし、attachToRoot(inflater.inflateの最後の引数)にfalseを渡してください。そうでなければ、のような例外が発生する可能性があります。指定した子にはすでに親があります。

+0

解決方法を試しましたが、getChildFragmentManager()によって返されたフラグメントをSupportMapFragmentにキャストできませんでした。 – lexlix

+0

@Felix 'Locations'クラスが' android.support.v4 .app.Fragment'または 'android.app.Fragment'? – Rehan

+0

android.app.Fragment – lexlix

関連する問題