操作栏搜索视图中的错误

时间:2015-10-24 12:06:04

标签: java android

我跟随this tutorial,但我在此段落中收到错误:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    MenuItem menuItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) menuItem.getActionView();

    searchView.setQueryHint("Type something..."); //at this line (line 28 in the project)

    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    View searchPlate = searchView.findViewById(searchPlateId);
    if (searchPlate!=null) {
        searchPlate.setBackgroundColor(Color.DKGRAY);
        int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
        if (searchText!=null) {
            searchText.setTextColor(Color.WHITE);
            searchText.setHintTextColor(Color.WHITE);
        }
    }
    return true;
}

我认为我的问题是SearchView ......

我不知道您需要回答哪些信息...如果您需要,请向我详细说明。

Logcat:

10-24 11:59:43.533 9216-9216/? E/AndroidRuntime: FATAL EXCEPTION: main
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime: java.lang.NullPointerException
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at com.drarig29.searchactionbar.MainActivity.onCreateOptionsMenu(MainActivity.java:28)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:262)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:241)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1247)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1527)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:90)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:128)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:615)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 11:59:43.533 9216-9216/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案