简体   繁体   English

使用SearchView的nullpointerexception

[英]nullpointerexception with SearchView

The older APIs 15 and 16 don't have this error, but for some reason the new API 19 throws a NullPointerException at SearchView.setOnQueryTextListener(this) . 较早的API 15和16没有此错误,但是由于某些原因,新的API 19在SearchView.setOnQueryTextListener(this)引发了NullPointerException
I can fix it by moving the listener to onCreateOptionsMenu , but then it doesn't work on the older devices and vice-versa when I move it back to onCreateView . 我可以通过将侦听器移到onCreateOptionsMenu来修复它,但是当我将其移回到onCreateView时,它就无法在较旧的设备上运行,反之亦然。

public class EventFragment extends Fragment implements AdapterView.OnItemClickListener, SearchView.OnQueryTextListener, View.OnClickListener
{
    private List<EventItem> items;
    private ProgressBar progressBar;
    private View view;
    private JazzyListView listView;
    static EventAdapter adapter;
    SearchView searchView;

    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        getActivity().invalidateOptionsMenu();

        // creates call to
        // onPrepareOptionsMenu()
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

           MenuItem searchItem=  menu.findItem(R.id.search);
                searchView =
                    (SearchView) MenuItemCompat.getActionView(searchItem);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (view == null) {
            //inflate the layout for the items in view
            view = inflater.inflate(R.layout.fragment_layout, container, false);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
            listView = (JazzyListView) view.findViewById(R.id.listView);

            Button button = (Button) view.findViewById(R.id.button3);
            //Customize listView/searchview
            listView.setTextFilterEnabled(true);
            listView.setOnItemClickListener(this);
            listView.setSmoothScrollbarEnabled(true);
            searchView.setOnQueryTextListener(this);
            button.setOnClickListener(this);
            startService();
        } else {
            // If we are returning from a configuration change:
            //  remove view and re-attach it to the current configuration
            ViewGroup parent = (ViewGroup) view.getParent();
            parent.removeView(view);
        }

        return view;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }

Here is menu: 这是菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
    android:title="@string/hello"
    android:icon="@drawable/search"
    android:showAsAction="collapseActionView|always"
    android:actionViewClass="android.widget.SearchView"
  />

</menu>

Here is the logcat: 这是logcat:

 05-13 16:33:34.146  10646-10646/com.parse.starter E/CrashReporting﹕ ParseCrashReporting caught a RuntimeException exception for com.parse.starter. Building report.
05-13 16:33:34.169  10646-10646/com.parse.starter E/CrashReporting﹕ Handling exception for crash
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parse.starter/com.parse.starter.EventKGHS}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2060)
        at android.app.ActivityThread.access$600(ActivityThread.java:127)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1181)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4558)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.parse.starter.EventFragment.onQueryTextChange(EventFragment.java:160)
        at android.widget.SearchView.onTextChanged(SearchView.java:1091)
        at android.widget.SearchView.access$2000(SearchView.java:90)
        at android.widget.SearchView$11.onTextChanged(SearchView.java:1550)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7692)
        at android.widget.TextView.setText(TextView.java:3365)
        at android.widget.TextView.setText(TextView.java:3218)
        at android.widget.EditText.setText(EditText.java:78)
        at android.widget.TextView.setText(TextView.java:3193)
        at android.widget.TextView.onRestoreInstanceState(TextView.java:3093)
        at android.view.View.dispatchRestoreInstanceState(View.java:9968)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2324)
        at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2324)
        at android.view.View.restoreHierarchyState(View.java:9946)
        at com.android.internal.view.menu.MenuBuilder.restoreActionViewStates(MenuBuilder.java:358)
        at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:429)
        at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:890)
        at android.app.Activity.invalidateOptionsMenu(Activity.java:2671)
        at com.parse.starter.EventFragment.onCreate(EventFragment.java:57)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
        at android.app.Activity.performStart(Activity.java:4646)
.......

Judging by the lack of input and results. 从缺乏投入和结果来判断。 I must have discovered a bug! 我一定发现了一个错误! I shall soon report it. 我将尽快报告。

Update: what if instead of setting the onquerylistener to "this" I used something else? 更新:如果我没有使用onquerylistener设置为“ this”,该怎么办?

**尝试在实例化EventFragment的活动中监听searchView **

Ok I have corrected the issue. 好的,我已经解决了这个问题。 I did three things and I'm not sure which one fixed it or if all of it did, but that is besides the point. 我做了三件事,我不确定是哪一件事修复了它,或者所有的事情都解决了,但这不是重点。 First of all, I created a private OnQueryTextListener class for the SearchView listener to be set to. 首先,我为要设置的SearchView侦听器创建了一个私有的OnQueryTextListener类。 Then I moved the SearchView OnQueryTextListener to onCreateOptionsMenu. 然后,将SearchView OnQueryTextListener移到onCreateOptionsMenu。 Lastly, and this is the one presume fixed it if I had to pick one, I removed the getActivity().invalidateOptionsMenu(); 最后,如果要选择一个,这是一个固定的假设,我删除了getActivity()。invalidateOptionsMenu();。 line. 线。 I may still submit a bug report as this really shouldn't have happened in the first place. 我可能仍会提交错误报告,因为这本来不应该发生的。 Leave your explanation of why this may have happened in a comment below or edit my answer. 在下面的评论中留下您为什么会这样的解释,或编辑我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM