조각에서 데이터베이스 쿼리를 호출하는 방법을 찾으려면 도움이 필요합니다.조각에서 데이터베이스 도우미 호출하기
나는 그것에 관하여 약간 화제를 읽고 getActivity()는 저를 돕지 않는 제일 해결책 인 것을 보인다.
java.lang.NullPointerException이 : '를 java.util.List를 가상 메서드를 호출하는 시도 [내 패키지 이름] .DatabaseHelper 여기
내 소스 코드//package
//imports
public class CategoriesFragment extends Fragment
{
DatabaseHelper db;
ArrayList<String> mCategoryList;
DynamicListView lvCategories;
StableArrayAdapter stableArrayAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_categories, container, false);
lvCategories = (DynamicListView) rootView.findViewById(R.id.lvCategories);
db = new DatabaseHelper(getActivity());
mCategoryList = new ArrayList<>();
stableArrayAdapter = new StableArrayAdapter(getActivity(), R.layout.text_view, mCategoryList);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
lvCategories.setCheeseList(mCategoryList);
lvCategories.setAdapter(stableArrayAdapter);
lvCategories.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvCategories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//not defined
}
});
//If listview is empty
if (lvCategories.getCount() == 0) {
lvCategories.setVisibility(View.GONE);
} else {
lvCategories.setVisibility(View.VISIBLE);
}
}catch (Exception e) {
Toast.makeText(getActivity(), String.valueOf(e), Toast.LENGTH_LONG).show();
}
}
public void AddCategoryItem(String word, String reportId) {
//get category item by name
Category cat = db.getCategory(word);
//Adding both ids to table CategoryReport
db.createCategoryReport(new CategoryReport(cat.getId(), Integer.parseInt(reportId)));
}
public String FormatCategory(String word) {
String formated = word;
int occurence = 1;
if (mCategoryList.contains(formated)) {
//Salon, Salon #2, Salon #3, etc.
while (mCategoryList.contains(formated)) {
occurence++;
//formated and word are separated to prevent 'Chambre #1 #2' output syntax
formated = word + " #" + String.valueOf(occurence);
}
}
return formated;
}
public void RefreshList(String report_id) {
//database
List<Category> categories = db.getCategoriesByReport(report_id);
//populate liste
mCategoryList.clear();
for (Category c : categories) {
mCategoryList.add(FormatCategory(c.getName()));
}
//refresh
stableArrayAdapter = new StableArrayAdapter(getActivity(), R.layout.text_view, mCategoryList);
lvCategories.setCheeseList(mCategoryList);
lvCategories.setAdapter(stableArrayAdapter);
lvCategories.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
if (lvCategories.getCount() == 0) {
lvCategories.setVisibility(View.GONE);
} else {
lvCategories.setVisibility(View.VISIBLE);
}
}
}
그리고 오류 메시지입니다 .getCategoriesByReport (java.lang.String) '을 null 객체 참조에 추가합니다.
이 오류는 RefreshList 방법의 첫번째 라인에서 발생된다.
나는 쿼리 오류에 관한 것이 아니라는 것을 안다. 왜냐하면 필자가 직접적으로 테스트 한 바있다. 당신의 도움에 대한
감사 :
getView(). getContext(); – Sush
어디에서 RefreshList 메서드를 호출하고 있습니까? – Stallion
어디에서 RefreshList를 호출하나요? – dindinii