我正在制作一个需要在MainActivity中获取结果的应用程序,当我调用getData()
时,我从实例化中获取数据,但是当我尝试计算位置时,它返回null。
这是我的Location.class
public class Location implements GoogleApiClient.OnConnectionFailedListener {
private List<Integer> mTypeList;
private ArrayList<String> listItems;
ArrayAdapter<String> adapter;
ListView listView;
LatLng mLatLang;
private PlaceLikelihood pl;
private Context mContext;
private GoogleApiClient mGoogleApiClient;
public Location(Context context) {
this.listItems = new ArrayList<>();
this.mTypeList = new ArrayList<>();
mContext = context;
buildGoogleApiClient();
connect();
getData();
}
public void buildGoogleApiClient(){
mGoogleApiClient = new GoogleApiClient
.Builder(mContext)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
}
public void connect() {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
public void getData() {
if (ActivityCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
final PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(@NonNull PlaceLikelihoodBuffer placeLikelihoods) {
pl = placeLikelihoods.get(0);
Log.e("singleplacetype", "onResult: "+pl.getPlace().getPlaceTypes());
Log.e("singleliketype2", "onResult: "+pl.getLikelihood());
Log.e("singleliketype3", "onResult: "+pl.getPlace().getName());
mTypeList = pl.getPlace().getPlaceTypes();
//Latitude and longitude
mLatLang = pl.getPlace().getLatLng();
String latlongshrink = mLatLang.toString();
Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(latlongshrink);
while (m.find()) {
Log.e("Test", "" + m.group(1));
latlongshrink = m.group(1);
}
for (int i = 0; i < mTypeList.size(); i++) {
try {
nearestPlaces(latlongshrink, getPlaceTypeForValue(mTypeList.get(i)));
} catch (Exception e) {
e.printStackTrace();
}
try {
listItems.add(pl.getPlace().getName() + " " + pl.getLikelihood() + " " + mTypeList + " " + getPlaceTypeForValue(mTipoList.get(i)));
Log.e("singleList", "onResult: "+listItems );
} catch (Exception e) {
e.printStackTrace();
}
}
placeLikelihoods.release();
}
});
}
public Integer getTypes(int posicion) {
Log.e("listType", "getTypes: "+mTypeList);
int type;
if(mTypeList.size()>0){
type = mTypeList.get(posicion);
}else{
return null;
}
return type;
}
现在,这是我的Location.class,我这样在MainActivity中调用该类
Location loc = new Location(getApplicationContext());
我还有一个与第一类一起计算某些值的类,因此在实例化Location.class之后,我可以成功地看到getData()
在返回到我的某些地方时已执行。
json.class
private Integer getPosition(Location loc) {
return loc.getTypes(0);
}
所以,现在再次进入我的MainActivity中,我试图从Location.class但从json.class中获取值,因为getPosition
在被执行之前会做其他事情
MainActivity.class
Json json = new Json(getApplicationContext());
Log.e("jsonClassData", "onCreate: "+json.getPosition(loc));
现在,此行返回null,但是我不知道为什么自创建getData()以来,getTypes的内部mTypeList的大小为0,而由于mTypeList.size为0,返回的空值是
如果在MainActivity.class中,我称此行,它也将返回null
Location loc = new Location(getApplicationContext());
loc.getType(0);
似乎getData()没有将值保存在mTypeList内
为什么会这样?
谢谢
答案 0 :(得分:0)
Location loc = new Location(getApplicationContext());
您不应该使用new
运算符来创建活动。
您应该为此使用意图。
因此取消所有这些公共成员函数。
答案 1 :(得分:0)
问题是getData()
是一个等待结果的异步操作。现在,如果我不等待该数据完成,那么getTypes
就没有任何价值。现在,感谢MikeM。我只是使用简单的interface
解决了这个问题。
我创建了一个名为PlaceSuccessListener的接口
public interface PlaceSuccessListener {
void onPlaceFound(int placeFound);
}
在我的未决结果的最后一个调用了它
private PlaceSuccessListener mPlaceSuccessListener;
getData();
.....
placeLikelihoods.release();
mPlaceSuccessListener.onPlaceFound(200);
}
然后在我的MainActivity
中,我只是等待结果完成才能访问我的数据。我实现了PlaceSuccessListener
并附加了setInterface(this)
@Override
public void onPlaceFound(int placeFound) {
if(placeFound==200){
Log.e("jsonClassData", "onCreate: "+json.getPosition(loc));
}
}