我是Android新手,我正在尝试创建一个应用程序,根据他们的状态显示人员,在应用程序中使用带有碎片的导航抽屉。
当我试图列出特定类别的人时,我收到了错误。即
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()无法应用"
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class Past extends Fragment {
ListView list;
String[] itemname ={
"John",
"Mike",
"Ria",
"jack",
"leo",
};
Integer[] imgid={
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
};
public Past() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_past, container, false);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
//here am getting an error
list=(ListView)getView().findViewById(R.id.list);
list.setAdapter(adapter);
/*list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
}
});*/
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
下面给出了CustomListAdapter.java代码
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;
};
}
从MainActivity调用的片段在下面给出
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;
}
答案 0 :(得分:1)
除了注释中指出的问题之外,您在设置适配器之前从onCreateView()方法返回,因此无法访问代码问题。
设置对膨胀视图的引用,使用该视图查找ListView,然后返回方法末尾的View:
@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
}
答案 1 :(得分:0)
通过this.getActivity()
代替this
。如果您仍然收到错误,请使用新错误更新您的问题。