Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context must not be null

Tags:

I get this error in the LogCat when I try to run the application.

java.lang.IllegalArgumentException: Context must not be null.

This happens when I add the Picasso code in the class.

here is the adpater.Java

It says the Context is null, I have read other posts but I could not find a solution.

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{
private List<App> mApps;
private boolean mHorizontal;
private boolean mPager;
private Context mContext;


public Adapter(Context ctx, boolean horizontal, boolean pager, List<App> apps) {
    mHorizontal = horizontal;
    mApps = apps;
    mPager = pager;
    this.mContext=ctx;
}



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {




        if (mPager) {
            return new ViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.adaper_pager, parent, false));
        } else {
            return mHorizontal ? new ViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.adapter, parent, false)) :
                    new ViewHolder(LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.adapter_vertical, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        App app = mApps.get(position);
        //holder.imageView.setImageResource(app.getDrawable());
        Picasso.with(mContext).load(app.getLink()).into(holder.imageView);
        holder.nameTextView.setText(app.getName());
        holder.ratingTextView.setText(String.valueOf(app.getRating()));

    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return mApps.size();
    }



    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        public TextView nameTextView;
        public TextView ratingTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.igOne);
            nameTextView = (TextView) itemView.findViewById(R.id.nameTV);
            ratingTextView = (TextView) itemView.findViewById(R.id.rate);
        }

    }

}

Please Help. some say it is because the imageView is null. I have no idea about that.

MainActivity

public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {


    private static final String TAG = "MainActivity";
    private Context mContext = MainActivity.this;
    private static final int ACTIVITY_NUM = 0;


    public static final String ORIENTATION = "orientation";

    private RecyclerView mRecyclerView;
    private boolean mHorizontal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "OnCreate: Starting MainActivity");
        setupBottomNavigationView();


        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);

        if (savedInstanceState == null) {
            mHorizontal = true;
        } else {
            mHorizontal = savedInstanceState.getBoolean(ORIENTATION);
        }

        setupAdapter();

    }


    /**
     * Setup Bottom Navigation View
     */

    private void setupBottomNavigationView() {
        Log.d(TAG, "setupBottomNavigationView: Setting up Bottom Navigation View");
        BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottom_nav_bar);
        BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
        BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationViewEx);
        Menu menu = bottomNavigationViewEx.getMenu();
        MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
        menuItem.setChecked(true);


    }

    public void click(View v) {
        Intent mIntent = null;
        switch (v.getId()) {
            case R.id.serve:
                mIntent = new Intent(this, SearchActivity.class);
                break;

            case R.id.imageButton2:
                mIntent = new Intent(this, SearchActivity.class);
                break;
        }
        startActivity(mIntent);
    }


    ///////////

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(ORIENTATION, mHorizontal);
    }

    private void setupAdapter() {
        List<App> apps = getApps();

        SnapAdapter snapAdapter = new SnapAdapter();
        if (mHorizontal) {
            snapAdapter.addSnap(new Snap(Gravity.CENTER_HORIZONTAL, "Snap Start", apps));
            snapAdapter.addSnap(new Snap(Gravity.START, "Snap Middle", apps));
            snapAdapter.addSnap(new Snap(Gravity.END, "Snap End", apps));
            snapAdapter.addSnap(new Snap(Gravity.CENTER, "Pager snap", apps));
        } else {
            snapAdapter.addSnap(new Snap(Gravity.CENTER_VERTICAL, "Snap center", apps));
            snapAdapter.addSnap(new Snap(Gravity.TOP, "Snap top", apps));
            snapAdapter.addSnap(new Snap(Gravity.BOTTOM, "Snap bottom", apps));
        }

        mRecyclerView.setAdapter(snapAdapter);
    }

    private List<App> getApps() {

        List<App> apps = new ArrayList<>();
        apps.add(new App("Google+", "http://uupload.ir/files/aud7_brickone.jpg", 4.6f));

        return apps;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        return false;
    }


}

SnapAdapter

public class SnapAdapter extends RecyclerView.Adapter<SnapAdapter.ViewHolder> implements GravitySnapHelper.SnapListener {

    public static final int VERTICAL = 0;
    public static final int HORIZONTAL = 1;
    private Context mContext;



    private ArrayList<Snap> mSnaps;
    // Disable touch detection for parent recyclerView if we use vertical nested recyclerViews
    private View.OnTouchListener mTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    };

    public SnapAdapter() {
        mSnaps = new ArrayList<>();
    }

    public void addSnap(Snap snap) {
        mSnaps.add(snap);
    }

    @Override
    public int getItemViewType(int position) {
        Snap snap = mSnaps.get(position);
        switch (snap.getGravity()) {
            case Gravity.CENTER_VERTICAL:
                return VERTICAL;
            case Gravity.CENTER_HORIZONTAL:
                return HORIZONTAL;
            case Gravity.START:
                return HORIZONTAL;
            case Gravity.TOP:
                return VERTICAL;
            case Gravity.END:
                return HORIZONTAL;
            case Gravity.BOTTOM:
                return VERTICAL;
        }
        return HORIZONTAL;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = viewType == VERTICAL ? LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_snap_vertical, parent, false)
                : LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_snap, parent, false);

        if (viewType == VERTICAL) {
            view.findViewById(R.id.recycle_view).setOnTouchListener(mTouchListener);
        }

        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Snap snap = mSnaps.get(position);
        holder.snapTextView.setText(snap.getText());

        if (snap.getGravity() == Gravity.START || snap.getGravity() == Gravity.END) {
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
                    .recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
            holder.recyclerView.setOnFlingListener(null);
            new GravitySnapHelper(snap.getGravity(), false, this).attachToRecyclerView(holder.recyclerView);
        } else if (snap.getGravity() == Gravity.CENTER_HORIZONTAL) {
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
                    .recyclerView.getContext(), snap.getGravity() == Gravity.CENTER_HORIZONTAL ?
                    LinearLayoutManager.HORIZONTAL : LinearLayoutManager.VERTICAL, false));
            holder.recyclerView.setOnFlingListener(null);
            new LinearSnapHelper().attachToRecyclerView(holder.recyclerView);
        } else if (snap.getGravity() == Gravity.CENTER) { // Pager snap
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
                    .recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
            holder.recyclerView.setOnFlingListener(null);
            new PagerSnapHelper().attachToRecyclerView(holder.recyclerView);
        } else { // Top / Bottom
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
                    .recyclerView.getContext()));
            holder.recyclerView.setOnFlingListener(null);
            new GravitySnapHelper(snap.getGravity()).attachToRecyclerView(holder.recyclerView);
        }


         holder.recyclerView.setAdapter(new Adapter(mContext, snap.getGravity() == Gravity.START
            || snap.getGravity() == Gravity.END
            || snap.getGravity() == Gravity.CENTER_HORIZONTAL,
            snap.getGravity() == Gravity.CENTER, snap.getApps()));
    }

    @Override
    public int getItemCount() {
        return mSnaps.size();
    }

    @Override
    public void onSnap(int position) {
        Log.d("Snapped: ", position + "");
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView snapTextView;
        public RecyclerView recyclerView;

        public ViewHolder(View itemView) {
            super(itemView);
            snapTextView = (TextView) itemView.findViewById(R.id.snapTextView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycle_view);
        }

    }
}
like image 672
J. Doe Avatar asked Aug 19 '17 08:08

J. Doe


2 Answers

java.lang.IllegalArgumentException: Context must not be null.

A constructor in Java is a block of code similar to a method that's called when an instance of an object is created.

You should pass Context

     private Context mContext;

    public Adapter(Context ctx,boolean horizontal, boolean pager, List<App> apps) {
        mHorizontal = horizontal;
        mApps = apps;
        mPager = pager;
        this.mContext=ctx; // Call here


}

Logcat Throws

the error is: Error:(110, 40) error: constructor Adapter in class Adapter cannot be applied to given types; required: boolean,boolean,List,Context found: boolean,boolean,List reason: actual and formal argument lists differ in length

Pass Value LIKE

new Adapter(Your_Activity.this, boolean, boolean , List<App>);
like image 121
IntelliJ Amiya Avatar answered Oct 12 '22 12:10

IntelliJ Amiya


Call Adapter like this

  Adapter  mAdapter = new Adapter (Youractivity_name.this, boolean, boolean , list);

and change the constructor to

 public Adapter(Context context ,boolean horizontal, boolean pager, List<App> apps) {
    mHorizontal = horizontal;
    mApps = apps;
    mPager = pager;
    mContext = context;
}
like image 26
Mayank Bhatnagar Avatar answered Oct 12 '22 13:10

Mayank Bhatnagar