1

この私のコード:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

onCreate() メソッド内: (バンドルの作成と変数の割り当てを除いて、onCreate() にはこれしかありません。そのバンドルを使用します。)

FragmentManager myFM = getSupportFragmentManager();
SupportMapFragment myMAPF =(SupportMapFragment)myFM.
      findFragmentById(R.id.mapfragment);
map=myMAPF.getMap();//Exception at this line

レイアウト:

<fragment
    android:id="@+id/mapfragment"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

マニフェスト:

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

API キーを生成したときに、debug.keystore ファイルをコピーし、より便利な場所に保存してから生成したことを言及する必要があります。元の場所にある元のファイルを使用して再度実行したところ、同じ SH1 が返されたため、別の API キーを生成しませんでした。API Access ページでインタラプタを有効にしました。

以前に同じことを尋ねる回答を見たことがありますが、多くの解決策を試しました...運が悪い.

Android バージョン 4.0 を搭載したデバイスで、USB 経由で Eclipse からアプリを実行しています。

誰でもエラーの場所を見つけるのを手伝ってもらえますか? ありがとうございました。

編集

すみません、スタックトレースを忘れました

10-23 13:54:53.230: E/AndroidRuntime(6786): java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/mypackage.MapActivity}: java.lang.NullPointerException
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.os.Looper.loop(Looper.java:137)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at java.lang.reflect.Method.invoke(Method.java:511)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at dalvik.system.NativeStart.main(Native Method)
10-23 13:54:53.230: E/AndroidRuntime(6786): Caused by: java.lang.NullPointerException
10-23 13:54:53.230: E/AndroidRuntime(6786):     at mypackage.MapActivity.onCreate(MapActivity.java:73)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.Activity.performCreate(Activity.java:4470)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-23 13:54:53.230: E/AndroidRuntime(6786):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

奇妙なことに気付きました。スタックトレースの最初の行に、アプリの完全なパッケージが 2 倍あります...なぜこれが起こっているのですか?

4

1 に答える 1

2

あなたのマップはまだ利用できません...次のような方法を使用して、Play サービスを利用する前にそれらが利用可能かどうかを判断できます (onCreate ではなく onActivityCreated で呼び出します)。

protected boolean isPlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (status == ConnectionResult.SUCCESS) {
        return (true);
    } else if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
        // deal with error
    } else {
            // maps is not available
    }

    return (false);
}

また、フラグメントは、Play サービスで必要なコールバックを実装する必要があります (インターフェイス GooglePlayServicesClient.ConnectionCallbacks および GooglePlayServicesClient.OnConnectionFailedListener :

    /*
     * Called by Location Services when the request to connect the
     * client finishes successfully. At this point, you can
     * request the current location or start periodic updates
     */
    @Override
    public void onConnected(Bundle dataBundle) {
        // Display the connection status
        Toast.makeText(context, "Connected to location services", Toast.LENGTH_SHORT).show();

    }

    /*
     * Called by Location Services if the connection to the
     * location client drops because of an error.
     */
    @Override
    public void onDisconnected() {
        // Display the connection status
        Toast.makeText(context, "Disconnected from location services. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

    /*
     * Called by Location Services if the attempt to
     * Location Services fails.
     */
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        /*
         * Google Play services can resolve some errors it detects.
         * If the error has a resolution, try sending an Intent to
         * start a Google Play services activity that can resolve
         * error.
         */
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(
                        SomeActivity,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
                /*
                 * Thrown if Google Play services canceled the original
                 * PendingIntent
                 */
            } catch (IntentSender.SendIntentException e) {
                // Log the error
                e.printStackTrace();
            }
        } else {
            /*
             * If no resolution is available, display a dialog to the
             * user with the error.
             */
            Toast.makeText(context, "Connection error", Toast.LENGTH_SHORT).show();
        }
    }
}

編集: これは、マップフラグメントを含むアクティビティで使用する方法です:

if (isPlayServicesAvailable()) {
    setContentView(R.layout.activity_with_map_fragment);
}

およびフラグメント内:

if (getMap() != null) {
    GoogleMap map = getMap();
    // do things with the map
}

これでうまくいくはずです;-)

于 2013-10-23T12:44:15.413 に答える