9

地図が出ない!私が得ることができるのはnullだけです。

これがコードです。

       public class MainActivity extends FragmentActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SupportMapFragment fragment = new SupportMapFragment();
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, fragment).commit();


       GoogleMap map;
        map = fragment.getMap();
             //here i cant access this snippet because map = null 
        if(map!=null){
        UiSettings mm = map.getUiSettings();
        map.setMyLocationEnabled(true);
          } }
           }

マニフェスト:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.anywhere_reminder"
android:versionCode="1"
android:versionName="1.0" >

    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
    <uses-feature
    android:glEsVersion="0x00020000"
     android:required="true"/>

   <permission
    android:name="com.example.anywhere_reminder.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

 <uses-permission android:name="com.example.anywhere_reminder.permission.MAPS_RECEIVE" />
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission   android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"     />

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity android:name="com.google.android.gms.BuildConfig" />
    <activity
        android:name="com.example.anywhere_reminder.MainActivity"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>

    <meta-data
     android:name="com.google.android.maps.v2.API_KEY"
   android:value=" my key "/> 

    </application>
    </manifest>

ここにxmlがあります:

  <RelativeLayout 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"
 tools:context=".MainActivity" >


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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

  </RelativeLayout>

この解決策のように解決しようとしましたGoogle Maps Android API v2 throws GooglePlayServicesNotAvailableException, out of date, SupportMapFragment.getMap() returns null .. それでも機能しませんでした

アップデート:

マップが機能するようになりました..これが編集された作業バージョンです

   public class MainActivity extends FragmentActivity {
 private GoogleMap myMap;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

          android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
          SupportMapFragment mySupportMapFragment 
           = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);


             myMap = mySupportMapFragment.getMap();
             if(myMap!=null)
            myMap.setMyLocationEnabled(true);


        }
4

5 に答える 5

17

を介して動的フラグメントを作成していFragmentTransactionます。を呼び出したときcommit()、フラグメントはまだ画面に追加されてFragmentTransactionいません。したがって、 はまだSupportMapFragment呼び出されていないため、 はありません。onCreateView()GoogleMap

<fragment>静的フラグメント (レイアウト内のタグ) に切り替えるかGoogleMap、トランザクションが処理されるまで使用を遅らせてください。

于 2013-02-16T11:52:48.313 に答える
6

FragmentManager クラスの executePendingTransactions() は、この遅延を修正するために設計されました。ドキュメントから : FragmentTransaction が FragmentTransaction.commit() でコミットされた後、プロセスのメイン スレッドで非同期に実行されるようにスケジュールされます。このような保留中の操作をすぐに実行したい場合は、この関数を (メイン スレッドからのみ) 呼び出すことができます。すべてのコールバックとその他の関連する動作はこの呼び出し内から行われることに注意してください。そのため、これがどこから呼び出されるかについて注意してください。

于 2013-02-17T01:30:13.087 に答える
3

コード スニペットを移動します。

GoogleMap マップ; マップ = fragment.getMap();

// ここでマップは null ではありません

if(map!=null){

UiSettings mm = map.getUiSettings();
map.setMyLocationEnabled(true);

}

onResume() メソッドに追加すると、問題が解決します。

于 2013-08-28T04:38:05.273 に答える
1

それを解決する別の方法。MapFragment にインターフェースを実装して、GoogleMap 作成の準備が整ったときにアクティビティと通信します。

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    listener.onGoogleMapCreattion();


}

次に、Activtiy にリスナーを実装する必要があります。

    @Override
public void onGoogleMapCreation() {
    setUpMapIfNeeded();

}

    private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
         mMap = mMapFragment.getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
          // The Map is verified. It is now safe to manipulate the map.
            setUpMap();

        }
    }
  }
于 2013-05-06T10:23:35.350 に答える