Android java.lang.实例化异常:类com.e没有零参数构造函数
我得到了这个错误Android java.lang.实例化异常:类com.e没有零参数构造函数,android,Android,我得到了这个错误 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 Activ
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;
}
公共类搜索扩展活动{
私人回收视图mRecyclerView;
专用回收器查看适配器mAdapter;
私人回收站视图mRecyclerView1;
专用回收器视图适配器mAdapter1;
语境;
公共搜索(上下文){
mContext=context;//我猜错误在这里,但我需要为下面的MyAdapter定义上下文
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u recycler\u视图);
mRecyclerView=(RecyclerView)findViewById(R.id.my\u recycler\u视图);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(
新的LinearLayoutManager(此,LinearLayoutManager.HORIZONTAL,false));
mAdapter=newmyadapter(getDataSet(),mContext);
mRecyclerView.setAdapter(mAdapter);
}
私有ArrayList getDataSet(){
ArrayList结果=新建ArrayList();
对于(int-index=0;index<5;index++){
字符串obj=新字符串(“用户”+索引);
结果。添加(索引,obj);
}
返回结果;
}
您需要定义一个无args构造函数,正如错误所说:
public Search() {
// No args constructor
}
适配器所需的上下文
是活动
本身,您不需要通过构造函数获取它。只需使用此
,因为您已经处于活动的上下文中:
mAdapter = new MyAdapter(getDataSet(), this);
然后,您可以删除为自定义活动定义的重载的构造函数。删除搜索(…)构造函数
,只需将conetext
传递给适配器
即可
mAdapter = new MyAdapter(getDataSet(),Search.this);
由于您已经在搜索活动中,因此创建了构造函数
public Search(Context context) {
mContext = context;
}
现在,因为您有一个用户定义的构造函数,所以编译器不提供任何默认构造函数,所以您也需要自己定义一个无参数构造函数
public Search() {
// Constructor body
}
如果必须使用参数,例如
IntentService
public BaseIntentService() {
super("IntentService");
}
否则
public Search() {
// Constructor body
}
如何定义将其传递给myAdapter的上下文?啊..我当时正在做
(getDataset(),this.Search)
我虽然这个,但是Search
与这个
是一样的…什么?你需要使用这个
(或者搜索.this
),而不是此。搜索
。你有链接解释两者之间的区别吗?我仍在做我的第一个应用程序,有很多东西需要学习你需要使用类名。如果你在匿名类中工作,此引用活动的上下文(比如在视图中。OnClickListener
例如,在处理按钮单击时),因为在这种情况下使用this
将引用匿名类,而不是容器活动。检查此项:尝试创建零参数构造函数或其和旧问题的可能副本