我需要片段内的 Android 集成帮助,当代码在 Activity 中实现时工作正常,但在我的情况下,我需要在片段中实现它。
我正在尝试从 Fragment 调用 dropin ui,但无法这样做。
我遵循的步骤:- 1) 我使用 get_braintree_token(rootview.getContext()) 获取了 Braintree 令牌。
2) 获得令牌后,我将它传递给 onBraintreeSubmit(braintreeToken,viewb) 3) 当它到达 onBraintreeSubmit 函数中的 startActivityForResult() 时,BrainTree UI 没有膨胀。它直接移动到 onActivityResult(),结果代码为 1 和一些随机请求代码。我在下面写了我的函数。
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_map, container, false);
get_braintree_token(rootview.getContext());
return rootview;
}
public void get_braintree_token(final Context viewb)
{
StringRequest stringRequest = new StringRequest(Request.Method.GET, "My server URL", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
braintreeToken = response.toString();
Log.e("Braintreetoken","token is:"+braintreeToken);
onBraintreeSubmit(braintreeToken, viewb);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
braintreeToken = null;
}
});
// Add the request to the RequestQueue.
requestQueue.add(stringRequest);
}
public void onBraintreeSubmit(String token, Context c) {
Log.e("tokenreceived",token);
DropInRequest dropInRequest = new DropInRequest();
dropInRequest.clientToken(token);
Intent drop = dropInRequest.getIntent(c);
startActivityForResult(drop, BRAINTREE_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BRAINTREE_REQUEST_CODE) {
if (resultCode == 1) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
// use the result to update your UI and send the payment method nonce to your server
} else if (resultCode == Activity.RESULT_CANCELED) {
// the user canceled
} else {
// handle errors here, an exception may be available in
Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
}
}
}