我正在开发一个应用程序,包括用于Android的Google Maps API,但是当我尝试显示地图时,我的应用程序崩溃。
这里我的 MapActivity.java ,其中,来自在之前的活动中,用户可以选择不同的摄像头位置。有6个标记,包含6个infowindows,其中4个将在点击时启动Google导航。
package com.example.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.CameraPosition.Builder;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapActivity extends Activity implements OnInfoWindowClickListener {
private GoogleMap mMap, map;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
Intent intent=getIntent();
String pkg="com.example.myapplication";
final int num=intent.getIntExtra(pkg+".number", 1);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationmanager.getBestProvider(criteria, true);
Location myLocation = locationmanager.getLastKnownLocation(provider);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Tu sei qui!"));
if(num==0){
mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
else if(num==1){
LatLng ne = new LatLng(45.49991,9.169847);
mMap.moveCamera(CameraUpdateFactory.newLatLng(ne));
mMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
else if(num==2){
LatLng no = new LatLng(45.473479,9.147917);
mMap.moveCamera(CameraUpdateFactory.newLatLng(no));
mMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
else if(num==3){
LatLng se = new LatLng(45.462177,9.257262);
mMap.moveCamera(CameraUpdateFactory.newLatLng(se));
mMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
else if(num==4){
LatLng so = new LatLng(45.431844,9.204222);
mMap.moveCamera(CameraUpdateFactory.newLatLng(so));
mMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
final LatLng one = new LatLng(45.483725,9.212669);
final LatLng two = new LatLng(45.463743,9.217819);
final LatLng three = new LatLng(45.430744,9.169754);
final LatLng four = new LatLng(45.505625,9.177994);
final LatLng five = new LatLng(45.497684,9.208549);
final LatLng six = new LatLng(45.523909,9.135765);
Marker One=mMap.addMarker(new MarkerOptions()
.position(one)
.title("Street 1")
.snippet("Start navigation")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker Two=mMap.addMarker(new MarkerOptions()
.position(two)
.title("Street 2")
.snippet("Start navigation")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.casa_marker)));
Marker Three=mMap.addMarker(new MarkerOptions()
.position(three)
.title("Street 3")
.snippet("Start navigation")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker Four=mMap.addMarker(new MarkerOptions()
.position(four)
.title("Street 4")
.snippet("Start navigation")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker Five=mMap.addMarker(new MarkerOptions()
.position(five)
.title("Street 5")
.snippet("Start navigation")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker Six=mMap.addMarker(new MarkerOptions()
.position(six)
.title("Street 6")
.snippet("Start navigation")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker) {
if(num==1){
LocationManager locationmanager1 = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria1 = new Criteria();
String provider1 = locationmanager1.getBestProvider(criteria1, true);
Location myLocation1 = locationmanager1.getLastKnownLocation(provider1);
double mLat= myLocation1.getLatitude();
double mLong= myLocation1.getLongitude();
Intent intent = new Intent( Intent.ACTION_VIEW,
Uri.parse("http://ditu.google.cn/maps?f=d&source=s_d" +
"&saddr="+mLat+","+mLong+"&daddr=45.498303,9.166803&hl=zh&t=m&dirflg=d"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK&Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
}
else if(num==2){
LocationManager locationmanager2 = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria2 = new Criteria();
String provider2 = locationmanager2.getBestProvider(criteria2, true);
Location myLocation2 = locationmanager2.getLastKnownLocation(provider2);
double mLat= myLocation2.getLatitude();
double mLong= myLocation2.getLongitude();
Intent intent = new Intent( Intent.ACTION_VIEW,
Uri.parse("http://ditu.google.cn/maps?f=d&source=s_d" +
"&saddr="+mLat+","+mLong+"&daddr=45.475149,9.151304&hl=zh&t=m&dirflg=d"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK&Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
}
else if(num==3){
LocationManager locationmanager3 = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria3 = new Criteria();
String provider3 = locationmanager3.getBestProvider(criteria3, true);
Location myLocation3 = locationmanager3.getLastKnownLocation(provider3);
double mLat= myLocation3.getLatitude();
double mLong= myLocation3.getLongitude();
Intent intent = new Intent( Intent.ACTION_VIEW,
Uri.parse("http://ditu.google.cn/maps?f=d&source=s_d" +
"&saddr="+mLat+","+mLong+"&daddr=45.465505,9.264067&hl=zh&t=m&dirflg=d"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK&Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
}
else if(num==4){
LocationManager locationmanager4 = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria4 = new Criteria();
String provider4 = locationmanager4.getBestProvider(criteria4, true);
Location myLocation4 = locationmanager4.getLastKnownLocation(provider4);
double mLat= myLocation4.getLatitude();
double mLong= myLocation4.getLongitude();
Intent intent = new Intent( Intent.ACTION_VIEW,
Uri.parse("http://ditu.google.cn/maps?f=d&source=s_d" +
"&saddr="+mLat+","+mLong+"&daddr=45.428621,9.227804&hl=zh&t=m&dirflg=d"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK&Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
}
} });
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
// Defines the contents of the InfoWindow
@Override
public View getInfoContents(Marker arg0) {
// Getting view from the layout file info_window_layout
View v = getLayoutInflater().inflate(R.layout.activity_map_infowindow, null);
// Getting the position from the marker
//LatLng Bologna = new LatLng(44.49489,11.34262);
LatLng latLng = arg0.getPosition();
TextView titolo = (TextView) v.findViewById(R.id.titolo);
TextView info = (TextView) v.findViewById(R.id.info);
String title=arg0.getTitle();
String informations=arg0.getSnippet();
titolo.setText(title);
info.setText(informations);
return v;
}
});
}
private void MapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.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.
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.notmain, menu);
return true;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case R.id.login:
startActivity(new Intent(this, LoginActivity.class));
return true;
}
return super.onMenuItemSelected(featureId, item);
}
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(MapActivity.this,SplashscreenActivity.class);
startActivity(intent);
finish();
}
}
这是我的 activity_map.xml ,其中有一个包含以下内容的相对布局: 1)一个包含4个按钮的线性布局 2)地图的片段
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3DA3E1"
tools:context=".MapActivity" >
<LinearLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btn_ne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@anim/a_dbne" />
<ImageButton
android:id="@+id/btn_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@anim/a_dbno"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp" />
<ImageButton
android:id="@+id/btn_se"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@anim/a_dbse"
android:layout_marginRight="1dp" />
<ImageButton
android:id="@+id/btn_so"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@anim/a_dbso" />
</LinearLayout>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/buttons" />
</RelativeLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="18" />
<permission
android:name="com.example.myapplication.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.myapplication.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<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="AI-------------------1Oa6tY" />
<activity
android:name="com.example.myapplication.SplashscreenActivity"
android:label="MyApplication"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myapplication.MapActivity"
android:label="MyApplication"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.example.myapplication.MainActivity"
android:label="MyApplication"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.example.myapplication.ChooseMapActivity"
android:label="MyApplication"
android:screenOrientation="portrait" >
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
logcat的
03-18 11:21:37.240: E/AndroidRuntime(18406): FATAL EXCEPTION: main
03-18 11:21:37.240: E/AndroidRuntime(18406): Process: com.example.myapplication, PID: 18406
03-18 11:21:37.240: E/AndroidRuntime(18406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MapActivity}: java.lang.NullPointerException
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.os.Handler.dispatchMessage(Handler.java:102)
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.os.Looper.loop(Looper.java:136)
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-18 11:21:37.240: E/AndroidRuntime(18406): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 11:21:37.240: E/AndroidRuntime(18406): at java.lang.reflect.Method.invoke(Method.java:515)
03-18 11:21:37.240: E/AndroidRuntime(18406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-18 11:21:37.240: E/AndroidRuntime(18406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-18 11:21:37.240: E/AndroidRuntime(18406): at dalvik.system.NativeStart.main(Native Method)
03-18 11:21:37.240: E/AndroidRuntime(18406): Caused by: java.lang.NullPointerException
03-18 11:21:37.240: E/AndroidRuntime(18406): at com.example.myapplication.MapActivity.onCreate(MapActivity.java:53)
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.app.Activity.performCreate(Activity.java:5231)
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-18 11:21:37.240: E/AndroidRuntime(18406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-18 11:21:37.240: E/AndroidRuntime(18406): ... 11 more
所以,我的应用程序在我的Nexus 5(Kitkat 4.4.2)上崩溃了......你能帮助我吗?每一个帮助将不胜感激! :)