Search code examples
androidandroid-intentandroid-activityput

This error is coming while doing intent Cannot resolve method 'putExtra(java.lang.String, <lambda parameter>)'?


I am getting this error when doing intent. I don't know why it is coming. I need to go to the fragment to activity. I need to go to the next activity with the api in this application. I have tried many times and I am not getting the answer.**Cannot resolve method 'putExtra(java.lang.String, <lambda parameter>)'**I have given the image below How to do fragment to activity intent i am new android devloper

 package com.kannada.newspaper.india.utils;

import static com.kannada.newspaper.india.Constant.EXTRA_OBJC;
import static com.kannada.newspaper.india.Constant.getApiUrl;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;


import com.kannada.newspaper.india.AppConfig;
import com.kannada.newspaper.india.Constant;
import com.kannada.newspaper.india.MainActivity;
import com.kannada.newspaper.india.R;
import com.kannada.newspaper.india.activities.ActivityCategoryDetail;
import com.kannada.newspaper.india.adapters.GalleryAdapter;
import com.kannada.newspaper.india.model.Category;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FragmentCategory extends Fragment {
    private Call<CallbackHome> callbackCall = null;
    SharedPref sharedPref;
    private View  root_view;
    public static final String EXTRA_OBJC = "key.EXTRA_OBJC";

    private GalleryAdapter adapterCategory;
    private GridView gridView;
    private List<Category> mensWears;
    private GalleryAdapter adapter;
    private Category category;
    public FragmentCategory() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,
                             Bundle savedInstanceState) {
        requestAction();
        category = (Category) getActivity().getIntent().getSerializableExtra(Constant.EXTRA_OBJC);

        // Inflate the layout for this fragment
        root_view = inflater.inflate(R.layout.fragment_phones,container,false);
        ((TextView) root_view.findViewById(R.id.txt_title_category)).setText(getResources().getString(R.string.home_title_category));



        return root_view;


    }



    private void requestAction() {

        new Handler().postDelayed(this::requestHomeData, Constant.DELAY_TIME);
    }


    private void requestHomeData() {
        this.callbackCall = RestAdapter.createAPI(getApiUrl).getHome(AppConfig.REST_API_KEY);
        this.callbackCall.enqueue(new Callback<CallbackHome>() {
            public void onResponse(Call<CallbackHome> call, Response<CallbackHome> response) {
                CallbackHome responseHome = response.body();
                if (responseHome == null || !responseHome.status.equals("ok")) {

                    return;
                }
                displayData(responseHome);


            }

            public void onFailure(Call<CallbackHome> call, Throwable th) {
                Log.e("onFailure", th.getMessage());
                if (!call.isCanceled()) {

                }
            }
        });
    }
    private void displayData(CallbackHome responseHome) {
        displayCategory(responseHome.category);

    }


    private void displayCategory(List<Category> list) {
        GridView gridView = (GridView) root_view.findViewById(R.id.gridHolder);




        adapterCategory = new GalleryAdapter(getActivity(), list);
        gridView.setAdapter(adapterCategory);
        GalleryAdapter.setOnItemClickListener((v, obj, position) -> {
            Intent intent = new Intent(getActivity(), ActivityCategoryDetail.class);
            intent.putExtra(EXTRA_OBJC, obj);
            startActivity(intent);

        });
        LinearLayout lyt_category = root_view.findViewById(R.id.lyt_category);
        if (list.size() > 0) {
//            lyt_category.setVisibility(View.VISIBLE);
        } else {
//            lyt_category.setVisibility(View.GONE);
        }
    }

}

enter image description here

GalleryAdapter adapter

public class GalleryAdapter extends BaseAdapter {
    private Context context;

    private List<Category>  mensWears;


    public GalleryAdapter(Context context, List<Category> mensWears) {
        this.context = context;
        this.mensWears = mensWears;
    }

    public static void setOnItemClickListener(Object o) {
    }

    @Override
    public int getCount() {
        return mensWears.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i,View view,ViewGroup viewGroup) {
        final Category mensWear = mensWears.get(i);

        if (view == null) {
            final LayoutInflater layoutInflater = LayoutInflater.from(context);
            view = layoutInflater.inflate(R.layout.custom_gallery_layout, null);
        }
        //For text
        TextView prdId = view.findViewById(R.id.category_name);
        ImageView imageView = view.findViewById(R.id.category_image);
//        prdId.setText(prdId.toString());
        Picasso.get()
                .load(getApiUrl + "/upload/category/" + mensWears.get(i).category_image())
                .placeholder(R.drawable.ic_thumbnail)
                .into(imageView);


        prdId.setText(mensWears.get(i).getItemName());


//        //For images
//        final ImageView imageView = view.findViewById(R.id.name);
//        if(!TextUtils.isEmpty(mensWear.getItemName())){
//
////            Picasso.with(context).load(imageUrlFromServer+mensWear.category_image())
////                    .into(imageView);

        return view;
    }

}

Solution

  • You cannot put Object type in putExtra , it has to be either serailized, string, double and other primitive type.

    You can do like this:

     Category category = (Category)adapter.getItemAtPosition(pos);
    

    or this should also work:

    Category category = (Category) obj
    

    then,

    intent.putExtra(EXTRA_OBJC,category)
    

    Note: Your Category class should implement parcelable or serilizable to be passed as an intent.

    public class Category implements Serializable {
    ..........}