【问题标题】:(intent.getStringExtra("PaymentDetails") is null(intent.getStringExtra("PaymentDetails") 为空
【发布时间】: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)

标签: android json paypal


【解决方案1】:

正如 Abdul 所说,我们还需要查看发送活动。此外,getIntent.getStringExtra 还支持在 key 后面设置一个默认值,这至少可以减少 null 异常崩溃的可能性。

【讨论】:

    猜你喜欢
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 2016-12-16
    相关资源
    最近更新 更多