剛剛接觸到android,我正在嘗試創建一個應用程序,該應用程序將根據用戶的狀態在正在使用帶抽象碎片的導航抽屜中顯示人員。CustomListAdapter中的CustomListAdapter()無法應用
當我試圖列出特定類別的人的時候,我得到了一個錯誤。即
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'.
Compilation failed; see the compiler error output for details.
我得到錯誤 「CustomListAdapter()中CustomListAdapter不能應用於」 下方
從MainActivity稱爲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;
};
}
片段
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代碼給出以下給出
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;
}
你傳入片段上下文活動上下文而初始化適配器即CustomListAdapter adapter = new CustomListAdapter(this,itemname,imgid);嘗試將其更改爲CustomListAdapter adapter = new CustomListAdapter(getActivity(),itemname,imgid);並嘗試 – Raghavendra
我已經試過它給出了一個不可測量的統計錯誤 – kiki
你發佈的日誌說這個錯誤,所以這個更改更新日誌。 – Raghavendra