我在其他地方写了这个代码,将谷歌地图v2集成到我的Android应用程序,我收到错误。我需要知道我犯了什么错误。下面列出了代码和logcat错误:
EkoMap.java:
package com.src.apps.myekoapp;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.wallet.Address;
@SuppressLint("NewApi")
public class EkoMap extends Activity {
private GoogleMap ekoMap;
LocationManager locMan;
Location lastLoc;
LatLng lastLatLng;
private Marker userMarker;
private int user_icon;
Geocoder mGeocoder;
double lat, lng;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_fragment);
user_icon = R.drawable.user_icon;
// Setting zoom controls
//ekoMap.getUiSettings().setZoomControlsEnabled(true);
try {
if (ekoMap == null) {
ekoMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.the_map)).getMap();
}
if (ekoMap != null) {
// ok - proceed
}
ekoMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
// Updating User Location
updatePlaces();
updateWithNewlocation(lastLoc);
}
@Override
public void onResume() {
super.onResume();
}
// showing current location
private void updatePlaces() {
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
lat = lastLoc.getLatitude();
lng = lastLoc.getLongitude();
lastLatLng = new LatLng(lat, lng);
if (userMarker != null)
userMarker.remove();
userMarker = ekoMap.addMarker(new MarkerOptions().position(lastLatLng)
.title("You are here")
.icon(BitmapDescriptorFactory.fromResource(user_icon))
.snippet("Your last recorded location"));
ekoMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 300,
null);
}
// showing the new location as the user moves
public void updateWithNewlocation(Location l) {
mGeocoder = new Geocoder(this, Locale.getDefault());
String addressString = "No address found";
if (!Geocoder.isPresent())
addressString = "No geocoder available";
else {
try {
List<android.location.Address> address = mGeocoder
.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (address.size() > 0) {
android.location.Address addresses = address.get(0);
for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++)
sb.append(addresses.getAddressLine(i)).append("\n");
sb.append(addresses.getLocality()).append("\n");
sb.append(addresses.getPostalCode()).append("\n");
sb.append(addresses.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {
Log.d("WHERE AM I", "IOException", e);
}
}
Toast.makeText(getApplicationContext(),
"Your current Location is " + addressString, Toast.LENGTH_SHORT)
.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Adding menus to change the look of the map
MenuInflater mMenuInflater = getMenuInflater();
mMenuInflater.inflate(R.menu.map_fragment_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem mMenuItem) {
// Handle presses on the action bar items
switch (mMenuItem.getItemId()) {
// Handle up/home navigation
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
// Handle hybrid view
case R.id.item_hybrid:
try {
ekoMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
} catch (Exception e) {
e.printStackTrace();
}
// Handle satellite view
case R.id.item_satellite:
try {
ekoMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} catch (Exception e) {
e.printStackTrace();
}
// Handle terrain view
case R.id.item_terrain:
try {
ekoMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
} catch (Exception e) {
e.printStackTrace();
}
// Handel about app
case R.id.action_about:
return true;
// Handle call
case R.id.action_call:
startActivity(new Intent(getApplicationContext(), CallLASMA.class));
// Return menuItem
default:
return super.onOptionsItemSelected(mMenuItem);
}
}
}
LogCat错误:
06-26 17:28:18.096: E/AndroidRuntime(415): FATAL EXCEPTION: main
06-26 17:28:18.096: E/AndroidRuntime(415): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.src.apps.myekoapp/com.src.apps.myekoapp.EkoMap}: java.lang.NullPointerException
06-26 17:28:18.096: E/AndroidRuntime(415): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
06-26 17:28:18.096: E/AndroidRuntime(415): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
06-26 17:28:18.096: E/AndroidRuntime(415): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
06-26 17:28:18.096: E/AndroidRuntime(415): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
06-26 17:28:18.096: E/AndroidRuntime(415): at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 17:28:18.096: E/AndroidRuntime(415): at android.os.Looper.loop(Looper.java:126)
06-26 17:28:18.096: E/AndroidRuntime(415): at android.app.ActivityThread.main(ActivityThread.java:3997)
06-26 17:28:18.096: E/AndroidRuntime(415): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 17:28:18.096: E/AndroidRuntime(415): at java.lang.reflect.Method.invoke(Method.java:491)
06-26 17:28:18.096: E/AndroidRuntime(415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-26 17:28:18.096: E/AndroidRuntime(415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-26 17:28:18.096: E/AndroidRuntime(415): at dalvik.system.NativeStart.main(Native Method)
06-26 17:28:18.096: E/AndroidRuntime(415): Caused by: java.lang.NullPointerException
06-26 17:28:18.096: E/AndroidRuntime(415): at com.src.apps.myekoapp.EkoMap.updatePlaces(EkoMap.java:81)
06-26 17:28:18.096: E/AndroidRuntime(415): at com.src.apps.myekoapp.EkoMap.onCreate(EkoMap.java:67)
06-26 17:28:18.096: E/AndroidRuntime(415): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
06-26 17:28:18.096: E/AndroidRuntime(415): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
06-26 17:28:18.096: E/AndroidRuntime(415): ... 11 more
答案 0 :(得分:0)
locMan
为空。
您是否在清单中添加了以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />