1

GoogleMapオブジェクトからオブジェクトを取得しようとしていSupportMapFragmentます。これもAPIレベル8で機能させたいので、このようなソリューションはfindFragmentById機能しません。

私はこのXMLを持っています:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:background="#fff"
   android:id="@+id/map_view"
   android:layout_height="match_parent" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:map="http://schemas.android.com/apk/res-auto"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      map:cameraZoom="11"
      map:cameraTargetLat="32.1275701"
      map:cameraTargetLng="34.7983432"
      map:uiZoomControls="false"/>

   <ImageView
        android:id="@+id/button_image"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="bottom"    
        android:contentDescription="@string/back"    
        android:layout_alignParentBottom="true"
        android:clickable="true"
        android:onClick="returnToMain"
        android:background="@drawable/return_button" />
    </RelativeLayout>

アップデート:

Javaコード:

public void category(View v){
    if(isNetworkAvailable()){
        if(viewMap==null){
            setContentView(R.layout.map_view);
            viewMap=(RelativeLayout)findViewById(R.id.map_view);
            Fragment ssmf=getFragmentManager().findFragmentById(R.id.map);
            mapObject=((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            mapObject.addMarker(new MarkerOptions()
            .position(new LatLng(32.1275701, 34.7983432))
            .title("Hello world"));
        }
        else{
            setContentView(viewMap);
        }
    }
}

このエラーが発生しました:

FragmentフォームをにキャストできませんSupportMapFragment

4

2 に答える 2

2

これはAPIレベル8でも機能させたいので、findFragmentByIdのようなソリューションは機能しません。

はい、そうなります。からFragmentActivity、上記のレイアウトを膨らませた後、を呼び出しfindFragmentById(R.id.map)、結果をにキャストしSupportMapFragment、それを呼び出してオブジェクトgetMap()を取得しますGoogleMap

于 2013-01-02T19:54:53.130 に答える