我试图通过意图传递一些数据并从一个活动**(实际上它是活动的适配器)**到片段。所有字符串值都没有任何问题地传递给片段,但是整数和双精度值都作为null传递。
它显示了这个警告:
key invoiceId expected String但value是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
在你开始使用SalesViewActivity
的课程中
intent.putExtra(SalesViewFragment.ARG_INVOICE_ID, sales.get(position).getInvoiceInvoiceId());
sales.get(position).getInvoiceInvoiceId()
的类型是Integer
,但在SalesViewActivity
中你使用
bundle.putString(SalesViewFragment.ARG_INVOICE_ID, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_ID));
你正在向Integer
施放String
,这就是为什么应用程序抛出ClassCastException
。
在SalesViewActivity
中更改您的代码
bundle.putInt(SalesViewFragment.ARG_INVOICE_ID, getIntent().getIntExtra(SalesViewFragment.ARG_INVOICE_ID, 0));
代替
bundle.putString(SalesViewFragment.ARG_INVOICE_ID, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_ID));
使用getIntent().getDoubleExtra()
而不是getIntent().getStringExtra()
获得双倍价值。