我收到此错误java.lang.InstantiationException:类COM.E没有无参数的构造
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
这是下面的类:
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;
}
尝试创建一个无参数的构造函数的 –
可能的复制[java.lang.InstantiationException:类没有无参数的构造函数(http://stackoverflow.com/questions/29947038/java-lang-instantiationexception-class -has-no-zero-argument-constructor) – sud007
其旧问题 – Moudiz