1

ハードコーディングされた一連の緯度と経度の値を使用してポリラインを表示する単純な Android プロジェクトに取り組んでいます。

パッケージcom.flip.GoogleMaps MainActivity.java;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GoogleMap myMap = null;
    FragmentManager myFragmentManager = getFragmentManager();
    MapFragment myMapFragment = MapFragment)myFragmentManager.findFragmentById(R.id.map);
    myMap = myMapFragment.getMap();
    myMap.setMyLocationEnabled(true);
    myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    super.onCreate(savedInstanceState);


    PolylineOptions rectOptions = new PolylineOptions()
    .add(new LatLng(37.35, -122.0))
    .add(new LatLng(37.45, -122.0))  
    .add(new LatLng(37.45, -122.2))  
    .add(new LatLng(37.35, -122.2))  
    .add(new LatLng(37.35, -122.0));

    @SuppressWarnings("unused")
    Polyline polyline = myMap.addPolyline(rectOptions);         
    }
}

は次のactivity_main.xmlとおりです。

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

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

</LinearLayout>

そして、AndroidManifest.xmlは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flip.GoogleMaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<permission      android:name="com.flip.GoogleMaps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"></permission>
<uses-permission android:name="com.flip.GoogleMaps.permission.MAPS_RECEIVE"/>
<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" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBD9fsiMd_G9Pzeq2Eqas2FwJjzMGbSOnA"/>
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name="com.flip.GoogleMaps.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>

</application>
</manifest>

起動に失敗すると、Logcat に次のエラーが表示されます。

03-14 12:22:13.573: E/AndroidRuntime(1120): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flip.GoogleMaps/com.flip.GoogleMaps.MainActivity}: java.lang.NullPointerException
03-14 12:22:13.573: E/AndroidRuntime(1120):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at android.os.Looper.loop(Looper.java:137)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at     android.app.ActivityThread.main(ActivityThread.java:4745)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at dalvik.system.NativeStart.main(Native Method)
03-14 12:22:13.573: E/AndroidRuntime(1120):     Caused by: java.lang.NullPointerException
03-14 12:22:13.573: E/AndroidRuntime(1120):     at com.flip.GoogleMaps.MainActivity.onCreate(MainActivity.java:20)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at android.app.Activity.performCreate(Activity.java:5008)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-14 12:22:13.573: E/AndroidRuntime(1120):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-14 12:22:13.573: E/AndroidRuntime(1120):     ... 11 more

私は今絶望的です。助けてください!

4

1 に答える 1

0

コードにタイプミスがあります。変化する

MapFragment myMapFragment = MapFragment)myFragmentManager.findFragmentById(R.id.map);

MapFragment myMapFragment = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
于 2013-03-14T11:33:32.090 に答える