我有以下问题:
当我从fragment1切换到fragment2时,我得到一个nullpointer异常 fragment2有一个带过滤器的searchview。某些移动电话出现错误,我不知道为什么。
应用程序在mListAdapter.getFilter()中崩溃。在那里过滤:
@Override
public boolean onQueryTextChange(String newText) {
mListAdapter.getFilter().filter(newText);
lv.setAdapter(mListAdapter);
return false;
}
这是Logcat:
08-25 09:18:25.840: E/AndroidRuntime(1430): FATAL EXCEPTION: main
08-25 09:18:25.840: E/AndroidRuntime(1430): java.lang.NullPointerException
08-25 09:18:25.840: E/AndroidRuntime(1430): at com.kansosystem.fragments.FragmentList.onQueryTextChange(FragmentList.java:291)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.widget.SearchView.onTextChanged(SearchView.java:1154)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.widget.SearchView.access$2000(SearchView.java:92)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.widget.SearchView$11.onTextChanged(SearchView.java:1623)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.widget.TextView.sendOnTextChanged(TextView.java:7079)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.widget.TextView.setText(TextView.java:3550)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.widget.TextView.setText(TextView.java:3405)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.widget.EditText.setText(EditText.java:80)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.widget.TextView.setText(TextView.java:3380)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.widget.TextView.onRestoreInstanceState(TextView.java:3280)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.view.View.dispatchRestoreInstanceState(View.java:11910)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2590)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2590)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2590)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2590)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.view.View.restoreHierarchyState(View.java:11888)
08-25 09:18:25.840: E/AndroidRuntime(1430): at com.android.internal.view.menu.MenuBuilder.restoreActionViewStates(MenuBuilder.java:358)
08-25 09:18:25.840: E/AndroidRuntime(1430): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:417)
08-25 09:18:25.840: E/AndroidRuntime(1430): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.app.Activity.invalidateOptionsMenu(Activity.java:2595)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.app.FragmentManagerImpl.invalidateOptionsMenu(FragmentManager.java:1950)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.app.Fragment.setHasOptionsMenu(Fragment.java:901)
08-25 09:18:25.840: E/AndroidRuntime(1430): at com.kansosystem.fragments.FragmentList.onCreate(FragmentList.java:53)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:796)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.app.BackStackRecord.run(BackStackRecord.java:635)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.os.Handler.handleCallback(Handler.java:615)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.os.Handler.dispatchMessage(Handler.java:92)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.os.Looper.loop(Looper.java:137)
08-25 09:18:25.840: E/AndroidRuntime(1430): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-25 09:18:25.840: E/AndroidRuntime(1430): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 09:18:25.840: E/AndroidRuntime(1430): at java.lang.reflect.Method.invoke(Method.java:511)
08-25 09:18:25.840: E/AndroidRuntime(1430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-25 09:18:25.840: E/AndroidRuntime(1430): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-25 09:18:25.840: E/AndroidRuntime(1430): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
首先,确保您的适配器不是null
。你可以通过像
Log.d("tag","mListAdapter="+mListAdapter);
mListAdapter.getFilter().filter(newText);
然后检查logcat以查看mListAdapter
是否给出了mListAdapter=null
。如果是这样,问题是你的适配器。
其次,转到自定义listadapter类中的getfilter
方法,并确保您的过滤器不会返回null
。 必须返回您的过滤器。