OnQueryTextListener中的Nullpointerexception

时间:2014-06-11 21:07:56

标签: java android searchview

我想在ActionBar上安装SearchView。但是,如果我添加一个OnQueryTextListener,则会出现NullPointerException。此外,我无法在栏中显示searchView。

MainActivity.java

public class MainActivity extends ActionBarActivity implements OnQueryTextListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 

        if (savedInstanceState == null) { 
            getSupportFragmentManager().beginTransaction() 
                    .add(R.id.container, new PlaceholderFragment()).commit(); 
        } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

        // Inflate the menu; this adds items to the action bar if it is present. 

            MenuInflater inflater = getMenuInflater(); 
            inflater.inflate(R.menu.main, menu); 
            MenuItem searchItem = menu.findItem(R.id.action_search); 
            SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchItem); 
            searchView.setOnQueryTextListener(this); 
            return super.onCreateOptionsMenu(menu); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        // Handle action bar item clicks here. The action bar will 
        // automatically handle clicks on the Home/Up button, so long 
        // as you specify a parent activity in AndroidManifest.xml. 
        int id = item.getItemId(); 
        if (id == R.id.action_settings) { 
            return true; 
        } 
        return super.onOptionsItemSelected(item); 
    } 

    /** 
     * A placeholder fragment containing a simple view. 
     */ 
    public static class PlaceholderFragment extends Fragment { 

        public PlaceholderFragment() { 
        } 

        @Override 
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                Bundle savedInstanceState) { 
            View rootView = inflater.inflate(R.layout.fragment_main, container, 
                    false); 
            return rootView; 
        } 
    } 

    @Override 
    public boolean onQueryTextChange(String arg0) { 
        // TODO Auto-generated method stub 
        return false; 
    } 

    @Override 
    public boolean onQueryTextSubmit(String arg0) { 
        // TODO Auto-generated method stub 
        return false; 
    } 

} 

menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="de.group42.todo.MainActivity" > 
<item 
          android:id="@+id/action_search" 
          android:title="Search" 
          android:icon="@drawable/abc_ic_search" 
          android:showAsAction="always" 
          android:actionViewClass="android.support.v7.widget.SearchView" 
          /> 
    <item 
        android:id="@+id/action_settings" 
        android:orderInCategory="100" 
        android:title="@string/action_settings" 
        app:showAsAction="never"/> 

</menu> 

我不知道我做错了什么。愿有人帮帮我吗?

编辑:LogCat说

06-11 23:13:10.755: E/AndroidRuntime(17723): FATAL EXCEPTION: main
06-11 23:13:10.755: E/AndroidRuntime(17723): java.lang.NullPointerException
06-11 23:13:10.755: E/AndroidRuntime(17723):    at de.group42.todo.MainActivity.onCreateOptionsMenu(MainActivity.java:42)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.app.Activity.onCreatePanelMenu(Activity.java:2578)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:507)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:934)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:292)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.view.Choreographer.doFrame(Choreographer.java:560)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.os.Handler.handleCallback(Handler.java:730)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.os.Looper.loop(Looper.java:176)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at android.app.ActivityThread.main(ActivityThread.java:5419)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at java.lang.reflect.Method.invokeNative(Native Method)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at java.lang.reflect.Method.invoke(Method.java:525)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
06-11 23:13:10.755: E/AndroidRuntime(17723):    at dalvik.system.NativeStart.main(Native Method)

第42行正在添加侦听器。希望你能帮帮我。

谢谢:)

2 个答案:

答案 0 :(得分:0)

您的searchView似乎被分配了null

我发现这个问题对您有帮助:

MenuItemCompat.getActionView always returns null

答案 1 :(得分:0)

尝试将命名空间从android:actionViewClass更改为app:actionViewClass