我的搜索视图不起作用/给出一个空指针异常集可搜索信息



我已经花了最后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)

我对搜索做的事情;

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>

这是菜单文件夹中的菜单_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>

我不使用pro-guard,所以情况并非如此,我一直使用应用程序:showasaction和app:actionViewClass,但是正如我所说的那样,我在同一行上获得了NullPoInterException,请提前!

用此行替换搜索视图的初始化

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

替换:

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

with:

MenuItem searchItem =menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
<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>

相关内容

  • 没有找到相关文章

最新更新