1. Check out our companion app, Forums for Android! Download from Google Play

Help with SearchView

Discussion in 'Application Development' started by Vectron, Apr 19, 2017.

  1. Vectron

    Vectron New Member
    Thread Starter
    15

    Apr 19, 2017
    4
    0
    15
    I have been working on a project for my school. I am creating an app that will have the school map broken into four different sections and a home page. I am using a drawer menu navigation view for that and it works (attached file titled HHS).

    I have tried adding a materialsearchview to the code but when ever I run the code/app on the emulator the app just crashes. I believe that everything is put in correctly but something might be conflicting that I don't have the knowledge for experience to figure out/know. I have attached the not working code with the materialsearchview (attached file titled HHS1).

    Any help would be greatly appreciated.
    Thanks,Vectron
     

    Attached Files:


    Advertisement

  2. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,805
    4,949
    738
    Male
    Software developer
    UK
    Hi and welcome to Android Forums.
    In order to help you, we would ideally like to see the stack trace from your app when it crashed. This can be found in the Logcat view of Android Studio, and will look something like this

    https://developer.android.com/studio/debug/stacktraces.html

    Attaching all the source is not that helpful, but hopefully the stack trace will help to focus on the problem area.

    Did you write this code? If this is a school project, aimed at teaching you about software development, then part of that process is debugging your code. You would be doing yourself a great favour by learning how to run your app in debug mode. Some useful information is given here

    https://androidforums.com/threads/please-read-me-before-posting.987318/
     
  3. Vectron

    Vectron New Member
    Thread Starter
    15

    Apr 19, 2017
    4
    0
    15
    Hi!
    Thanks for the first bit of help. the stack trace is attached. For clarification, I am doing this on my own. This isn't a school project or anything, I am choosing to make an app that I think will help the school. Our school can be very confusing especially for people who have never been in it before. Therefore I am making this app with the map built in with room numbers so they can find room they want. I am also trying to implement the material search view so they can search for the room they want and it will show up on the screen, but it is only making the app crash.
    Thanks,
    Vectron
     

    Attached Files:

  4. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,805
    4,949
    738
    Male
    Software developer
    UK
    There's nothing in the stack trace file you attached. Can you resend it please?
     
  5. Vectron

    Vectron New Member
    Thread Starter
    15

    Apr 19, 2017
    4
    0
    15
    Sorry about there.
    Here it is
     

    Attached Files:

  6. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,805
    4,949
    738
    Male
    Software developer
    UK
    I couldn't open your zip files on my system, but looking at the stack trace, you have a NullPointerException, which is causing the app to crash

    Code (Text):
    1.  
    2. 04-20 06:13:04.143 2381-2381/? E/AndroidRuntime: FATAL EXCEPTION: main
    3.                                                  Process: com.example.cgeli.hhsfinal, PID: 2381
    4.                                                  java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setOnMenuItemClickListener(android.view.MenuItem$OnMenuItemClickListener)' on a null object reference
    5.                                                      at com.miguelcatalan.materialsearchview.MaterialSearchView.setMenuItem(MaterialSearchView.java:470)
    6.                                                      at com.example.cgeli.hhsfinal.MainActivity.onCreateOptionsMenu(MainActivity.java:144)
    7.                                                      at android.app.Activity.onCreatePanelMenu(Activity.java:3142)
    8.                                                      at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:362)
    9.                                                      at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
    10.  
    So the place you should be focusing on is line 144 in MainActivity. Your MenuItem appears to be null.
    If you would like to post the specific part of the code here in the thread, I can take a closer look, but as I say, your zip files are inaccessible for me.
     
    touringhaggis likes this.
  7. Vectron

    Vectron New Member
    Thread Starter
    15

    Apr 19, 2017
    4
    0
    15
    Here is the code. Line 144 is bolded
    Code (Text):
    1.  
    2.  [USER=1021285]@override[/USER]
    3.     protected void onPostCreate(Bundle savedInstanceState) {
    4.         super.onPostCreate(savedInstanceState);
    5.         actionBarDrawerToggle.syncState();
    6.     }
    7.  
    8.     public boolean onCreateOptionsMenu(Menu menu) {
    9.         MenuInflater inflater = getMenuInflater();
    10.         inflater.inflate(R.menu.drawer_menu, menu);
    11.         MenuItem item = menu.findItem(R.id.action_search);
    12.        searchView.setMenuItem(item); (line 144)
    13.  
    14.         return true;
    15.     }
    16.  
    17.     [USER=1021285]@override[/USER]
    18.     public void onBackPressed() {
    19.         if (searchView.isSearchOpen()) {
    20.             searchView.closeSearch();
    21.         } else {
    22.             super.onBackPressed();
    23.         }
    24.     }
    25. }
    26.  
     
    #7 Vectron, Apr 23, 2017
    Last edited by a moderator: Apr 24, 2017
  8. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,805
    4,949
    738
    Male
    Software developer
    UK
    So at line 144 you know that variable searchView is null.
    Question is, where do you initialise this variable?
     

Share This Page

Loading...