Android Lambda表达式返回null

Android Lambda表达式返回null,android,lambda,google-distancematrix-api,Android,Lambda,Google Distancematrix Api,我正在编写一个lambda表达式来将给定的纬度和经度转换为地址。表达式应该以坐标为参数,并返回其相应的地址。但是,返回的值为null。以下是我的班级: public class LambdaDeclarations { String loc; private static final String TAG = "LambdaDeclarations"; public CoordinatesToAddressInterface convert = (latitude, longitude,

我正在编写一个lambda表达式来将给定的纬度和经度转换为地址。表达式应该以坐标为参数,并返回其相应的地址。但是,返回的值为null。以下是我的班级:

public class LambdaDeclarations {

String loc;

private static final String TAG = "LambdaDeclarations";

public CoordinatesToAddressInterface convert = (latitude, longitude, context) -> {
    RequestQueue queue = Volley.newRequestQueue(context);

    Log.d(TAG, "onCreate: Requesting: Lat: "+latitude+" Lon: "+longitude);
    String url ="https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins="+latitude+","+longitude+"&destinations="+latitude+","+longitude+"&key=AIzaSyCdKSW0glin4h9sGYa_3hj0L83zI0NsNRo";
    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            (String response) -> {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray destinations = jsonObject.getJSONArray("destination_addresses");
                    Log.d(TAG, "GETRequest: JSON Object: "+destinations.toString());
                    String location = destinations.toString();
                    Log.d(TAG, "Location: "+location);
                    setLocation(location);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }, error -> Log.d(TAG, "onErrorResponse: That didn't work!"));
    queue.add(stringRequest);
    return getLocation();
};


public String getLocation() {
    return loc;
}

public void setLocation(String location) {
    this.loc = location;
    }
}
以下是logcat的输出:

09-16 10:31:09.160 26525-26525/com.rmit.tejas.mad_foodtruck_2 D/LambdaDeclarations: GETRequest: JSON Object: ["77 State Route 32, West Melbourne VIC 3003, Australia"]
Location: ["77 State Route 32, West Melbourne VIC 3003, Australia"]
09-16 10:31:09.176 26525-26525/com.rmit.tejas.mad_foodtruck_2 D/LambdaDeclarations: GETRequest: JSON Object: ["111 Adderley St, West Melbourne VIC 3003, Australia"]
Location: ["111 Adderley St, West Melbourne VIC 3003, Australia"]
09-16 10:31:09.177 26525-26525/com.rmit.tejas.mad_foodtruck_2 D/LambdaDeclarations: GETRequest: JSON Object: ["4\/326 William St, Melbourne VIC 3000, Australia"]
Location: ["4\/326 William St, Melbourne VIC 3000, Australia"]
以下是我的用法:

myViewHolder.textView3.setText("Location: i->"+i+" add: "+l.convert.toAddress(trackingInfos.get(i).getLatitude(),trackingInfos.get(i).getLongitude(),context));
l
是类
lambdeclarations
的对象,以下是相关接口:

public interface CoordinatesToAddressInterface {
String toAddress(double latitude, double longitude, Context context);
}
当我尝试从相关适配器打印坐标时,它们被正确打印。因此位置设置正确,但当我尝试从另一个类访问它时,它会显示字符串的空值。您能建议另一种方法从表达式中提取位置吗?

首先,它只是一个匿名类实现,它被设计为用作方法或类参数,并解决匿名类的隐藏问题。
因此,在您的情况下,您根本不需要它,只需像往常一样将
CoordinatesAddressInterface
接口实现为命名类即可

第二,您使用的截击错误,您提供给
StringRequest
的第一个lambda(下文将是call-response-callback)将在HTTP请求完成但返回语句

return getLocation();
在执行
setLocation(location)
之前,甚至在执行响应回调之前,都会立即返回null,这就是为什么每次调用
convert()
时都会返回null,尽管您仍然可以看到打印的日志,因为响应回调无论如何都会执行(假设请求成功)

要正确使用响应回调,您必须在回调中更新UI,就像下面这样

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
public static final String TAG = "MyAdapter";
private RequestQueue mQueue;

public MyAdapter(Context context) {
    this.mQueue = Volley.newRequestQueue(context);
}

public RequestQueue getMyAdapterRequestQueue() {
    return this.mQueue;
}

    ...

@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
    String url ="some url";

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            (String response) -> {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray destinations = jsonObject.getJSONArray("destination_addresses");
                    Log.d(TAG, "GETRequest: JSON Object: "+destinations.toString());
                    String location = destinations.toString();
                    Log.d(TAG, "Location: "+location);
                    // update UI
                    holder.mTextView.setText(location);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }, error -> Log.d(TAG, "onErrorResponse: That didn't work!"));

    stringRequest.setTag(TAG);
    mQueue.add(stringRequest);
}

取消请求后将不会调用响应回调

包含lambda声明的类是什么?你如何使用你的lambda?您能提供使用它的代码吗?我已经添加了lambda类的全部代码,以及lambda函数的用法。你能告诉我我可能做错了什么吗?
@Override
protected void onStop () {
    super.onStop();
    if (myAdapter.getMyAdapterRequestQueue() != null) {
        myAdapter.getMyAdapterRequestQueue().cancelAll(MyAdapter.TAG);
    }
}