1
我正在一個簡單的Android項目上使用一組硬編碼的經度和緯度值顯示一條折線。谷歌地圖polyline項目顯示java.lang.RuntimeException
MainActivity.java
如圖所示: package com.flip.GoogleMaps;
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
是folllows:
<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
我現在無可救藥。請幫忙!
什麼是線20? – Yellos 2013-03-14 11:33:48
你能告訴我哪一行是你問的嗎?來自xml或清單中的一個還是來自.java的一個? – 2013-03-14 14:12:01