什么是在android studio上运行谷歌地图应用程序时出错的原因是用api 22跟踪用户在真实设备上的位置? [英] What is reason for getting error while running google map app on android studio to track user locationon real device with api 22?
问题描述
我正在使用Android Studio在Google Map上显示我的当前位置。但是收到以下错误:
找不到类'android.os.UserManager',引用自方法com.google.android.gms.common.zze.zzaq
和
03-10 17:42:38.362 8762-8762 / com.example.dell_pc.mymapapp E / AndroidRuntime:FATAL EXCEPTION:main
java.lang.RuntimeException:无法启动activity ComponentInfo {com.example.dell_pc.mymapapp / com.example.dell_pc.mymapapp.MapsActivity}:java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
在android.app.ActivityThread.access $ 600(ActivityThread.java:128)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)
在android.os.Handler.dispatchMessage( Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4517)
在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:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik .system.NativeStart.main(Native Method)
引起:java.lang.NullPointerException
at com.example.dell_pc.mymapapp.MapsActivity.setUpMap(MapsActivity.java:108)
at com.example.dell_pc.mymapapp.MapsActivity.setUpMapIfNeeded(MapsActivity.java:60)
at com.example.dell_pc.mymapapp.Maps Activity.onCreate(MapsActivity.java:25)
在android.app.Activity.performCreate(Activity.java:4470)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
在android.app.ActivityThread.access $ 600( ActivityThread.java:128)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java) :4517)
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:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(原生方法)
请提前帮助。谢谢。
我尝试了什么:
我的MapActivity.java看起来如下代码
包com.example.dell_pc.mymapapp;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android .support.v4.app.FragmentActivity;
导入android.os.Bundle;
导入android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com。 google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gm s.maps.model.MarkerOptions;
公共类MapsActivity扩展FragmentActivity {
私有GoogleMap mMap; //如果Google Play服务APK不可用,可能为null。
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume(){
super.onResume();
setUpMapIfNeeded();
}
/ **
*如果可以的话设置地图(即Google Play服务APK是正确的
*已安装)并且地图尚未实例化。这将确保当{@link #mMap}不为空时,我们只会拨打{@link #setUpMap()}一次。
*
*如果没有安装{@link SupportMapFragment}(和
* {@link com.google.android.gms.maps.MapView MapView})将显示一个提示,让用户在他们的设备上安装/更新Google Play服务APK。
*
*用户可以在按照提示并正确
*安装/更新/启用Google Play服务后返回此FragmentActivity。由于FragmentActivity可能不会在这个过程中完全被破坏(很可能它只会被停止或暂停),{@ link #onCreate( Bundle)}可能不会被再次调用,所以我们应该在{@link #onResume()}中调用这个
*方法来保证它将被调用。
* /
private void setUpMapIfNeeded(){
//执行空检查以确认我们尚未实例化地图。
if(mMap == null){
//尝试从SupportMapFragment获取地图。
mMap =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map) )
.getMap();
//检查我们是否成功获取地图。
if(mMap!= null){
setUpMap();
}
}
}
/ **
*这是我们可以添加标记或线条,添加侦听器或移动相机的地方。在这种情况下,我们
*只需在非洲附近添加一个标记。
*
*这应该只是调用一次,当我们确定{@link #mMap}不为空时。
* /
private void setUpMap(){
mMap.addMarker(new MarkerOptions()。position(new LatLng(0,0))。title(Marker)。snippet(Snippet));
//启用Google地图的MyLocation层
尝试{
mMap.setMyLocationEnabled(true);
} catch(SecurityException e){
Log.e(PERMISSION_EXCEPTION,PERMISSION_NOT_GRANTED);
}
//从系统服务获取LocationManager对象LOCATION_SERVICE
LocationManager locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
/ /创建一个标准对象来检索提供者
标准条件=新标准();
//获取最佳提供者的名称
String provider = locationManager.getBestProvider(标准,真的);
//获取当前位置
位置myLocation = null;
尝试{
myLocation = locationManager.getLastKnownLocation(provider);
} catch(SecurityException e){
Log.e(PERMISSION_EXCEPTION,PERMISSION_NOT_GRANTED);
}
< br $>
//设置地图类型
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//获取当前位置的纬度
double latitude = myLocation.getLatitude();
//获取经度当前位置
double longitude = myLocation.getLongitude();
//为当前位置创建一个LatLng对象
LatLng latLng = new LatLng(纬度,经度);
//在Google地图中显示当前位置
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//放大谷歌地图
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(新MarkerOptions()。position(新LatLng) (纬度,经度))。title(你在这里!)。片段(考虑你自己));
}
}
我添加的依赖项是
defaultConfig {
applicationIdcom.example。 dell_pc.mymapapp
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName1.0
multiDexEnabled true
}
buildTyp es {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'< br $>
}
}
productFlavors {
}
}
依赖项{
编译fileTree(包括:['* .jar'],dir:'libs')
//编译'com.android.support:support-v4:23.2.0'
编译'com.android.support:appcompat- v7:23.2.0'
编译'com.android.support:design:23.2.0'
编译'com.google.android.gms:play-services-地图:8.4.0'
编译'com.google.android.gms:play-services:8.4.0'
compile'com.google.android.gms: play-services-ads:8.4.0'
编译'com.google.android.gms:play-services-gcm:8.4.0'
compile'com。 android.support:multidex:1.0.0'
编译'com.google.android.gms:play-services-identity:8.4.0'
}
600(ActivityThread.java:128)
在android.app.ActivityThread
H.handleMessage(ActivityThread.java:1161)
在android.os .Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4517)
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:993 )
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
引起:java.lang .NullPointerException
at com.example.dell_pc.mymapapp.MapsActivity.setUpMap(MapsActivity.java:108)
at com.example.dell_pc.mymapapp.MapsActivity.setUpMapIfNeeded(MapsActivity.java:60)
at com.example.dell_pc.mymapapp.MapsActivity.onCreate(MapsActivity.java:25)
at android.app.Activity.performCreate(Activity.java:4470)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1053)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access
I am Using Android Studio to display My Current location on Google Map.But getting following Errors:
Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.zze.zzaq
and
03-10 17:42:38.362 8762-8762/com.example.dell_pc.mymapapp E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell_pc.mymapapp/com.example.dell_pc.mymapapp.MapsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) at android.app.ActivityThread.access$600(ActivityThread.java:128) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4517) 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:993) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.dell_pc.mymapapp.MapsActivity.setUpMap(MapsActivity.java:108) at com.example.dell_pc.mymapapp.MapsActivity.setUpMapIfNeeded(MapsActivity.java:60) at com.example.dell_pc.mymapapp.MapsActivity.onCreate(MapsActivity.java:25) at android.app.Activity.performCreate(Activity.java:4470) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) at android.app.ActivityThread.access$600(ActivityThread.java:128) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4517) 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:993) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) at dalvik.system.NativeStart.main(Native Method)
please help.Thankx in advance.
What I have tried:
My MapActivity.java looks like below code
package com.example.dell_pc.mymapapp;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
*
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
*
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* 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.
*
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));
// Enable MyLocation Layer of Google Map
try {
mMap.setMyLocationEnabled(true);
} catch (SecurityException e) {
Log.e("PERMISSION_EXCEPTION","PERMISSION_NOT_GRANTED");
}
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation=null ;
try {
myLocation = locationManager.getLastKnownLocation(provider);
} catch (SecurityException e) {
Log.e("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED");
}
// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Get latitude of the current location
double latitude = myLocation.getLatitude();
// Get longitude of the current location
double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));
}
}
The dependencies that I have added are
defaultConfig {
applicationId "com.example.dell_pc.mymapapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.android.support:multidex:1.0.0'
compile 'com.google.android.gms:play-services-identity:8.4.0'
}
600(ActivityThread.java:128) at android.app.ActivityThread
H.handleMessage(ActivityThread.java:1161) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4517) 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:993) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.dell_pc.mymapapp.MapsActivity.setUpMap(MapsActivity.java:108) at com.example.dell_pc.mymapapp.MapsActivity.setUpMapIfNeeded(MapsActivity.java:60) at com.example.dell_pc.mymapapp.MapsActivity.onCreate(MapsActivity.java:25) at android.app.Activity.performCreate(Activity.java:4470) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) at android.app.ActivityThread.access
这篇关于什么是在android studio上运行谷歌地图应用程序时出错的原因是用api 22跟踪用户在真实设备上的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!