我的Searchview不起作用/给出了NullPointerException setSearchableInfo

时间:2019-03-12 08:36:36

标签: java android nullpointerexception searchview

我花了最后8个小时来解决这个简单的哑巴NPE(即将失明)。几乎阅读了有关此问题的所有内容,尝试了所有可能的结果,但结果相同,因此我将发布与之相关的所有代码,希望有人能帮忙!

首先,这是我通过logcat遇到的全部错误;

2019-03-12 11:16:56.130 23426-23426/com.demotxt.myapp.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.demotxt.myapp.myapplication, PID: 23426
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
        at com.demotxt.myapp.myapplication.activities.Main3Activity.onCreateOptionsMenu(Main3Activity.java:130)
        at android.app.Activity.onCreatePanelMenu(Activity.java:3317)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340)
        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
        at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
        at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:55)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

我的Main3Activity,我负责搜索;

public class Main3Activity extends AppCompatActivity {

    private final String JSON_URL = "https://MYURLXX" ;
    private JsonArrayRequest request ;
    private RequestQueue requestQueue ;
    private List<Anime> lstAnime ;
    private RecyclerView recyclerView ;
    RecyclerViewLiveAdapter adapter;
    RecyclerView.LayoutManager layoutManager;

    Toolbar toolbar;
    SearchView searchView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        lstAnime = new ArrayList<>() ;
        recyclerView = findViewById(R.id.recyclerviewid);
        jsonrequest();



    }

    private void jsonrequest() {

        request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                JSONObject jsonObject  = null ;

                for (int i = 0 ; i < response.length(); i++ ) {


                    try {
                        jsonObject = response.getJSONObject(i) ;
                        Anime anime = new Anime() ;
                        anime.setName(jsonObject.getString("name"));
                        anime.setDescription(jsonObject.getString("description"));
                        anime.setRating(jsonObject.getString("Rating"));
                        anime.setCategorie(jsonObject.getString("categorie"));
                        anime.setStudio(jsonObject.getString("studio"));
                        anime.setImage_url(jsonObject.getString("img"));
                        anime.setLink(jsonObject.getString("link"));
                        anime.setDrm_scheme(jsonObject.getString("drm_scheme"));
                        anime.setDrm_license_url(jsonObject.getString("drm_license_url"));
                        anime.setDrm(jsonObject.getString("drm"));
                        anime.setSubtitle(jsonObject.getString("subtitle"));
                        anime.setSubtitle1(jsonObject.getString("subtitle1"));
                        anime.setSubtitle2(jsonObject.getString("subtitle2"));
                        lstAnime.add(anime);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }

                setuprecyclerview(lstAnime);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


        requestQueue = Volley.newRequestQueue(Main3Activity.this);
        requestQueue.add(request) ;


    }

    private void setuprecyclerview(List<Anime> lstAnime) {


        RecyclerViewLiveAdapter myadapter = new RecyclerViewLiveAdapter(this,lstAnime) ;
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myadapter);

    }

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

        MenuItem searchItem =menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) searchItem.getActionView();
        SearchManager searchManager=(SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                newText=newText.toLowerCase();
                List<Anime> myList=new ArrayList<>();
                for (Anime anime:lstAnime){
                    String moviename =anime.getName().toLowerCase();
                    if (moviename.contains(newText))
                        myList.add(anime);
                }
                adapter.setSearchOperation(myList);
                return false;
            }
        });
        return true;

    }
}

我的RecyclerViewAdapter中的相关部分;

public void setSearchOperation(List<Anime> newList) {
    mData=new ArrayList<>();
    mData.addAll(newList);
    notifyDataSetChanged();
}

我在res / xml中的searchable.xml;

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search" >
</searchable>

AndroidManifest.xml中Main3Activity的相关部分;

  <activity android:name=".activities.Main3Activity">
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"
            android:value=".activities.Main3Activity"/>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH"/>
            </intent-filter>
        </activity>

这是工具栏的布局,由RecyclerView的布局调用,例如“ include layout =“ @ layout / toolbar_layout”>

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"></android.support.v7.widget.Toolbar>

这是菜单文件夹中的menu_item xml文件;

<?xml version="1.0" encoding="utf-8"?>
<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">


    <item

        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="Search Test"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView"/>
</menu>

我不使用亲卫队,所以不是这样,我总是使用app:showAsAction和app:actionViewClass,但是正如我所说的,我在同一行上都得到了nullpointerexception,谢谢!!

2 个答案:

答案 0 :(得分:1)

用此行替换SearchView的初始化

SearchView searchView = new SearchView(this.getSupportActionBar().getThemedContext());

答案 1 :(得分:0)

替换:

SearchView searchView = (SearchView) searchItem.getActionView();

使用方式:

MenuItem searchItem =menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

还要确保您的 menu.xml 文件应如下所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
      android:title="@string/action_search"
      android:icon="@drawable/action_search"
      yourapp:showAsAction="always|collapseActionView"
      yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>