无法启动 activity java.lang.NullPointerException
Unable to start activity java.lang.NullPointerException
我正在尝试在我的应用程序中使用 google 地图。我已经添加了库和所有需要的东西,但应用程序以 NULL 指针异常终止。请建议我。提前谢谢你。
Logcat
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yd.hhivaction/com.yd.hhivacation.MapIt}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access0(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.yd.hhivacation.MapIt.onCreate(MapIt.java:56)
at android.app.Activity.performCreate(Activity.java:5163)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access0(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
MapIt.java
package com.yd.hhivacation;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
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 MapIt extends ActionBarActivity implements LocationListener,
OnMapReadyCallback, android.location.LocationListener {
GoogleMap googlemap;
String listingid;
JSONArray data;
String laitude;
String logitude;
String headline, subline;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_it);
Intent in = getIntent();
listingid = in.getStringExtra("listingid");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
googlemap = mapFragment.getMap();
googlemap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}
/**
* This is where we can add markers or lines, add listeners or move the
* camera. In this case, we just add a marker near Africa.
*/
@Override
public void onMapReady(final GoogleMap map) {
String url2 = "http://m.hhivacation.com/webapi/getmapdetailsbyID.php?listingid="
+ listingid;
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url2,
null, new Response.Listener<JSONObject>() {
@SuppressLint("NewApi")
Context context;
@SuppressLint("UseValueOf")
@Override
public void onResponse(JSONObject response) {
try {
boolean suc = Boolean.parseBoolean(response
.getString("success"));
if (suc = true) {
data = response.getJSONArray("listing_details");
JSONObject feedObj = (JSONObject) data.get(0);
headline = feedObj.getString("headline");
subline = feedObj.getString("subline");
laitude = feedObj.getString("map_lat");
logitude = feedObj.getString("map_long");
if (laitude.length() != 0
& laitude.length() != 0) {
double lo, la;
try {
lo = new Double(logitude);
la = new Double(laitude);
} catch (NumberFormatException e) {
lo = 75.8655; // your default value
la = 22.7253;
}
final LatLng PERTH = new LatLng(la, lo);
// Marker perth = map
// .addMarker(new MarkerOptions()
// .position(PERTH)
// .title(headline)
// .snippet(subline));
//
// map.getUiSettings().setZoomControlsEnabled(
// false);
//
// map.moveCamera(CameraUpdateFactory.newLatLngZoom(
// new LatLng(
// Double.parseDouble(laitude),
// Double.parseDouble(logitude)),
// 4));
}
Log.d("mapit", headline + "&" + subline);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("resonce", laitude + "--" + logitude);
/*
* responseText.setText("Response:" + " " +
* response.toString() + ":" + st1);
*/
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("App", "Error: " + error.getMessage());
// hide the progress dialog
}
});
VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(
jsonObjReq);
}
@SuppressLint("UseValueOf")
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (laitude != null && logitude != null) {
if (laitude.length() != 0 & logitude.length() != 0) {
double lo, la;
try {
lo = new Double(logitude);
la = new Double(laitude);
} catch (NumberFormatException e) {
la = 42.3482; // your default value
lo = 75.1890;
}
double latitude = lo;
double longitude = la;
LatLng latLng = new LatLng(latitude, longitude);
googlemap.addMarker(new MarkerOptions().position(latLng));
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googlemap.animateCamera(CameraUpdateFactory.zoomTo(6));
// Toast.makeText(MapIt.this, location + " & " + longitude,
// Toast.LENGTH_SHORT).show();
}
}
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="2"
android:versionName="1.1" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:name="com.yd.hhivacation.MyApplication"
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCNe5t_yjE_NwwWTNj8MYq2oJNetFfFIk4" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.yd.hhivacation.FirstActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SubCat"
android:label="@string/title_activity_sub_cat"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".Listing"
android:label="@string/title_activity_listing"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".Detail"
android:label="@string/title_activity_detail"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".WriteReview"
android:label="@string/title_activity_write_review"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".MapIt"
android:label="@string/title_activity_map_it"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Search"
android:label="@string/title_activity_search"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".F_about"
android:label="@string/title_activity_f_about"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".F_feedback"
android:label="@string/title_activity_f_feedback"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".F_public"
android:label="@string/title_activity_f_public"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".F_advert"
android:label="@string/title_activity_f_advert"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".Loadreviews"
android:label="@string/title_activity_loadreviews"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".Websites"
android:label="@string/title_activity_websites"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
</application>
</manifest>
activity_map_it
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
新 Logcat 几处改动后
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yd.hhivaction/com.yd.hhivacation.MapIt}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access0(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.yd.hhivacation.MapIt.onCreate(MapIt.java:52)
at android.app.Activity.performCreate(Activity.java:5163)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access0(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
@52 googlemap.setMyLocationEnabled(真);
错误可能在第 googlemap = mapFragment.getMap();
行。在上一行中,您使用:mapFragment.getMapAsync(this);
,在 onMapReady
中,您应该像 googlemap = map
一样存储 google 映射。所以你需要做的是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_it);
Intent in = getIntent();
listingid = in.getStringExtra("listingid");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}
@Override
public void onMapReady(final GoogleMap map) {
if (this.googlemap == null) {
googlemap = map;
googlemap.setMyLocationEnabled(true);
}
String url2 = "http://m.hhivacation.com/webapi/getmapdetailsbyID.php?listingid="
+ listingid;
....
....
}
也在onLocationChanged
:
if (googlemap != null) {
googlemap.addMarker(new MarkerOptions().position(latLng));
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googlemap.animateCamera(CameraUpdateFactory.zoomTo(6));
}
希望对您有所帮助。
我正在尝试在我的应用程序中使用 google 地图。我已经添加了库和所有需要的东西,但应用程序以 NULL 指针异常终止。请建议我。提前谢谢你。
Logcat
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yd.hhivaction/com.yd.hhivacation.MapIt}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access0(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.yd.hhivacation.MapIt.onCreate(MapIt.java:56)
at android.app.Activity.performCreate(Activity.java:5163)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access0(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
MapIt.java
package com.yd.hhivacation;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
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 MapIt extends ActionBarActivity implements LocationListener,
OnMapReadyCallback, android.location.LocationListener {
GoogleMap googlemap;
String listingid;
JSONArray data;
String laitude;
String logitude;
String headline, subline;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_it);
Intent in = getIntent();
listingid = in.getStringExtra("listingid");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
googlemap = mapFragment.getMap();
googlemap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}
/**
* This is where we can add markers or lines, add listeners or move the
* camera. In this case, we just add a marker near Africa.
*/
@Override
public void onMapReady(final GoogleMap map) {
String url2 = "http://m.hhivacation.com/webapi/getmapdetailsbyID.php?listingid="
+ listingid;
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url2,
null, new Response.Listener<JSONObject>() {
@SuppressLint("NewApi")
Context context;
@SuppressLint("UseValueOf")
@Override
public void onResponse(JSONObject response) {
try {
boolean suc = Boolean.parseBoolean(response
.getString("success"));
if (suc = true) {
data = response.getJSONArray("listing_details");
JSONObject feedObj = (JSONObject) data.get(0);
headline = feedObj.getString("headline");
subline = feedObj.getString("subline");
laitude = feedObj.getString("map_lat");
logitude = feedObj.getString("map_long");
if (laitude.length() != 0
& laitude.length() != 0) {
double lo, la;
try {
lo = new Double(logitude);
la = new Double(laitude);
} catch (NumberFormatException e) {
lo = 75.8655; // your default value
la = 22.7253;
}
final LatLng PERTH = new LatLng(la, lo);
// Marker perth = map
// .addMarker(new MarkerOptions()
// .position(PERTH)
// .title(headline)
// .snippet(subline));
//
// map.getUiSettings().setZoomControlsEnabled(
// false);
//
// map.moveCamera(CameraUpdateFactory.newLatLngZoom(
// new LatLng(
// Double.parseDouble(laitude),
// Double.parseDouble(logitude)),
// 4));
}
Log.d("mapit", headline + "&" + subline);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("resonce", laitude + "--" + logitude);
/*
* responseText.setText("Response:" + " " +
* response.toString() + ":" + st1);
*/
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("App", "Error: " + error.getMessage());
// hide the progress dialog
}
});
VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(
jsonObjReq);
}
@SuppressLint("UseValueOf")
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (laitude != null && logitude != null) {
if (laitude.length() != 0 & logitude.length() != 0) {
double lo, la;
try {
lo = new Double(logitude);
la = new Double(laitude);
} catch (NumberFormatException e) {
la = 42.3482; // your default value
lo = 75.1890;
}
double latitude = lo;
double longitude = la;
LatLng latLng = new LatLng(latitude, longitude);
googlemap.addMarker(new MarkerOptions().position(latLng));
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googlemap.animateCamera(CameraUpdateFactory.zoomTo(6));
// Toast.makeText(MapIt.this, location + " & " + longitude,
// Toast.LENGTH_SHORT).show();
}
}
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="2"
android:versionName="1.1" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:name="com.yd.hhivacation.MyApplication"
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCNe5t_yjE_NwwWTNj8MYq2oJNetFfFIk4" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.yd.hhivacation.FirstActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SubCat"
android:label="@string/title_activity_sub_cat"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".Listing"
android:label="@string/title_activity_listing"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".Detail"
android:label="@string/title_activity_detail"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".WriteReview"
android:label="@string/title_activity_write_review"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".MapIt"
android:label="@string/title_activity_map_it"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Search"
android:label="@string/title_activity_search"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".F_about"
android:label="@string/title_activity_f_about"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".F_feedback"
android:label="@string/title_activity_f_feedback"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".F_public"
android:label="@string/title_activity_f_public"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".F_advert"
android:label="@string/title_activity_f_advert"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".Loadreviews"
android:label="@string/title_activity_loadreviews"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".Websites"
android:label="@string/title_activity_websites"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
</application>
</manifest>
activity_map_it
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
新 Logcat 几处改动后
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yd.hhivaction/com.yd.hhivacation.MapIt}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access0(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.yd.hhivacation.MapIt.onCreate(MapIt.java:52)
at android.app.Activity.performCreate(Activity.java:5163)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access0(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
@52 googlemap.setMyLocationEnabled(真);
错误可能在第 googlemap = mapFragment.getMap();
行。在上一行中,您使用:mapFragment.getMapAsync(this);
,在 onMapReady
中,您应该像 googlemap = map
一样存储 google 映射。所以你需要做的是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_it);
Intent in = getIntent();
listingid = in.getStringExtra("listingid");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}
@Override
public void onMapReady(final GoogleMap map) {
if (this.googlemap == null) {
googlemap = map;
googlemap.setMyLocationEnabled(true);
}
String url2 = "http://m.hhivacation.com/webapi/getmapdetailsbyID.php?listingid="
+ listingid;
....
....
}
也在onLocationChanged
:
if (googlemap != null) {
googlemap.addMarker(new MarkerOptions().position(latLng));
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googlemap.animateCamera(CameraUpdateFactory.zoomTo(6));
}
希望对您有所帮助。