gpt4 book ai didi

java - 无法从内部类检索 LatLng

转载 作者:行者123 更新时间:2023-12-01 10:15:34 26 4
gpt4 key购买 nike

我正在从 MainActivity 向名为 MapClass 的类发送 IntentService 调用。 MapClass 有一个名为 MapClassInner 的内部类,它计算用户的当前坐标。但是,外部类包含 onHandleIntent 方法,该方法最终将使用广播将坐标发送回 MainActivity。我的问题是 - 如何访问 MapClassInner 的内部变量?我很确定答案很简单,但我已经在这个项目上连续工作了 2 个小时,所以我的想法变得有点不对劲。

这是代码(MapClass.java):

import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import meetup.be2015.gcm_meetup.MainActivity.MyLocationReceiver;


public class MapClass extends IntentService {
public static final String REQUEST_STRING = "myRequest";
public static final String RESPONSE_LAT = "lat";
public static final String RESPONSE_LNG = "lng";
public GoogleMap map;

public MapClass() {
super("MapClass");
}

private class MapClassInner extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;
private GoogleMap mgoogleMap;
private LatLng latLng;

private GoogleApiClient client;

public MapClassInner(GoogleMap map){
this.mgoogleMap = map;
}

@Override
public void onMapReady(GoogleMap googleMap) {
Log.d("ON_MAP_READY","In OnMapReady");
mgoogleMap = googleMap;
mgoogleMap.setMyLocationEnabled(true); //Sets location to current position
buildGoogleApiClient();
mGoogleApiClient.connect();
}

protected synchronized void buildGoogleApiClient() {
Log.d("BUILD", "In buildClient()");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

@Override
public void onConnected(Bundle bundle) {
Log.d("ON_CONNECTED","In onConnected()");
Location MLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (MLastLocation != null) {
latLng = new LatLng(MLastLocation.getLatitude(), MLastLocation.getLongitude());


} else {
Log.d("onConnected", "Value of LatLng is NULL");
latLng = new LatLng(0, 0);
}
}

public LatLng getLatLng(){
return this.latLng;
}

@Override
public void onConnectionSuspended(int i) {
//Notify
Log.d("ConnectionSuspended", "Connection Suspended. Status: " + i);
mgoogleMap.clear();
mGoogleApiClient.disconnect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//Notify
Log.d("ConnectionFailed", "Connection Failed. Status: " + connectionResult.toString());
mgoogleMap.clear();
mGoogleApiClient.disconnect();
}
}



@Override
protected void onHandleIntent(Intent intent) {
//Instantiating the inner class
MapClassInner inner = new MapClassInner(map);

//Trying to access the inner LatLng variable
LatLng coords = inner.getLatLng();

//Gives me a NullPointerException

//Converting them to String values
String lat = String.valueOf(coords.latitude);
String lng = String.valueOf(coords.longitude);

//Sending broadcast Intent
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MyLocationReceiver.PROCESS_RESPONSE);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(RESPONSE_LAT, lat);
broadcastIntent.putExtra(RESPONSE_LNG, lng);
sendBroadcast(broadcastIntent);
}
}

如何从内部类获取LatLng?我正在考虑使用 EventBus 来解决这个问题。它会起作用吗?是的,请对我宽容一点;我对 Android 和 Java 还比较陌生。

最佳答案

为了简化所有代码,我所做的就是创建一个静态的“AppData.java”类,并且可以从包中的任何类访问。然后我把这样的数据放在那里。

public class AppData
{

private static final AppData appData = new AppData(); // Make a static instance so it is not re-inited.

// My data
public Location lastKnownLocation;

/**
* Method to return the static instance of the AppData.
*
* @return An AppData object that is static so you can get/set global data across the application.
*/
public static AppData getInstance() {return appData;}
}

您可以添加 getter 和 setter。我还给了我一个带有上下文的构造函数,并且只从我的主 Activity 中调用了一次,它让我可以在类中实现首选项管理,这对于我保留在那里进行持久化的更复杂的数据很有帮助。

要在任何地方使用它,只需执行以下操作:

AppData appData = AppData.getInstance();
appData.lastKnownLocation = <whatever your code does>

等等...

关于java - 无法从内部类检索 LatLng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35919758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com