android - (intent.getStringExtra ("PaymentDetails") 为空

标签 android json paypal

我将 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

最佳答案

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

关于android - (intent.getStringExtra ("PaymentDetails") 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48169398/

相关文章:

php - 带有送货和账单地址的 Paypal 表格变量

android - 如何在给定 oAuth token 的情况下实例化 com.google.api.services.calendar.Calendar 对象?

java - (Java、JSON)使用 Youtube API 从包含已删除项目的播放列表中获取数据

php - 在 Android 中对 Json 输出进行排序

php - 如何在 PayPal 快速结帐中禁用 "note to buyer"?

PayPal SetExpressCheckout 与付款按钮

java - 更新 RecyclerView onChildChanged Firebase

Android 发布 APK 获取错误 NoSuchFieldError

java - 异步任务技术

ios - iOS NSJSONSerialization返回null