Android谷歌位置/地图 - 获取当前位置,我不断收到错误(运行时错误),不知道为什么?

时间:2014-03-31 17:20:18

标签: android eclipse google-maps location currentlocation

好的,我正在创建一个使用Google服务(位置服务)查找用户当前位置的应用。当我从主要活动转到下一个活动时,我收到一个错误,我不知道为什么。我有一段时间以前的工作,但我改变了一些东西(我记不住了),现在它不起作用了。基本上我单击主屏幕上的开始按钮,然后我的程序关闭。由开始按钮启动的活动用于获取用户的当前位置,然后继续查找用户当前位置与5个设置位置之间的距离。然后它处理了这些距离。

这是我的logCat :( 我很抱歉,但我刚加入,我无法添加图片

E / MoreInfoHPW_ViewGroup(20140):父视图不是TextView E / MoreInfoHPW_ViewGroup(20140):父视图不是TextView E / AndroidRuntime(20140):致命异常:主要 E / AndroidRuntime(20140):处理:com.emma.unsaltedsights,PID:20140 E / AndroidRuntime(20140):java.lang.RuntimeException:无法启动活动ComponentInfo {com.emma.unsaltedsights / com.emma.unsaltedsights.PickLocation}: 显示java.lang.NullPointerException

E / AndroidRuntime(20140):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)

E / AndroidRuntime(20140):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)

E / AndroidRuntime(20140):在android.app.ActivityThread.access $ 800(ActivityThread.java:157) E / AndroidRuntime(20140):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1247)

E / AndroidRuntime(20140):在android.os.Handler.dispatchMessage(Handler.java:102) E / AndroidRuntime(20140):在android.os.Looper.loop(Looper.java:157) E / AndroidRuntime(20140):在android.app.ActivityThread.main(ActivityThread.java:5293) E / AndroidRuntime(20140):at java.lang.reflect.Method.invokeNative(Native Method) E / AndroidRuntime(20140):at java.lang.reflect.Method.invoke(Method.java:515) E / AndroidRuntime(20140):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1259)

E / AndroidRuntime(20140):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) E / AndroidRuntime(20140):at dalvik.system.NativeStart.main(Native Method) E / AndroidRuntime(20140):引起:java.lang.NullPointerException E / AndroidRuntime(20140):at com.emma.unsaltedsights.PickLocation.findingDistance(PickLocation.java:921)

E / AndroidRuntime(20140):at com.emma.unsaltedsights.PickLocation.onCreate(PickLocation.java:59) E / AndroidRuntime(20140):在android.app.Activity.performCreate(Activity.java:5389) E / AndroidRuntime(20140):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)

E / AndroidRuntime(20140):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)

E / AndroidRuntime(20140):... 11更多

这是我的MainActivity Class(开始屏幕):

公共类MainActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
public void launchApp(View view){
    Intent intent = new Intent(this, PickLocation.class);
    startActivity(intent);
)

}

这是我的PickLocation类(女儿活动):

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(isGooglePlay()){
            setContentView(R.layout.pick_location);
            setUp();
            findingDistance();
            setButtonsText(distances);          
        }       
    }

private void setUp(){

        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        provider = lm.getBestProvider(new Criteria(), true);
        if(provider == null ){
            onProviderDisabled(provider);
        }
        //get current location
        currentloc = lm.getLastKnownLocation(provider);
        if(currentloc != null){
            onLocationChanged(currentloc);  
        }
}

public void findingDistance(){


        Location jis = createLocation("flp",-6.284172,106.789093); //double
        Location lippo = createLocation("flp",-6.260516,106.811823); 
        Location senayan = createLocation("flp",-6.227877,106.797661);
        Location gandaria = createLocation("flp",-6.245164, 106.782597);
        Location ranch = createLocation("flp",-6.255405,106.811500);

        disJ = currentloc.distanceTo(jis); //float

        disL = currentloc.distanceTo(lippo);

        disS = currentloc.distanceTo(senayan); 

        disG = currentloc.distanceTo(gandaria);

        disR = currentloc.distanceTo(ranch); 

        intdisJ = (int) disJ;
        intdisL = (int) disL;
        intdisS = (int) disS;
        intdisG = (int) disG;
        intdisR = (int) disR;

        distances = new Integer[5];
        distances[0] = intdisJ;
        distances[1] = intdisL;
        distances[2] = intdisS;
        distances[3] = intdisG;
        distances[4] = intdisR;

    }

private boolean isGooglePlay()  { // Services Connected
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if(status == ConnectionResult.SUCCESS){
            return(true);
        }else{
            ((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
        }
            return(false);
    }

@Override
    public void onLocationChanged(Location location) {//I think this is the error source
        currentloc = location;

    }

我是Android的总菜鸟,这实际上是我的第一个应用程序,所以请彻底解答。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并设法解决了这个问题。

我在导入google-play-services_lib文件夹后开始工作,视频人员向您展示如何在10分钟左右进行导入:https://www.youtube.com/watch?v=awX5T-EwLPc