Android 无法应用CustomListAdapter中的CustomListAdapter()
我是android新手,我正在尝试创建一个应用程序,它将根据用户的状态显示用户,在应用程序中,我使用带有片段的导航抽屉 当我试图列出某人的特定类别时,我犯了一个错误。i、 eAndroid 无法应用CustomListAdapter中的CustomListAdapter(),android,android-fragments,Android,Android Fragments,我是android新手,我正在尝试创建一个应用程序,它将根据用户的状态显示用户,在应用程序中,我使用带有片段的导航抽屉 当我试图列出某人的特定类别时,我犯了一个错误。i、 e Error:(50, 57) error: incompatible types: Past cannot be converted to Activity Note: Some input files use or override a deprecated API. Note: Recompile with -Xlin
Error:(50, 57) error: incompatible types: Past cannot be converted to Activity
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
编译失败;有关详细信息,请参阅编译器错误输出
获取错误“CustomListAdapter中的CustomListAdapter()无法应用”
传递
this.getActivity()
而不是this
。如果仍然出现错误,请使用新错误更新您的问题。除了注释中指出的问题外,在设置适配器之前,您还将从onCreateView()方法返回,因此存在无法访问的代码问题
设置对膨胀视图的引用,使用该视图查找ListView,然后在方法末尾返回该视图:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_past, container, false); //modified
CustomListAdapter adapter = new CustomListAdapter(getActivity(), itemname, imgid);
list=(ListView) rootView.findViewById(R.id.list); //modified
list.setAdapter(adapter);
return rootView; //added
}
初始化适配器时,您正在将片段上下文传递给活动上下文,即CustomListAdapter=new CustomListAdapter(this,itemname,imgid);尝试将其更改为CustomListAdapter=new CustomListAdapter(getActivity(),itemname,imgid);tryi已经试过了,它给出了一个不可搜索的语句错误。你发布的日志显示了这个错误,所以这次更改会更新日志。编辑后,我得到了相同的错误“unreachable statement”错误:(47,27)错误:unreachable statement错误:(62,5)错误:缺少返回语句注意:某些输入文件使用或重写不推荐使用的API。注意:使用-Xlint重新编译:不推荐使用以获取详细信息。错误:任务执行失败:app:compiledBugJavaWithJavaC。“>编译失败;有关详细信息,请参阅编译器错误输出。@Raghavendra此问题已解决。感谢支持意味着lotI am ha同样的问题,但是IDE也不支持getActivity。
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description "+itemname[position]);
return rowView;
};
}
else if(i==1){
switch (j){
case 0:
Past past = new Past();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameholde,past);
fragmentTransaction.commit();
break;
case 1:
Current current = new Current();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameholde, current);
fragmentTransaction.commit();
break;
case 2:
Area area = new Area();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameholde, area);
fragmentTransaction.commit();
break;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_past, container, false); //modified
CustomListAdapter adapter = new CustomListAdapter(getActivity(), itemname, imgid);
list=(ListView) rootView.findViewById(R.id.list); //modified
list.setAdapter(adapter);
return rootView; //added
}