Pasindu Weerakoon
Pasindu Weerakoon

Reputation: 628

Data Passing Issue adapter to fragment in android (The default value <null> was returned)

I was trying to pass some data through an intent and bundle from one activity ** (actually it is Adapter of the activity) ** to fragment. All the string values passing without any issue to the fragment, but integer and double values are passed as null.

It shows this warning :

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

The codes are as follows:

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);

Here is SalesViewActivity.java (I think the problem occurs here)

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();

Here is the 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";

After getting the data to the fragment I'll assign them to the variables. Here is the code :

 @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);
        }
    }

I have tried lots of solution and read some articles, but did not work for me. If I did something wrong please help me to find it. Any Solutions highly appreciated. Thanks in advance.

Here are some references:

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

Upvotes: 0

Views: 1338

Answers (2)

Kozmotronik
Kozmotronik

Reputation: 2520

I had a similar issue while passing data using Bundle object. In my case I defined 2 keys like:

    public static final String ARGS_OPTIONS = "com.example.modalMenu.args.options";
    public static final String ARGS_ICONS = "com.example.modalMenu.args.options";

The problem was caused because I had copied and pasted the ARGS_ICONS from ARGS_OPTIONS in the IDE, leading to overwrite the ARGS_OPTIONS with the ARGS_ICONS content since the key strings are both the same. Then I changed the ARGS_ICONS string and boom! it is fixed:

    public static final String ARGS_OPTIONS = "com.example.modalMenu.args.options";
    public static final String ARGS_ICONS = "com.example.modalMenu.args.icons";

Conclusion: Copy-paste is a sweet poison so as the sugars to our body. Use it very carefully to not get poisonated your code.

Upvotes: 0

Son Truong
Son Truong

Reputation: 14173

In class where you start SalesViewActivity, you use

intent.putExtra(SalesViewFragment.ARG_INVOICE_ID, sales.get(position).getInvoiceInvoiceId());

Type of sales.get(position).getInvoiceInvoiceId() is Integer, but in SalesViewActivity you use

bundle.putString(SalesViewFragment.ARG_INVOICE_ID, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_ID));

You are casting an Integer to a String, that why the app throws a ClassCastException.

In SalesViewActivity change your code

bundle.putInt(SalesViewFragment.ARG_INVOICE_ID, getIntent().getIntExtra(SalesViewFragment.ARG_INVOICE_ID, 0));

instead of

bundle.putString(SalesViewFragment.ARG_INVOICE_ID, getIntent().getStringExtra(SalesViewFragment.ARG_INVOICE_ID));

For double value using getIntent().getDoubleExtra() instead of getIntent().getStringExtra().

Upvotes: 1

Related Questions