我有一个片段,在该片段中,我刚刚添加了一个搜索视图和列表视图,并尝试执行searhcQuery,但无法获得所需的结果 evrytime 正在搜索视图中键入任何内容,我的应用程序正在崩溃,这就是我实现搜索视图的方式 -
Search_View.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String Text) {
notesAdapter.getFilter().filter(Text);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
notesAdapter.getFilter().filter(newText);
return true;
}
});
这就是我得到的错误——
01-21 22:53:29.995 1676-1836/? E/bst_gps: bstgps: failed to open device. err: 2.
01-21 22:53:31.535 6763-6763/? E/InputEventSender: Exception dispatching finished signal.
01-21 22:53:31.535 6763-6763/? E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
01-21 22:53:31.545 6763-6763/? E/MessageQueue-JNI: java.lang.NullPointerException
at pb.myapp.TabFragment2$3.onQueryTextChange(TabFragment2.java:176)
at android.widget.SearchView.onTextChanged(SearchView.java:1153)
at android.widget.SearchView.access$2000(SearchView.java:92)
at android.widget.SearchView$11.onTextChanged(SearchView.java:1638)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.handleTextChanged(TextView.java:7467)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9183)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:223)
at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
at android.widget.TextView.doKeyDown(TextView.java:5532)
at android.widget.TextView.onKeyDown(TextView.java:5343)
at android.widget.AutoCompleteTextView.onKeyDown(AutoCompleteTextView.java:716)
at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
at android.view.View.dispatchKeyEvent(View.java:7696)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.support.v4.view.ViewPager.dispatchKeyEvent(ViewPager.java:2567)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
at android.app.Activity.dispatchKeyEvent(Activity.java:2418)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:224)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3852)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3558)
at and
01-21 22:53:31.545 6763-6763/? D/AndroidRuntime: Shutting down VM
01-21 22:53:31.545 6763-6763/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d68b20)
01-21 22:53:31.545 1880-1979/? D/BstCommandProcessor-Application: Application crash has been observed.
01-21 22:53:31.545 1880-1979/? W/BstCommandProcessor-Application: in sendHttpRequest, requestType is of CRASH_APP type but one of the requiredInfo is NULL, crashedApp = com.bluestacks.BstCommandProcessor.BstCrashedAppInfo@528037b8
01-21 22:53:31.545 1880-1979/? D/BstCommandProcessor-Application: in sendHttpRequest, request to send to (fqdn): http://10.0.2.2:2861/AppCrashedInfo
data: {"packageName":"pb.myapp","shortPackageName":"pb.myapp","versionCode":1,"versionName":"1.0"}
01-21 22:53:31.545 6763-6763/? D/AndroidRuntime: procName from cmdline: pb.pocketboard
01-21 22:53:31.545 6763-6763/? E/AndroidRuntime: in writeCrashedAppName, pkgName :pb.pocketboard
01-21 22:53:31.545 6763-6763/? D/AndroidRuntime: file written successfully with content: pb.pocketboard StringBuffer : ;pb.pocketboard
01-21 22:53:31.555 6763-6763/? I/Process: Sending signal. PID: 6763 SIG: 9
01-21 22:53:31.555 6763-6763/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: pb.myapp, PID: 6763
java.lang.NullPointerException
at pb.pocketboard.TabFragment2$3.onQueryTextChange(TabFragment2.java:176)
at android.widget.SearchView.onTextChanged(SearchView.java:1153)
at android.widget.SearchView.access$2000(SearchView.java:92)
at android.widget.SearchView$11.onTextChanged(SearchView.java:1638)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.handleTextChanged(TextView.java:7467)
at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9183)
at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
at android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:223)
at android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)
at android.widget.TextView.doKeyDown(TextView.java:5532)
at android.widget.TextView.onKeyDown(TextView.java:5343)
at android.widget.AutoCompleteTextView.onKeyDown(AutoCompleteTextView.java:716)
at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
at android.view.View.dispatchKeyEvent(View.java:7696)
我的整个代码:-
public class TabFragment2 extends Fragment {
ArrayList <String> notesObject;
ArrayAdapter<ParseObject> notesAdapter;
ArrayAdapter<String> CarsAdapter;
ListView List_View;
SearchView Search_View;
SwipeRefreshLayout swipeToReload;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.tab_fragment_2, container, false);
List_View = (ListView) view.findViewById(R.id.notesList);
Search_View = (SearchView) view.findViewById(R.id.searchView);
swipeToReload = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Notes");
query.whereExists("Title");
query.fromLocalDatastore();
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list,
ParseException e) {
if (e == null) {
if (list.size() <= 0) {
Toast.makeText(getActivity(), "No results", Toast.LENGTH_LONG).show();
}
ArrayList<String> notesArray = new ArrayList<String>();
for (ParseObject j : list) {
notesArray.add(j.getString("Title"));
}
ArrayAdapter notesAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, notesArray);
List_View.setAdapter(notesAdapter);
} else {
e.getMessage();
Log.d("" + e.getMessage(), " errorr");
}
}
}
);
swipeToReload.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Log.i("", "onRefresh called from SwipeRefreshLayout");
ParseQuery<ParseObject> query = ParseQuery.getQuery("Notes");
query.whereExists("Title");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list,
ParseException e) {
if (e == null) {
try {
ParseObject.pinAll(list);
} catch (Exception pinE) {
Toast.makeText(getActivity(), "Pinning Failed", Toast.LENGTH_LONG).show();
}
ArrayList<String> notesArray = new ArrayList<String>();
for (ParseObject j : list) {
notesArray.add(j.getString("Title"));
}
ArrayAdapter notesAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, notesArray);
List_View.setAdapter(notesAdapter);
swipeToReload.setRefreshing(false);
notesAdapter.notifyDataSetChanged();
Toast.makeText(getActivity(), "Refresh Done", Toast.LENGTH_LONG).show();
} else {
e.getMessage();
Log.d("" + e.getMessage(), " errorr");
}
}
}
);
}
});
Search_View.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String Text) {
Log.d("tag","notesAdapter="+notesAdapter);
notesAdapter.getFilter().filter(Text);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.d("tag","notesAdapter="+notesAdapter);
notesAdapter.getFilter().filter(newText);
return true;
}
});
return view;
}
public void getNotesFromServer(){
ParseQuery<ParseObject> query = ParseQuery.getQuery("Notes");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list,
ParseException e) {
// TODO Auto-generated method stub
if (e == null)
{
ArrayList <String> notesArray = new ArrayList<String>();
for (ParseObject j : list){
notesArray.add(j.getString("Title"));
}
ArrayAdapter notesAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, notesArray );
List_View.setAdapter(notesAdapter);
} else {
e.getMessage();
Log.d(""+e.getMessage()," errorr");
}
}}
);}
}
确保您的适配器未null
。你可以简单地通过有一个日志来做到这一点
Log.d("tag","notesAdapter="+notesAdapter);
notesAdapter.getFilter().filter(newText);
然后检查 logcat 看看notesAdapter
是否给出notesAdapter=null
。如果是这样,那么问题出在您的适配器上。
其次,转到自定义列表适配器类中的getfilter
方法,并确保筛选器不会返回null
。它必须返回您的筛选器。