更改方向时如何保存searchView的状态

时间:2018-02-04 10:40:12

标签: android searchview

我在一项活动中有一个searchView。搜索过滤器工作得很好,除了有一个我不知道如何解决的问题。你看,当我从纵向到横向改变方向时,它会导致错误,我不知道我做错了什么。那么有谁能告诉我我做错了什么?

这是代码

 ArrayList<petbook_getItem> bookItem=new ArrayList<>();
RecyclerView petbook;
petbook_adapter adapter;
LinearLayoutManager linearLayoutManager;
TextView pet_title;
private static Parcelable pet_recycler;
SearchView searchView;
private static final String SEARCH_KEY = "search";
String mSearchString;
MenuItem searchItem;

if(savedInstanceState != null){
           mSearchString=savedInstanceState.getString(SEARCH_KEY);
        }

        pet_title=(TextView)findViewById(R.id.pet_book_title);
        petbook=(RecyclerView)findViewById(R.id.pet_book_pet);
        navbot=(BottomNavigationView) findViewById(R.id.petbook_navbot);
        toolbar=(Toolbar)findViewById(R.id.petbook_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setTitle("");
        linearLayoutManager=new LinearLayoutManager(this);
        petbook.setHasFixedSize(true);
        petbook.setLayoutManager(linearLayoutManager);
        refresh();
        disableShiftMode(navbot);
petbook.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (dy > 0 && navbot.isShown()) {
                    navbot.setVisibility(View.GONE);
                } else if (dy < 0 ) {
                    navbot.setVisibility(View.VISIBLE);

                }
            }
        });
    }

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       if(item.getItemId()==android.R.id.home){
           {
               finish();
           }

       }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater menuInflater= getMenuInflater();
        menuInflater.inflate(R.menu.petbook_searck,menu);
        searchItem=menu.findItem(R.id.pet_book_search_mode);
        searchView =(SearchView)MenuItemCompat.getActionView(searchItem);
        if (mSearchString != null && !mSearchString.isEmpty()) {
            searchItem.expandActionView();
            searchView.setQuery(mSearchString, true);
            searchView.clearFocus();
        }
        search(searchView);
        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                pet_title.setVisibility(View.INVISIBLE);
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                pet_title.setVisibility(View.VISIBLE);
                return true;
            }
        });
        return true;
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mSearchString = searchView.getQuery().toString();
        outState.putString(SEARCH_KEY, mSearchString);
    }
   @Override
    public void onPause() {
        super.onPause();
        pet_recycler = petbook.getLayoutManager().onSaveInstanceState();

    }
    @Override
    public void onResume()
    {  //
        super.onResume();
        if (mSearchString != null && !mSearchString.isEmpty()) {
            searchItem.expandActionView();
            searchView.setQuery(mSearchString, true);
            searchView.clearFocus();
            adapter.filterSearch(newList);
            adapter.notifyDataSetChanged();
        }
        else
        {
            bookItem.clear();
            refresh();
        }
        petbook.getLayoutManager().onRestoreInstanceState(pet_recycler);
    }

    public void search(android.support.v7.widget.SearchView search)
    {
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                newText=newText.toLowerCase();
                newList=new ArrayList<>();
                for(petbook_getItem items : bookItem){
                    String pets=items.getPet_name().toLowerCase();
                    if(pets.contains(newText))
                        newList.add(items);
                }
                adapter.filterSearch(newList);
                adapter.notifyDataSetChanged();
                return true;
            }
        });
    }

这是发生的错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: petguide.com.petguide101, PID: 25230
              java.lang.RuntimeException: Unable to resume activity {petguide.com.petguide101/petguide.com.petguide101.petbook.pet_book}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setQuery(java.lang.CharSequence, boolean)' on a null object reference
                  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3226)
                  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3257)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2600)
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4210)
                  at android.app.ActivityThread.access$900(ActivityThread.java:178)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1476)
                  at android.os.Handler.dispatchMessage(Handler.java:111)
                  at android.os.Looper.loop(Looper.java:194)
                  at android.app.ActivityThread.main(ActivityThread.java:5631)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setQuery(java.lang.CharSequence, boolean)' on a null object reference
                  at petguide.com.petguide101.petbook.pet_book.onResume(pet_book.java:197)
                  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1267)
                  at android.app.Activity.performResume(Activity.java:6178)
                  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3211)
                  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3257) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2600) 
                  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4210) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:178) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1476) 
                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                  at android.os.Looper.loop(Looper.java:194) 
                  at android.app.ActivityThread.main(ActivityThread.java:5631) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 

1 个答案:

答案 0 :(得分:1)

在这里!代码中的所有内容对我来说都很好 在项目清单中的活动定义中使用此属性标记。

android:configChanges="keyboardHidden|orientation|screenSize"

这将停止屏幕旋转上的自动活动重启问题。还有更多方法可以做到这一点 check out THIS for more