我是 android 新手。我为 MAP 创建了一个应用程序,并在模拟器中安装了 vending.apk 和 gms.apk 以在模拟器中运行地图。并且还在我的eclipse中复制google-play-service_lib项目并将google-play-service.zip包含在我的项目lib文件夹中并在项目中包含google-play-service.jar。在运行时它显示错误。Logcat 如下所示。请帮我解决这个问题。
public class MainActivity extends Activity{
GMapV2GetRouteDirection routedirection;
GoogleMap googlemap;
MarkerOptions markerOption;
LatLng PositionFrom;
LatLng PositionTo;
Document document;
MarkerOptions markerOptions;
Location location ;
/*static final LatLng Poprad = new LatLng(49.055, 20.299);
static final LatLng Prag = new LatLng(50.085, 14.458);*/
private GoogleMap map;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
routedirection=new GMapV2GetRouteDirection();
/*SupportMapFragment mapfragment=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googlemap=mapfragment.getMap();*/
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googlemap.setMyLocationEnabled(true);
googlemap.getUiSettings().setZoomControlsEnabled(true);
googlemap.getUiSettings().setCompassEnabled(true);
googlemap.getUiSettings().setMyLocationButtonEnabled(true);
googlemap.getUiSettings().setAllGesturesEnabled(true);
googlemap.setTrafficEnabled(true);
googlemap.animateCamera(CameraUpdateFactory.zoomTo(10));
markerOption=new MarkerOptions();
PositionFrom=new LatLng(6777.7,8888.5);
PositionTo=new LatLng(4444.3, 3333.4);
GetRoutTask getRoute =new GetRoutTask();
getRoute.execute();
}
private class GetRoutTask extends AsyncTask<String,Void,String>{
private ProgressDialog pdialog;
String response = "";
protected void onPreExecute(){
pdialog=new ProgressDialog(MainActivity.this);
pdialog.setMessage("Loading.....");
pdialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
document=routedirection.Getdocument(PositionFrom, PositionTo, GMapV2GetRouteDirection.MODE_DRIVING);
response = "Success";
return response;
}
protected void onPostExecute(String result) {
googlemap.clear();
if(response.equalsIgnoreCase("Success")){
ArrayList<LatLng> directionPoint = routedirection.getDirection(document);
PolylineOptions rectLine = new PolylineOptions().width(10).color(
Color.RED);
for (int i = 0; i < directionPoint.size(); i++) {
rectLine.add(directionPoint.get(i));
}
// Adding route on the map
googlemap.addPolyline(rectLine);
markerOptions.position(PositionFrom);
markerOptions.draggable(true);
googlemap.addMarker(markerOptions);
}
pdialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<permission
android:name="example.map.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="example.map.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="example.map.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="AIzaSyBwP-HgSvM_SqUKYTdP30Jopp6V-z0LZdo"/>
</application>
</manifest>
LOGCAT
07-26 14:24:22.261: W/dalvikvm(860): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
07-26 14:24:22.291: E/AndroidRuntime(860): FATAL EXCEPTION: main
07-26 14:24:22.291: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.map/example.map.MainActivity}: java.lang.NullPointerException
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.os.Looper.loop(Looper.java:137)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-26 14:24:22.291: E/AndroidRuntime(860): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 14:24:22.291: E/AndroidRuntime(860): at java.lang.reflect.Method.invoke(Method.java:511)
07-26 14:24:22.291: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-26 14:24:22.291: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 14:24:22.291: E/AndroidRuntime(860): at dalvik.system.NativeStart.main(Native Method)
07-26 14:24:22.291: E/AndroidRuntime(860): Caused by: java.lang.NullPointerException
07-26 14:24:22.291: E/AndroidRuntime(860): at example.map.MainActivity.onCreate(MainActivity.java:49)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.Activity.performCreate(Activity.java:5008)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-26 14:24:22.291: E/AndroidRuntime(860): ... 11 more
07-26 14:25:04.591: I/Process(860): Sending signal. PID: 860 SIG: 9