0

好的,所以我正在创建一个使用 Google 服务(定位服务)来查找用户当前位置的应用程序。当我从主要活动转到下一项活动时出现错误,我真的不知道为什么。前一阵子我让它工作了,但是我改变了一些东西(我不记得了),现在它不起作用了。基本上我单击主屏幕上的开始按钮,然后我的程序关闭。由开始按钮启动的activity,应该是获取用户当前位置,然后继续查找用户当前位置与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):at android.app.ActivityThread.access$800(ActivityThread.java:157) E/AndroidRuntime(20140):at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)

E/AndroidRuntime(20140):at android.os.Handler.dispatchMessage(Handler.java:102) E/AndroidRuntime(20140):at android.os.Looper.loop(Looper.java:157) E/AndroidRuntime(20140) : 在 android.app.ActivityThread.main(ActivityThread.java:5293) E/AndroidRuntime(20140): 在 java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(20140): 在 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):at 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 类(开始屏幕):

公共类 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 的菜鸟,这实际上是我的第一个应用程序,所以请仔细回答。

4

1 回答 1

0

我有同样的问题,只是设法解决它。

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

于 2014-04-09T22:57:32.093 回答