17

I am getting this error

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.e.www.i/com.e.www.i.Search}: java.lang.InstantiationException: class com.e.www.i.Search has no zero argument constructor

This is the below class :

public class Search extends Activity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;

    private RecyclerView mRecyclerView1;
    private RecyclerView.Adapter mAdapter1;
    Context mContext;

    public Search(Context context) {

        mContext = context; // I guess the error is here, but I need to define context for below MyAdapter
    }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(
                new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
        mAdapter = new MyAdapter(getDataSet(),mContext);
        mRecyclerView.setAdapter(mAdapter);

      }

private ArrayList<String> getDataSet() {
        ArrayList results = new ArrayList<DataObject>();
        for (int index = 0; index < 5; index++) {
            String obj = new String("User " + index);
            results.add(index, obj);
        }
        return results;
    }
Moudiz
  • 7,211
  • 22
  • 78
  • 156

5 Answers5

15

You need to define a no-args constructor, just as the error says:

public Search() {
    // No args constructor
}

The context that you need for your adapter is the Activity itself, you don't need to get it via the constructor. Just use this, since you are already in the context of an activity:

mAdapter = new MyAdapter(getDataSet(), this);

And then you can drop the overloaded constructor that you defined for your custom activity.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • how to define the context to past it to myAdapter ? – Moudiz Feb 13 '16 at 13:08
  • ah .. I was doing it `(getDataset(), this.Search)` i though `this,search` is the same as `this` – Moudiz Feb 13 '16 at 13:10
  • euh... what ? you need to use `this` (or `Search.this`), not `this.Search`. – Mohammed Aouf Zouag Feb 13 '16 at 13:12
  • do you have link explain the difference between both ? I am still doing my first app , so many things I need to learn – Moudiz Feb 13 '16 at 13:13
  • 1
    **You need to use** `ClassName.this` to refer to the activity's context if you were working inside of an anonymous class (like from within a `View.OnClickListener` e.g, when handling a button click), because using `this` in this case would refere to the anonymous class, not to the container activity. Check this: http://stackoverflow.com/questions/10102151/whats-the-difference-between-this-and-activity-this – Mohammed Aouf Zouag Feb 13 '16 at 13:14
3

In search, you created the constructor

public Search(Context context) {
    mContext = context;
}

Now, since you have a user defined constructor, your compiler does not provide you with any default constructors, so you need to define a parameter-less constructor yourself too.

public Search() {
    // Constructor body
}
Abhay Shukla
  • 349
  • 1
  • 12
2

remove Search(..) constructor and just pass conetext to adapter like

 mAdapter = new MyAdapter(getDataSet(),Search.this);

As you are already in Search Activity

M D
  • 47,665
  • 9
  • 93
  • 114
1

If any one is facing this problem with dependency injection. please add the annotation for activity . @AndroidEntryPoint . Also check if you added the required annotations for viewmodel/fragment/application etc.

Manohar
  • 22,116
  • 9
  • 108
  • 144
0

If Argument is a must for example IntentService

public BaseIntentService() {
    super("IntentService");
}

Otherwise

public Search() {
    // Constructor body
}
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154