【发布时间】:2018-09-16 13:26:04
【问题描述】:
我试图通过 intent 和 bundle 从一个活动**(实际上是活动的适配器)** 传递一些数据到片段。所有字符串值都可以毫无问题地传递给片段,但整数和双精度值都作为 null 传递。
显示此警告:
键 invoiceId 应为字符串,但值为 java.lang.Integer。这 返回默认值
尝试转换生成的内部异常:
java.lang.ClassCastException: java.lang.Integer 无法转换为 java.lang.String
代码如下:
Intent intent = new Intent(mContext, SalesViewActivity.class);
intent.putExtra(SalesViewFragment.ARG_INVOICE_ID, sales.get(position).getInvoiceInvoiceId());
intent.putExtra(SalesViewFragment.ARG_INVOICE_CODE, sales.get(position).getInvoiceInvoiceCode());
intent.putExtra(SalesViewFragment.ARG_CUSTOMER_ID, sales.get(position).getInvoiceCustomerId());
intent.putExtra(SalesViewFragment.ARG_INVOICE_ORDER_ID, sales.get(position).getInvoiceOrderId());
intent.putExtra(SalesViewFragment.ARG_SALES_PERSON, sales.get(position).getInvoiceSalesPerson());
intent.putExtra(SalesViewFragment.ARG_INVOICE_CUSTOMER_NAME, sales.get(position).getCustomerCustomerName());
intent.putExtra(SalesViewFragment.ARG_INVOICE_TOTAL, sales.get(position).getInvoiceTotalAmount());
intent.putExtra(SalesViewFragment.ARG_START_TIME, sales.get(position).getInvoiceStartTimestamp());
intent.putExtra(SalesViewFragment.ARG_INVOICE_DISCOUNT, sales.get(position).getInvoiceDiscountTotal());
intent.putExtra(SalesViewFragment.ARG_INVOICE_CASH_DISCOUNT, sales.get(position).getInvoiceCashDiscount());
intent.putExtra(SalesViewFragment.ARG_INVOICE_DISCOUNT_TRADE, sales.get(position).getInvoiceDiscountTrade());
intent.putExtra(SalesViewFragment.ARG_INVOICE_DISCOUNT_PARENTAGE, sales.get(position).getInvoiceDiscountParentage());
intent.putExtra(SalesViewFragment.ARG_INVOICE_STATUS, sales.get(position).getInvoiceStatus());
intent.putExtra(SalesViewFragment.ARG_INVOICE_REMARKS, sales.get(position).getInvoiceRemarks());
mContext.startActivity(intent);
这里是SalesViewActivity.java (我认为问题出现在这里)
Fragment mFragment = null;
mFragment = new SalesViewFragment();
Bundle bundle = new Bundle();
bundle.putString(SalesViewFragment.ARG_INVOICE_ID, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_ID));
bundle.putString(SalesViewFragment.ARG_INVOICE_CODE, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_CODE));
bundle.putString(SalesViewFragment.ARG_CUSTOMER_ID, getIntent().getStringExtra(SalesViewFragment.ARG_CUSTOMER_ID));
bundle.putString(SalesViewFragment.ARG_INVOICE_ORDER_ID, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_ORDER_ID));
bundle.putString(SalesViewFragment.ARG_SALES_PERSON, getIntent().getStringExtra(SalesViewFragment.ARG_SALES_PERSON));
bundle.putString(SalesViewFragment.ARG_INVOICE_CUSTOMER_NAME, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_CUSTOMER_NAME));
bundle.putString(SalesViewFragment.ARG_INVOICE_TOTAL, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_TOTAL));
bundle.putString(SalesViewFragment.ARG_START_TIME, getIntent().getStringExtra(SalesViewFragment.ARG_START_TIME));
bundle.putString(SalesViewFragment.ARG_INVOICE_DISCOUNT, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_DISCOUNT));
bundle.putString(SalesViewFragment.ARG_INVOICE_CASH_DISCOUNT, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_CASH_DISCOUNT));
bundle.putString(SalesViewFragment.ARG_INVOICE_DISCOUNT_TRADE, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_DISCOUNT_TRADE));
bundle.putString(SalesViewFragment.ARG_INVOICE_DISCOUNT_PARENTAGE, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_DISCOUNT_PARENTAGE));
bundle.putString(SalesViewFragment.ARG_INVOICE_STATUS, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_STATUS));
bundle.putString(SalesViewFragment.ARG_INVOICE_REMARKS, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_REMARKS));
mFragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout, mFragment).commit();
这里是 salesViewFragment.java
public static final String ARG_INVOICE_ID = "invoiceId";
public static final String ARG_INVOICE_CODE = "code";
public static final String ARG_CUSTOMER_ID = "customerId";
public static final String ARG_INVOICE_ORDER_ID = "orderId";
public static final String ARG_SALES_PERSON = "salesPerson";
public static final String ARG_INVOICE_CUSTOMER_NAME = "customerName";
public static final String ARG_INVOICE_TOTAL = "total";
public static final String ARG_START_TIME = "startTime";
public static final String ARG_INVOICE_DISCOUNT = "discount";
public static final String ARG_INVOICE_CASH_DISCOUNT = "discountCash";
public static final String ARG_INVOICE_DISCOUNT_TRADE = "discountTrade";
public static final String ARG_INVOICE_DISCOUNT_PARENTAGE = "discountPerce";
public static final String ARG_INVOICE_STATUS = "invoiceStatus";
public static final String ARG_INVOICE_REMARKS = "invoiceRemarks";
将数据获取到片段后,我会将它们分配给变量。 这是代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_INVOICE_ID)) {
invoiceID = getArguments().getInt(ARG_INVOICE_ID);
code = getArguments().getString(ARG_INVOICE_CODE);
cusID = getArguments().getInt(ARG_CUSTOMER_ID);
orderId = getArguments().getInt(ARG_INVOICE_ORDER_ID);
salesPerson = getArguments().getString(ARG_SALES_PERSON);
cusName = getArguments().getString(ARG_INVOICE_CUSTOMER_NAME);
total = getArguments().getDouble(ARG_INVOICE_TOTAL);
startTime = getArguments().getString(ARG_START_TIME);
discounts = getArguments().getDouble(ARG_INVOICE_DISCOUNT);
discoutCash = getArguments().getDouble(ARG_INVOICE_CASH_DISCOUNT);
discoutTrade = getArguments().getDouble(ARG_INVOICE_DISCOUNT_TRADE);
discoutParcent = getArguments().getDouble(ARG_INVOICE_DISCOUNT_PARENTAGE);
invoiceStatus = getArguments().getString(ARG_INVOICE_STATUS);
invoiceRemarks = getArguments().getString(ARG_INVOICE_REMARKS);
}
}
我尝试了很多解决方案并阅读了一些文章,但对我不起作用。如果我做错了什么,请帮助我找到它。任何解决方案高度赞赏。提前致谢。
以下是一些参考资料:
why getStringExtra doesn't give the proper output?
Key _id expected Parcelable but value was java.lang.Long. while passing Parceable object between Activity https://github.com/fechanique/cordova-plugin-fcm/issues/253
【问题讨论】:
-
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String。例如,bundle.putString(SalesViewFragment.ARG_INVOICE_ID, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_ID));但您正试图将其转换为 int
标签: android android-fragments android-intent bundle