出于某种原因,当我点击onmylocationbutton点击(谷歌地图功能)时,它会崩溃给我。我不明白为什么。我在互联网上查看了logcat的问题,但由于某些原因我无法得到它。我应该在代码中更改什么?
MainActivity:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.widget.ListView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends Activity {
private String[] NavigationDrawerItems;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
public GoogleMap map;
public Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmap);
mTitle = mDrawerTitle = getTitle();
NavigationDrawerItems = getResources().getStringArray(
R.array.navigationdraweritems);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// location stuff
final GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
// the location manager and the Criteria to find the right source for
// location
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final Criteria criteria = new Criteria();
// location manager
final android.location.Location location = locationManager
.getLastKnownLocation(locationManager.getBestProvider(criteria,
false));
if (location != null) {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
location.getLatitude(), location.getLongitude()), 10));
}
// class that gets the new location
class NewLocation implements LocationListener {
@Override
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
// end of the class the gets the new location
map.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
// if the Gps is off it openes a dialog to turn it off
if (!locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else {
// if it on, animates to the current position
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location
.getLongitude()), 15));
return false;
}
return false;
}
});
// closes on create
}
之后还有一些代码。但它并没有与它相关并且工作正常。
我的LogCat:
06-04 13:29:30.566: E/AndroidRuntime(5036): FATAL EXCEPTION: main
06-04 13:29:30.566: E/AndroidRuntime(5036): java.lang.NullPointerException
06-04 13:29:30.566: E/AndroidRuntime(5036): at nir.rauch.flantir.MainActivity$1.onMyLocationButtonClick(MainActivity.java:109)
06-04 13:29:30.566: E/AndroidRuntime(5036): at com.google.android.gms.maps.GoogleMap$2.onMyLocationButtonClick(Unknown Source)
06-04 13:29:30.566: E/AndroidRuntime(5036): at com.google.android.gms.maps.internal.m$a.onTransact(Unknown Source)
06-04 13:29:30.566: E/AndroidRuntime(5036): at android.os.Binder.transact(Binder.java:347)
06-04 13:29:30.566: E/AndroidRuntime(5036): at foj.a(SourceFile:81)
06-04 13:29:30.566: E/AndroidRuntime(5036): at lxw.onClick(Unknown Source)
06-04 13:29:30.566: E/AndroidRuntime(5036): at android.view.View.performClick(View.java:4475)
06-04 13:29:30.566: E/AndroidRuntime(5036): at android.view.View$PerformClick.run(View.java:18786)
06-04 13:29:30.566: E/AndroidRuntime(5036): at android.os.Handler.handleCallback(Handler.java:730)
06-04 13:29:30.566: E/AndroidRuntime(5036): at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 13:29:30.566: E/AndroidRuntime(5036): at android.os.Looper.loop(Looper.java:137)
06-04 13:29:30.566: E/AndroidRuntime(5036): at android.app.ActivityThread.main(ActivityThread.java:5419)
06-04 13:29:30.566: E/AndroidRuntime(5036): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 13:29:30.566: E/AndroidRuntime(5036): at java.lang.reflect.Method.invoke(Method.java:525)
06-04 13:29:30.566: E/AndroidRuntime(5036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
06-04 13:29:30.566: E/AndroidRuntime(5036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
06-04 13:29:30.566: E/AndroidRuntime(5036): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的findViewById可能会在地图变量中返回null。
尝试使用MapView
MaoView mapView = (MapView) v.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
GoogleMap map = mapView.getMap();
map.setMyLocationEnabled(true);