我是android的新手,我在使用LocationManager时遇到了麻烦 我试着几乎所有的评论,应用程序正在显示地图。这意味着我已经从谷歌api获得了一个密钥,其他任何工作 然后我想得到我当前的位置。
我尝试取消注释这些
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
我从我的设备收到强制关闭消息..
这是我的活动
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends FragmentActivity {
private GoogleMap mMap;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
public Criteria criteria = new Criteria();
public String provider = locationManager.getBestProvider(criteria,true);
public Location myLocation = locationManager.getLastKnownLocation(provider);
public Double latid = myLocation.getLatitude();
public Double longi = myLocation.getLongitude();
public LatLng Coba = new LatLng(latid,longi);
protected Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMap.setMyLocationEnabled(true);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(Coba, 16));
}
}
logcat的?
12-28 16:01:38.830:E / Launcher(268):setWindowOpaque() 12-28 16:01:42.630:E / UinboxLogger(3310):[UinboxReceiver] onReceive()>>上下文是> android.app.ReceiverRestrictedContext@40523808 12-28 16:01:44.010:E / AndroidRuntime(3337):致命异常:主要 12-28 16:01:44.010:E / AndroidRuntime(3337):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.hellomap / com.example.hellomap.MainActivity}:java.lang.IllegalStateException:System onCreate()之前活动不可用的服务 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573) 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935) 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.os.Handler.dispatchMessage(Handler.java:99) 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.os.Looper.loop(Looper.java:130) 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.app.ActivityThread.main(ActivityThread.java:3687) 12-28 16:01:44.010:E / AndroidRuntime(3337):at java.lang.reflect.Method.invokeNative(Native Method) 12-28 16:01:44.010:E / AndroidRuntime(3337):at java.lang.reflect.Method.invoke(Method.java:507) 12-28 16:01:44.010:E / AndroidRuntime(3337):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 12-28 16:01:44.010:E / AndroidRuntime(3337):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 12-28 16:01:44.010:E / AndroidRuntime(3337):at dalvik.system.NativeStart.main(Native Method) 12-28 16:01:44.010:E / AndroidRuntime(3337):引起:java.lang.IllegalStateException:在onCreate()之前,活动不可用的系统服务 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.app.Activity.getSystemService(Activity.java:3536) 12-28 16:01:44.010:E / AndroidRuntime(3337):at com.example.hellomap.MainActivity。(MainActivity.java:30) 12-28 16:01:44.010:E / AndroidRuntime(3337):at java.lang.Class.newInstanceImpl(Native Method) 12-28 16:01:44.010:E / AndroidRuntime(3337):at java.lang.Class.newInstance(Class.java:1409) 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.app.Instrumentation.newActivity(Instrumentation.java:1021) 12-28 16:01:44.010:E / AndroidRuntime(3337):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565) 12-28 16:01:44.010:E / AndroidRuntime(3337):... 11更多 12-28 16:01:44.020:E /(171):Dumpstate> /数据/数/ dumpstate_app_error 12-28 16:01:49.609:E / Launcher(268):setWindowOpaque() 12-28 16:01:49.679:E / Launcher(268):MTP-LAUNCHER:媒体扫描尚未完成。
答案 0 :(得分:0)
在xml中使用
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/crntlocation"
class="com.google.android.gms.maps.SupportMapFragment" />
活动
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
and
//创建标准对象以检索提供者
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
答案 1 :(得分:0)
我认为这是因为你正试图启动
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
在调用onCreate方法之前。
//在onCreate()
中启动这些组件 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
public String provider = locationManager.getBestProvider(criteria,true);
public Location myLocation = locationManager.getLastKnownLocation(provider);
public Double latid = myLocation.getLatitude();
public Double longi = myLocation.getLongitude();
public LatLng Coba = new LatLng(latid,longi);
您的mMap可以返回null,请使用Try&amp;抓住了。