我在我的Fragment java类中使用sqlite中的自动完整文本侦听器,不幸的是我有一个错误nullpointerexception。这是我的logcat:
05-12 09:15:05.987: E/AndroidRuntime(20901): FATAL EXCEPTION: main
05-12 09:15:05.987: E/AndroidRuntime(20901): Process: iot.bayadcenter.slidingmenu, PID: 20901
05-12 09:15:05.987: E/AndroidRuntime(20901): java.lang.NullPointerException
05-12 09:15:05.987: E/AndroidRuntime(20901): at iot.bayadcenter.slidingmenu.billers.Payment.getItemsFromDb(Payment.java:271)
05-12 09:15:05.987: E/AndroidRuntime(20901): at iot.bayadcenter.searchbarsqlite.CustomAutoCompleteTextChangedListener.onTextChanged(CustomAutoCompleteTextChangedListener.java:42)
05-12 09:15:05.987: E/AndroidRuntime(20901): at android.widget.TextView.sendOnTextChanged(TextView.java:7610)
05-12 09:15:05.987: E/AndroidRuntime(20901): at android.widget.TextView.handleTextChanged(TextView.java:7672)
这是我的CustomAutoCompleteTextChangedListener.java
public class CustomAutoCompleteTextChangedListener implements TextWatcher{
public static final String TAG = "CustomAutoCompleteTextChangedListener.java";
Context context;
public CustomAutoCompleteTextChangedListener(Context context){
this.context = context;
}
@Override
public void onTextChanged(CharSequence userInput, int start, int before, int count) {
// if you want to see in the logcat what the user types
Log.e(TAG, "User input: " + userInput);
Payment mainActivity = new Payment();
// query the database based on the user input
mainActivity.item = mainActivity.getItemsFromDb(userInput.toString());//HERE IS LINE 42
// update the adapater
mainActivity.myAdapter.notifyDataSetChanged();
mainActivity.myAdapter = new ArrayAdapter<String>(mainActivity.getActivity(), android.R.layout.simple_dropdown_item_1line, mainActivity.item);
mainActivity.myAutoComplete.setAdapter(mainActivity.myAdapter);
}
}
这是我的片段(Payment.java)
public class Payment extends Fragment {
CustomAutoCompleteView myAutoComplete;
// adapter for auto-complete
ArrayAdapter<String> myAdapter;
// for database operations
DatabaseHandler databaseH;
// just to add some initial value
String[] item = new String[] {"Please search..."};
Context ctx = getActivity();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myAutoComplete = (CustomAutoCompleteView) rootView.findViewById(R.id.myautocomplete);
// add the listener so it will tries to suggest while the user types
myAutoComplete.addTextChangedListener(new CustomAutoCompleteTextChangedListener(getActivity()));
// set our adapter
myAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, item);
myAutoComplete.setAdapter(myAdapter);
这是另一部分:
// this function is used in CustomAutoCompleteTextChangedListener.java
public String[] getItemsFromDb(String searchTerm){
// add items on the array dynamically
List<MyObject> products = databaseH.read(searchTerm);//HERE IS MY LINE 27
int rowCount = products.size();
String[] item = new String[rowCount];
int x = 0;
for (MyObject record : products) {
item[x] = record.objectName;
x++;
}
return item;
}
答案 0 :(得分:0)
由于这里有一大堆例外,让我们从第一个可见的开始。第271行产生NullPointerException
,这是
List<MyObject> products = databaseH.read(searchTerm);
我想这是因为databaseH
为空。检查是否正确打开连接。
这一行也让我想知道Payment mainActivity = new Payment();
。在自定义类中创建Fragment
的新实例,该实例实现TextWatcher
...不要调用Fragment
中的任何方法,除非它不在屏幕上。此外,Fragment
用于附加到Activity
,但不用于处理数据库方法。看一下模式MVC。
我认为修复错误没有太大意义,最好重组一切。
希望它至少有一点帮助。