【发布时间】:2018-06-18 13:25:38
【问题描述】:
我将 PayPal SDK 添加到我的应用程序中,但我遇到了一个问题。 当我想查看付款详情时,应用程序会崩溃。
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
public class PaymentDeatils extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_deatils);
//Getting Intent
Intent intent = getIntent();
try {
JSONObject jsonDetails = new
JSONObject(intent.getStringExtra("PaymentDetails"));
showDetails(jsonDetails.getJSONObject("response"),
intent.getStringExtra("PaymentAmount"));
} catch (JSONException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void showDetails(JSONObject jsonDetails, String paymentAmount)
throws JSONException {
//Views
TextView txtId = (TextView) findViewById(R.id.txtId);
TextView textViewStatus= (TextView) findViewById(R.id.txtAnount);
TextView txtStatus = (TextView) findViewById(R.id.txtStatus);
//Showing the details from json object
txtId .setText(jsonDetails.getString("id"));
textViewStatus.setText(jsonDetails.getString("state"));
txtStatus.setText(paymentAmount);
}
}
这里有问题
JSONObject jsonDetails = new JSONObject(intent.getStringExtra("PaymentDetails"));
intent.getStringExtra("PaymentDetails") 可能为空。我该如何解决?
控制台出错。
java.lang.NullPointerException: Attempt to invoke virtual method 'int
java.lang.String.length()' on a null object reference
【问题讨论】:
-
将代码粘贴到您发送意图的位置
-
如果您处于这种状态,我想您已经从 Paypal 获得了对先前活动的 onActivityResult 的成功响应。问题是
intent.getStringExtra("PaymentDetails")结果为空。检查密钥 PaymentDetails 是否正确(没有错字)。还要检查你是否已经从之前的活动中调用了intent.putExtra("PaymentDetails", paymentDetails)。