Map Stop单击按钮时(尝试调用虚拟方法'java.lang.String java.lang.String.trim())

时间:2019-03-19 17:57:13

标签: java android

我正在尝试从服务器检索纬度和经度,并将其显示在我的Google地图活动中。但是我从服务器获取的值是String,所以我从String更改为Double,以便能够在Google地图位置中使用。我已经知道了位置,但是当我单击“显示地图”时,应用程序停止工作,并且logact中出现错误。请看一下。

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

    findViewById(R.id.btn_login).setOnClickListener(this);
    findViewById(R.id.btn_secret).setOnClickListener(this);
    findViewById(R.id.btn_Location).setOnClickListener(this);
}
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_login:
            login();
            break;
        case R.id.btn_secret:
            getSecret();
            break;
        case R.id.btn_Location:
            getLocation();
            break;
        case R.id.btn_Map:
            getMap();
            break;
    }

}

private static String token;
private  String lat;
private  String lng;



public String getLat() {
    return this.lat;
}

public String  getLng(){
    return this.lng;
}


private void login() {
    Login login = new Login("johndoe@example.com", "aaaaaa");
    Call<User> call = userClient.login(login);

    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            if (response.isSuccessful()) {
                Toast.makeText(MainActivity.this, response.body().getToken(), Toast.LENGTH_SHORT).show();
                token = response.body().getToken();
            } else {
                Toast.makeText(MainActivity.this, "login not correct", Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
        }
    });
}

private void getSecret() {
    Call<ResponseBody> call = userClient.getSecret(token);

    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                try {
                    Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(MainActivity.this, "token is not correct", Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();

        }
    });
}

private void getLocation() {
    Call<Location> call = userClient.getLocation();

    call.enqueue(new Callback<Location>() {
        @Override
        public void onResponse(Call<Location> call, Response<Location> response) {
            if (response.isSuccessful()) {
                Toast.makeText(MainActivity.this, response.body().getGps_latitude(), Toast.LENGTH_SHORT).show();
                Toast.makeText(MainActivity.this, response.body().getGps_longitude(), Toast.LENGTH_LONG).show();
                lat = response.body().getGps_latitude();
                lng = response.body().getGps_longitude();

            } else {
                Toast.makeText(MainActivity.this, "login not correct", Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onFailure(Call<Location> call, Throwable t) {
            Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();

        }

    });


}

private void getMap(){
    Intent intent = new Intent(this,GMap.class);
    startActivity(intent);
}

}

#Map Activity
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    MainActivity mainActivity = new MainActivity();


    double Lat = Double.parseDouble(mainActivity.getLat());
    double Lng = Double.parseDouble(mainActivity.getLng());

    LatLng sydney = new LatLng(Lat, Lng);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Home"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}



#Error Logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:539)
    at com.example.login_retro.Api.model.GMap.onMapReady(GMap.java:48)
    at com.google.android.gms.maps.zzak.zza(Unknown Source:2)
    at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source:12)
    at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source:12)
    at android.os.Binder.transact(Binder.java:628)
    at fv.b(:com.google.android.gms.dynamite_mapsdynamite@15180051@15.1.80 (040408-237499370):14)
    at com.google.android.gms.maps.internal.bd.a(:com.google.android.gms.dynamite_mapsdynamite@15180051@15.1.80 (040408-237499370):4)
    at com.google.maps.api.android.lib6.impl.bk.run(:com.google.android.gms.dynamite_mapsdynamite@15180051@15.1.80 (040408-237499370):4)
    at android.os.Handler.handleCallback(Handler.java:809)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:7383)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)

0 个答案:

没有答案