我已经成功展示了地图但是当尝试使用GoogleMap,MarkerOption和CameraPosition应用程序停止时。我没有阅读logcat的经验。我确信错误不是由Manifest和Layout引起的,因为我在之前的问题中修复了它。
下面是我的Activity和logcat。
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
的活动:
package tri.mylocapp;
//import libraries...
public class MainActivity extends Activity implements LocationListener{
GoogleMap googlemap;
double latitude, longitude;
LocationManager lm;
Location location;
CameraPosition camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
onLocationChanged(location);
}
@Override
public void onLocationChanged(Location location) {
//get latitude
latitude = location.getLatitude(); Log.d("MyTag",String.valueOf(latitude));
//get longitude
longitude = location.getLongitude(); Log.d("MyTag",String.valueOf(longitude));
//Create marker and add marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
googlemap.addMarker(marker);
//Move camera
camera = new CameraPosition.Builder().target(new LatLng(latitude,longitude)).zoom(12).build();
googlemap.animateCamera(CameraUpdateFactory.newCameraPosition(camera));
}
@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
}
}
Logcat(仅错误):
04-03 19:17:45.957: E/Trace(112): error opening trace file: No such file or directory (2)
04-03 19:17:46.001: E/Trace(209): error opening trace file: No such file or directory (2)
04-03 19:17:46.269: E/BandwidthController(109): runIptablesCmd(): failed /system/bin/iptables -t raw -N bw_raw_PREROUTING res=768
04-03 19:17:46.293: E/BandwidthController(109): runIptablesCmd(): failed /system/bin/ip6tables -t raw -N bw_raw_PREROUTING res=768
04-03 19:17:46.553: E/BandwidthController(109): runIptablesCmd(): failed /system/bin/iptables -t raw -A bw_raw_PREROUTING ! -i lo+ -m owner --socket-exists res=768
04-03 19:17:46.565: E/BandwidthController(109): runIptablesCmd(): failed /system/bin/ip6tables -t raw -A bw_raw_PREROUTING ! -i lo+ -m owner --socket-exists res=768
04-03 19:17:46.657: E/PhonePolicy(113): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
04-03 19:17:47.517: E/Genymotion(336): usbOnlinePath not found
04-03 19:17:47.517: E/Genymotion(336): batteryHealthPath not found
04-03 19:17:47.517: E/Genymotion(336): batteryTemperaturePath not found
04-03 19:17:48.741: E/libsuspend(336): Error opening /sys/power/autosleep: No such file or directory
04-03 19:17:48.741: E/libsuspend(336): Error opening /sys/power/wakeup_count: Permission denied
04-03 19:17:48.741: E/libsuspend(336): failed to initialize autosuspend
04-03 19:17:48.769: E/WVMExtractor(115): Failed to open libwvm.so
04-03 19:17:48.769: E/Trace(115): error opening trace file: No such file or directory (2)
04-03 19:17:48.793: E/EventHub(336): could not get driver version for /dev/input/mouse3, Not a typewriter
04-03 19:17:48.801: E/WVMExtractor(115): Failed to open libwvm.so
04-03 19:17:48.809: E/EventHub(336): could not get driver version for /dev/input/mouse2, Not a typewriter
04-03 19:17:48.829: E/EventHub(336): could not get driver version for /dev/input/mouse1, Not a typewriter
04-03 19:17:48.857: E/EventHub(336): could not get driver version for /dev/input/mouse0, Not a typewriter
04-03 19:17:48.885: E/EventHub(336): could not get driver version for /dev/input/mice, Not a typewriter
04-03 19:17:48.889: E/Trace(336): error opening trace file: No such file or directory (2)
04-03 19:17:49.053: E/ConnectivityService(336): Ignoring protectedNetwork 10
04-03 19:17:49.053: E/ConnectivityService(336): Ignoring protectedNetwork 11
04-03 19:17:49.053: E/ConnectivityService(336): Ignoring protectedNetwork 12
04-03 19:17:49.061: E/WifiStateMachine(336): Failed to reload STA firmware java.lang.IllegalStateException: command '2 softap fwreload eth1 STA' failed with '400 2 Softap operation failed (Success)'
04-03 19:17:49.061: E/CommandListener(109): Failed to open /proc/sys/net/ipv6/conf/eth1/use_tempaddr: No such file or directory
04-03 19:17:49.061: E/WifiStateMachine(336): Unable to change interface settings: java.lang.IllegalStateException: command '5 interface ipv6privacyextensions eth1 enable' failed with '400 5 Failed to set ipv6 privacy extensions (No such file or directory)'
04-03 19:17:49.149: E/WifiConfigStore(336): Error parsing configurationjava.io.FileNotFoundException: /data/misc/wifi/ipconfig.txt: open failed: ENOENT (No such file or directory)
04-03 19:17:49.149: E/wpa_supplicant(402): Line -1: too long model_name (len=41 max_len=32)
04-03 19:17:49.149: E/wpa_supplicant(402): Line -1: failed to parse 'model_name=Google Nexus S - 4.1.1 - API 16 - 480x800'.
04-03 19:17:49.149: E/WifiStateMachine(336): Failed to set model name Google Nexus S - 4.1.1 - API 16 - 480x800
04-03 19:17:49.149: E/wpa_supplicant(402): Line -1: too long model_number (len=41 max_len=32)
04-03 19:17:49.149: E/wpa_supplicant(402): Line -1: failed to parse 'model_number=Google Nexus S - 4.1.1 - API 16 - 480x800'.
04-03 19:17:49.149: E/WifiStateMachine(336): Failed to set model number Google Nexus S - 4.1.1 - API 16 - 480x800
04-03 19:17:49.149: E/WifiStateMachine(336): Failed to set frequency band 0
04-03 19:17:49.245: E/Trace(410): error opening trace file: No such file or directory (2)
04-03 19:17:49.697: E/Trace(451): error opening trace file: No such file or directory (2)
04-03 19:17:49.949: E/BandwidthController(109): runIptablesCmd(): failed /system/bin/iptables -t raw -A bw_raw_PREROUTING ! -i lo+ -m owner --socket-exists res=768
04-03 19:17:50.013: E/BandwidthController(109): runIptablesCmd(): failed /system/bin/ip6tables -t raw -A bw_raw_PREROUTING ! -i lo+ -m owner --socket-exists res=768
04-03 19:17:50.013: A/NetworkManagementService(336): problem enabling bandwidth controls
04-03 19:17:50.013: A/NetworkManagementService(336): com.android.server.NativeDaemonConnector$NativeDaemonFailureException: command '6 bandwidth enable' failed with '400 6 Bandwidth command failed'
04-03 19:17:50.013: A/NetworkManagementService(336): at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:345)
04-03 19:17:50.013: A/NetworkManagementService(336): at com.android.server.NativeDaemonConnector.executeForList(NativeDaemonConnector.java:282)
04-03 19:17:50.013: A/NetworkManagementService(336): at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:248)
04-03 19:17:50.013: A/NetworkManagementService(336): at com.android.server.NetworkManagementService.prepareNativeDaemon(NetworkManagementService.java:292)
04-03 19:17:50.013: A/NetworkManagementService(336): at com.android.server.NetworkManagementService.systemReady(NetworkManagementService.java:188)
04-03 19:17:50.013: A/NetworkManagementService(336): at com.android.server.ServerThread$1.run(SystemServer.java:779)
04-03 19:17:50.013: A/NetworkManagementService(336): at com.android.server.am.ActivityManagerService.systemReady(ActivityManagerService.java:7617)
04-03 19:17:50.013: A/NetworkManagementService(336): at com.android.server.ServerThread.run(SystemServer.java:768)
04-03 19:17:50.013: E/NetdConnector(336): NDC Command {6 bandwidth enable} took too long (771ms)
04-03 19:17:50.237: E/Trace(527): error opening trace file: No such file or directory (2)
04-03 19:17:50.249: E/Trace(540): error opening trace file: No such file or directory (2)
04-03 19:17:50.345: E/Trace(553): error opening trace file: No such file or directory (2)
04-03 19:17:50.489: E/GpsLocationProvider(336): no AGPS interface in agps_data_conn_open
04-03 19:17:50.489: E/Trace(587): error opening trace file: No such file or directory (2)
04-03 19:17:50.805: E/Trace(610): error opening trace file: No such file or directory (2)
04-03 19:17:50.825: E/ActivityThread(527): Failed to find provider info for com.android.inputmethod.latin.dictionarypack
04-03 19:17:50.829: E/BinaryDictionaryGetter(527): Could not find a dictionary pack
04-03 19:17:50.945: E/Trace(635): error opening trace file: No such file or directory (2)
04-03 19:17:51.025: E/Trace(645): error opening trace file: No such file or directory (2)
04-03 19:17:51.337: E/Trace(670): error opening trace file: No such file or directory (2)
04-03 19:17:52.149: E/Trace(705): error opening trace file: No such file or directory (2)
04-03 19:17:52.525: E/WVMExtractor(115): Failed to open libwvm.so
04-03 19:17:52.545: E/dalvikvm(670): Could not find class 'android.app.AppOpsManager', referenced from method box.a
04-03 19:17:52.609: E/WVMExtractor(115): Failed to open libwvm.so
04-03 19:17:52.645: E/WVMExtractor(115): Failed to open libwvm.so
04-03 19:17:52.697: E/WVMExtractor(115): Failed to open libwvm.so
04-03 19:17:52.737: E/dalvikvm(705): Could not find class 'hlg', referenced from method hlf.a
04-03 19:17:52.741: E/WVMExtractor(115): Failed to open libwvm.so
04-03 19:17:52.857: E/dalvikvm(705): Could not find class 'android.os.UserHandle', referenced from method ill.c
04-03 19:17:52.901: E/dalvikvm(705): Could not find class 'android.app.AppOpsManager', referenced from method box.a
04-03 19:17:52.957: E/Trace(758): error opening trace file: No such file or directory (2)
04-03 19:17:53.057: E/Trace(776): error opening trace file: No such file or directory (2)
04-03 19:17:53.145: E/dalvikvm(705): Could not find class 'android.os.UserManager', referenced from method bov.b
04-03 19:17:53.157: E/dalvikvm(705): Could not find class 'android.os.UserManager', referenced from method bov.c
04-03 19:17:53.165: E/Trace(798): error opening trace file: No such file or directory (2)
04-03 19:17:53.169: E/jdwp(798): Failed sending reply to debugger: Broken pipe
04-03 19:17:53.181: E/dalvikvm(705): Could not find class 'android.os.UserManager', referenced from method bov.d
04-03 19:17:53.205: E/Trace(811): error opening trace file: No such file or directory (2)
04-03 19:17:53.241: E/ExternalAccountType(776): Unsupported attribute readOnly
04-03 19:17:53.397: E/Trace(835): error opening trace file: No such file or directory (2)
04-03 19:17:53.521: E/Trace(852): error opening trace file: No such file or directory (2)
04-03 19:17:53.577: E/Trace(869): error opening trace file: No such file or directory (2)
04-03 19:17:53.757: E/Trace(914): error opening trace file: No such file or directory (2)
04-03 05:17:57.231: E/Trace(942): error opening trace file: No such file or directory (2)
04-03 05:17:57.275: E/Trace(958): error opening trace file: No such file or directory (2)
04-03 05:17:57.299: E/Trace(972): error opening trace file: No such file or directory (2)
04-03 05:17:57.351: E/Trace(989): error opening trace file: No such file or directory (2)
04-03 05:17:57.811: E/Trace(1036): error opening trace file: No such file or directory (2)
04-03 05:17:57.867: E/Trace(1050): error opening trace file: No such file or directory (2)
04-03 05:17:58.055: E/dalvikvm(670): Could not find class 'android.os.UserManager', referenced from method bov.b
04-03 05:17:58.055: E/dalvikvm(670): Could not find class 'android.os.UserManager', referenced from method bov.c
04-03 05:17:58.055: E/dalvikvm(670): Could not find class 'android.os.UserManager', referenced from method bov.d
04-03 05:17:58.087: E/dalvikvm(587): Could not find class 'android.os.UserManager', referenced from method bov.b
04-03 05:17:58.095: E/dalvikvm(587): Could not find class 'android.os.UserManager', referenced from method bov.c
04-03 05:17:58.095: E/dalvikvm(587): Could not find class 'android.os.UserManager', referenced from method bov.d
04-03 05:17:58.115: E/Trace(1079): error opening trace file: No such file or directory (2)
04-03 05:17:58.611: E/Trace(1127): error opening trace file: No such file or directory (2)
04-03 05:17:58.839: E/dalvikvm(587): Could not find class 'android.app.AppOpsManager', referenced from method box.a
04-03 05:17:58.923: E/Trace(1150): error opening trace file: No such file or directory (2)
04-03 05:17:58.987: E/AndroidRuntime(1127): FATAL EXCEPTION: main
04-03 05:17:58.987: E/AndroidRuntime(1127): java.lang.UnsatisfiedLinkError: Cannot load library: load_library[1093]: Library '/system/lib/libhoudini.so' not found
04-03 05:17:58.987: E/AndroidRuntime(1127): at java.lang.Runtime.loadLibrary(Runtime.java:370)
04-03 05:17:58.987: E/AndroidRuntime(1127): at java.lang.System.loadLibrary(System.java:535)
04-03 05:17:58.987: E/AndroidRuntime(1127): at bkn.a(ProGuard:30)
04-03 05:17:58.987: E/AndroidRuntime(1127): at bkd.<init>(ProGuard:89)
04-03 05:17:58.987: E/AndroidRuntime(1127): at bkd.a(ProGuard:261)
04-03 05:17:58.987: E/AndroidRuntime(1127): at com.google.android.apps.plus.service.ConnectivityReceiver.onReceive(ProGuard:16)
04-03 05:17:58.987: E/AndroidRuntime(1127): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
04-03 05:17:58.987: E/AndroidRuntime(1127): at android.app.ActivityThread.access$1500(ActivityThread.java:130)
04-03 05:17:58.987: E/AndroidRuntime(1127): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
04-03 05:17:58.987: E/AndroidRuntime(1127): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 05:17:58.987: E/AndroidRuntime(1127): at android.os.Looper.loop(Looper.java:137)
04-03 05:17:58.987: E/AndroidRuntime(1127): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 05:17:58.987: E/AndroidRuntime(1127): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 05:17:58.987: E/AndroidRuntime(1127): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 05:17:58.987: E/AndroidRuntime(1127): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 05:17:58.987: E/AndroidRuntime(1127): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 05:17:58.987: E/AndroidRuntime(1127): at dalvik.system.NativeStart.main(Native Method)
04-03 05:18:36.591: E/AndroidRuntime(1358): FATAL EXCEPTION: main
04-03 05:18:36.591: E/AndroidRuntime(1358): java.lang.RuntimeException: Unable to start activity ComponentInfo{tri.mylocapp/tri.mylocapp.MainActivity}: java.lang.NullPointerException
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.os.Looper.loop(Looper.java:137)
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 05:18:36.591: E/AndroidRuntime(1358): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 05:18:36.591: E/AndroidRuntime(1358): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 05:18:36.591: E/AndroidRuntime(1358): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 05:18:36.591: E/AndroidRuntime(1358): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 05:18:36.591: E/AndroidRuntime(1358): at dalvik.system.NativeStart.main(Native Method)
04-03 05:18:36.591: E/AndroidRuntime(1358): Caused by: java.lang.NullPointerException
04-03 05:18:36.591: E/AndroidRuntime(1358): at tri.mylocapp.MainActivity.onLocationChanged(MainActivity.java:40)
04-03 05:18:36.591: E/AndroidRuntime(1358): at tri.mylocapp.MainActivity.onCreate(MainActivity.java:34)
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.app.Activity.performCreate(Activity.java:5008)
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-03 05:18:36.591: E/AndroidRuntime(1358): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-03 05:18:36.591: E/AndroidRuntime(1358): ... 11 more
答案 0 :(得分:0)
如果没有启动活动,您将无法获得系统服务。因此,您无法像现在一样初始化位置管理器:
LocationManager lm =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
抛出异常是明确的:
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
您应该在任何活动方法中获取位置服务,当然还要保留声明:
private LocationManager lm;
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
lm =(LocationManager)getSystemService(Context.LOCATION_SERVICE);`
//...
}
答案 1 :(得分:0)
放置一行:
LocationManager lm =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
在你的onCreate()方法下面。 错误是自解释的。您无法在onCreate()方法之前初始化系统服务。