数据传递问题适配器到 android 中的片段(返回默认值 <null>)
Data Passing Issue adapter to fragment in android (The default value <null> was returned)
我试图通过一个 intent 和 bundle 从一个 activity **(实际上它是 activity 的适配器)传递一些数据 * * 碎片化。所有字符串值都毫无问题地传递给片段,但整数和双精度值作为空值传递。
显示此警告:
Key invoiceId expected String but value was a java.lang.Integer. The
default value was returned
Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.Integer cannot be cast to 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
的 class 中,您使用
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()
。
我在使用 Bundle
object 传递数据时遇到了类似的问题。在我的例子中,我定义了 2 个键,例如:
public static final String ARGS_OPTIONS = "com.example.modalMenu.args.options";
public static final String ARGS_ICONS = "com.example.modalMenu.args.options";
问题是因为我从ARGS_OPTIONS中复制并粘贴了ARGS_ICONS到IDE,导致ARGS_OPTIONS被[=20=覆盖了] 内容,因为键字符串都是相同的。然后我更改了 ARGS_ICONS 字符串和繁荣!已修复:
public static final String ARGS_OPTIONS = "com.example.modalMenu.args.options";
public static final String ARGS_ICONS = "com.example.modalMenu.args.icons";
结论:Copy-paste 是一种甜蜜的毒药,就像我们 body 的糖一样。小心使用它,不要让你的代码中毒。
我试图通过一个 intent 和 bundle 从一个 activity **(实际上它是 activity 的适配器)传递一些数据 * * 碎片化。所有字符串值都毫无问题地传递给片段,但整数和双精度值作为空值传递。
显示此警告:
Key invoiceId expected String but value was a java.lang.Integer. The default value was returned
Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.Integer cannot be cast to 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
的 class 中,您使用
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()
。
我在使用 Bundle
object 传递数据时遇到了类似的问题。在我的例子中,我定义了 2 个键,例如:
public static final String ARGS_OPTIONS = "com.example.modalMenu.args.options";
public static final String ARGS_ICONS = "com.example.modalMenu.args.options";
问题是因为我从ARGS_OPTIONS中复制并粘贴了ARGS_ICONS到IDE,导致ARGS_OPTIONS被[=20=覆盖了] 内容,因为键字符串都是相同的。然后我更改了 ARGS_ICONS 字符串和繁荣!已修复:
public static final String ARGS_OPTIONS = "com.example.modalMenu.args.options";
public static final String ARGS_ICONS = "com.example.modalMenu.args.icons";
结论:Copy-paste 是一种甜蜜的毒药,就像我们 body 的糖一样。小心使用它,不要让你的代码中毒。