我是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'.
编译失败;有关详细信息,请参阅编译器错误输出。
AM获取错误“无法应用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;
}
最佳答案
除了注释中指出的问题之外,在设置适配器之前,您将从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
}