2016-03-22 42 views
3

私はアンドロイドスタジオでのプログラミングの初心者です。私は複数の断片があるナビゲーションドロワーでGoogle Maps(目的地、ルートなどを設定)を使って作業しようとしています。 Googleマップの場合、onMapReady()メソッドでマーカーを使用して地図上で作業するのに問題があったのですが、代わりにonActivityCreated()メソッドを使用する必要がありましたが、作業を開始すると次のエラーが表示されていました:変換不可能なタイプです。android.app.Fragmentcom.google.android.gms.maps.SupportMapFragmentにキャストできません。次のコード行で:mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);も私に2番目のエラーを与えます:間違った第2引数型、Found:com.google.android.gms.maps.SupportMapFragment必須:android.app.Fragmentコードの次の行に:fm.beginTransaction().replace(R.id.map, mapFragment).commit();。残念ながら、私はこの問題に対する答えや解決策を見つけることができず、あなたの助けに感謝します。ここで変換できないタイプ。 android.app.fragmentをキャストできません

はマップが配置されているフラグメントのための私の完全なコードです:

package demo.mapas; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
//import android.support.v4.app.FragmentManager; 
import android.app.FragmentManager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.example.usuario.mapas.R; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 

/** 
* Created by USUARIO on 3/12/2016. 
*/ 
public class opcion1Fragment extends Fragment { 
    private SupportMapFragment mapFragment; 




    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_option1, container, false); 

     return rootView; 

    } 

    @Override 

    public void onActivityCreated(Bundle savedInstanceState){ 
     super.onActivityCreated(savedInstanceState); 

     FragmentManager fm = getChildFragmentManager(); 
     mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map); 
     if(mapFragment == null){ 
      mapFragment = SupportMapFragment.newInstance(); 
      fm.beginTransaction().replace(R.id.map, mapFragment).commit(); 
     } else { 
      mapFragment.getMapAsync(new OnMapReadyCallback() { 

       @Override 
       public void onMapReady(GoogleMap googleMap) { 
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 

       } 
      }); 
     } 
    } 

} 

答えて

7

あなたopcion1Fragmentandroid.app.Fragment;から延びるれます。しかし、あなたはandroid.support.v4.appのクラスを使う必要があります。

  1. 変更からのインポート:import android.app.Fragment;
  2. import android.support.v4.app.Fragment;にはimport android.support.v4.app.FragmentManager;

import android.app.FragmentManager;からインポートを変更して、あなたの問題が修正されますので、あなたがする必要があります。

これが役立ちますように!

+0

問題が修正されました。深く感謝! @ g2o –

+0

助けてくれてうれしい...解決されたように質問にしてください;) –

+0

Nice Answer :)うまくいきました – Mano

関連する問題