-5
對不起,如果有人已經問過這個問題,但是我一直收到我的應用程序出錯。我甚至無法運行,因爲我有一個錯誤說「無法訪問的聲明」。在片段中使用getActivity返回後無法訪問的語句
這是我在我的片段
package com.example.dasilvadd.students;
public class OngletCours extends Fragment
{
DatabaseHelper dbhelper = new DatabaseHelper(getActivity());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.ongletcours, container, false);
return rootView;
List<Cours> listeCours;
ArrayList<String> arrayList;
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),R.layout.ongletcours, arrayList);
ListView l1= (ListView) getView().findViewById(R.id.ListCours);
listeCours= dbhelper.getAllCours();
if (!listeCours.isEmpty())
{
String item;
String[] cours = {""};
arrayList=new ArrayList<>(Arrays.asList(cours));
l1.setAdapter(adapter);
for(int i = 0; i < listeCours.size(); i++) {
item = listeCours.get(i).getCours();
arrayList.add(item);
adapter.notifyDataSetChanged();
}
}
else{
Toast t = Toast.makeText(getActivity(),"Error",Toast.LENGTH_LONG);
t.show();
}
}
}
代碼時,我對我的ArrayAdapter
做getActivity()
,我List<Cours> listeCours;
不可達,我不知道爲什麼......
我現在正在收到另一個錯誤.. ArrayList arrayList; ArrayAdapter adapter = new ArrayAdapter (getActivity(),R.layout.ongletcours,arrayList); 它說,我的Arraylist可能不會被初始化 並感謝您的回報! –
ListView l1 =(ListView)rootView.findViewById(R.id.ListCours); –
一切正常,非常感謝你! –