searchView.setSearchableInfo(searchInfo) dă NPE de fiecare dată și nu pot găsi cauza (Programare, Android, Android Actionbar, Searchview)

Parth Vora a intrebat.
 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    MenuItem searchItem = menu.findItem(R.id.menu_search);
    android.widget.SearchView searchView = (android.widget.SearchView) searchItem.getActionView();

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchableInfo searchInfo = searchManager.getSearchableInfo(getComponentName());
    if(searchInfo != null) {
        searchView.setSearchableInfo(searchInfo);
    }
    //searchView.setIconifiedByDefault(false);

    return true;
}

Acesta este codul de creare a meniului în care am pus searchView.setSearchableInfo(searchInfo) dar îmi dă o eroare NPE.

Urmărire completă a stivei:

11-25 13:53:15.223    1693-1693/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at vt.parthvora.task5maps.MapsActivity.onCreateOptionsMenu(MapsActivity.java:221)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2505)
            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:262)
            at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
            at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:241)
            at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1247)
            at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1527)
            at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:90)
            at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:128)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

Comentarii

  • Vă rugăm să postați jurnalul de eroare –  > Por Mustanser Iqbal.
  • Verificați acum, vă rog… –  > Por Parth Vora.
  • searchView.setSearchableInfo(searchInfo); –  > Por Parth Vora.
  • verificați răspunsul meu, vă rog, sper că vă va rezolva problema –  > Por Mustanser Iqbal.
  • @ParthVora… A se vedea link-ul –  > Por Kirit Modi.
1 răspunsuri
Mustanser Iqbal

încercați acest cod pentru a configura vizualizarea de căutare. am copiat acest cod lipit de la unul dintre proiectele mele. puteți înlocui AutoCompleteTextView cu EditText sau orice altceva doriți

menu_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
tools_context="audioplayer.ui.MainActivity">
<item
    android_id="@+id/action_search"
    android_orderInCategory="100"
    android_title="@string/action_search"
    android_icon="@drawable/search"
    app_showAsAction="always|collapseActionView" />
</menu>

puneți acest cod în activitatea dvs. principală

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        MenuItem menuItem = menu.getItem(0).setActionView(R.layout.search_view);
        search = (AutoCompleteTextView) menuItem.getActionView().findViewById(
                R.id.search_txt);
        search.setFocusableInTouchMode(true);
        search.requestFocus();
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1,
                songNames);
        search.setAdapter(adapter);

        search.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                in.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
                // do what ever you want

            }
        });
        search.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView view, int actionId,
                                          KeyEvent event) {
                // TODO Auto-generated method stub
                if (actionId == EditorInfo.IME_ACTION_GO) {

                    if (search.getText().toString().length() > 0) {
                        startActivity(new Intent(MainActivity.this,
                                SearchActivity.class).putExtra("keyword",
                                search.getText().toString()));
                        overridePendingTransition(R.anim.slide_in_left,
                                R.anim.slide_out_left);
                        return true;
                    }

                }
                return false;
            }
        });

        ImageView imgClear = (ImageView) menuItem.getActionView().findViewById(
                R.id.clear_search);
        imgClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search.setText("");
                InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                im.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                        InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                im.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                        InputMethodManager.HIDE_IMPLICIT_ONLY);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

aici este aspectul meu search_view

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns_android="http://schemas.android.com/apk/res/android"
    android_layout_width="match_parent"
    android_layout_height="wrap_content">

    <RelativeLayout
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_background="@color/primary">

        <AutoCompleteTextView
            android_id="@+id/search_txt"
            android_layout_width="match_parent"
            android_layout_height="wrap_content"
            android_layout_centerVertical="true"
            android_layout_margin="@dimen/five_dp"
            android_drawableLeft="@drawable/abc_ic_search_api_mtrl_alpha"
            android_ems="10"
            android_hint="@string/action_search"
            android_imeOptions="actionGo"
            android_inputType="textAutoComplete"
            android_singleLine="true"
            android_visibility="visible" />


        <ImageButton
            android_id="@+id/clear_search"
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_layout_alignParentEnd="true"
            android_layout_alignParentRight="true"
            android_layout_centerVertical="true"
            android_focusable="true"
            android_background="@drawable/button_selector"
            android_layout_margin="@dimen/five_dp"
            android_padding="5dp"
            android_src="@drawable/abc_ic_clear_mtrl_alpha"
            android_visibility="visible" />

    </RelativeLayout>
</RelativeLayout>

Comentarii

  • am primit NPE la mSearchView.setSearchableInfo(info); – –  > Por Parth Vora.
  • Codul dvs. nu funcționează, de asemenea, vă rugăm să mă ghidați cum pot crea o vizualizare de căutare în studioul Android numai în activitatea principală? –  > Por Parth Vora.
  • ok așteptați m liitle ocupat voi actualiza răspunsul aproximativ după o oră. –  > Por Mustanser Iqbal.
  • verificați răspunsul meu actualizat… și anunțați-mă dacă funcționează sau nu pentru dvs.  > Por Mustanser Iqbal.
  • Ai verificat răspunsul meu? @ParthVora –  > Por Mustanser Iqbal.