HI'm having an issue implementing the search widget in my application. It seems to not be able to find the "actionview" of the menu item, but it is finding the item just fine.
I've looked around for answers and haven't seen a clear cut solution.
Here's the menu I'm declaring in XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/group_search_box"
android:title="#string/search_label"
android:icon="#drawable/ic_action_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
And here is how it is implemented.
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.group_search_box);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
Here are the libraries I'm adding, maybe I'm adding an incorrect one?
compile 'com.android.support:support-v4:+'
compile 'com.android.support:appcompat-v7:+'
and finally here is the crash logs
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
at com.example.myapp.fragments.GroupFragment.onCreateOptionsMenu(GroupFragment.java:88)
at android.app.Fragment.performCreateOptionsMenu(Fragment.java:1780)
at android.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1927)
at android.app.Activity.onCreatePanelMenu(Activity.java:2539)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
I've followed the docs to a T and still can't figure out the issue.
I faced the same issue today and got rid of the confusion as follows,
I am also using the following dependencies,
compile 'com.android.support:support-v4:+'
compile 'com.android.support:appcompat-v7:+'
Which means I am extending the activity class from AppCompatActivity rather than Activity class.
So in this code,
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
this, SearchView should be, android.support.v7.widget.SearchView not android.widget.SearchView. This is the reason you got,
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
And also,
<item
android:id="#+id/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#android:drawable/ic_search_category_default"
android:title="Search"
app:showAsAction="always|collapseActionView" />
Notice that,
it is, app:actionViewClass NOT android:actionViewClass
I had been facing the same problem and none of the solutions provided in the answers worked for me. But this change in the code worked for me.
Use
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/group_search_box"
android:title="#string/search_label"
android:icon="#drawable/ic_action_action_search"
appcompat:showAsAction="ifRoom|collapseActionView"
appcompat:actionViewClass="android.support.v7.widget.SearchView" />
Notice the change, "app:actionViewClass" has been changed to " appcompat:actionViewClass".
Worked good for me, hope it works for you too.
Check the menu Folder in res.
in My case Two Files are created
First original and 2nd Auto Create with 21.
Delete the other file issue resolve if Duplicate happen
You should remove getActivity() from searchManager like below,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
MenuItem searchItem = menu.findItem(R.id.searchMenuItem);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
android.support.v7.widget.SearchView searchView =
(android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
Related
Im not sure why i am getting this error.
I am not casting from two different toolbars
Here is my code:
androidx.appcompat.widget.Toolbar toolbar = (androidx.appcompat.widget.Toolbar) findViewById(R.id.toolbar_header);
setSupportActionBar(toolbar)
What am i doing wrong here.
Here is the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mycontactlist, PID: 12667
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mycontactlist/com.example.mycontactlist.ContactListActivity}: java.lang.ClassCastException: android.widget.Toolbar cannot be cast to androidx.appcompat.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
Wrong Toolbar class defined in your xml file. Change it from
<Toolbar .../>
to
<androidx.appcompat.widget.Toolbar .../>
In your XML, you probably declared your toolbar using just <Toolbar></Toolbar> in that case, the toolbar will be created from the package android.widget. So if you try to call findViewById by casting it to androidx.appcompat.widget.Toolbar it will surely throw you a RuntimeException.
If you are using AndroidX, which you should, then you have to change the xml declaration of your toolbar to <androidx.appcompat.widget.Toolbar></androidx.appcompat.widget.Toolbar>
You can then proceed to call your (androidx.appcompat.widget.Toolbar) findViewByid(..) which should succeed
I have added a menu in a simple app of hello world! But my app keeps crashing.
I am not adding my xml code. My xml file name is menu inside the menu folder in res.
Here is my java file.
MyActivity.java
public boolean onCreateOptionsMenu(Menu menu1){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu1); //line 19
return true;
}
Logcat
06-03 18:03:57.682 13542-13542/com.example.kaushalraj.a424 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kaushalraj.a424, PID: 13542
android.view.InflateException: Couldn't resolve menu item onClick handler action in class com.example.kaushalraj.a424.MainActivity
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:253)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19)
at android.app.Activity.onCreatePanelMenu(Activity.java:3388)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
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.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NoSuchMethodException: action [interface android.view.MenuItem]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:249)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19)
at android.app.Activity.onCreatePanelMenu(Activity.java:3388)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
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.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I don't know if this your case (and i don't have the reputation to comment) but: What app theme are you using? If you are using a theme that does not have a actionbar at the top then you will get this error when you try to attach your menu to it, as the actionbar does not exist.
You have two solutions:
Check your theme in res/values/styles.xml if you are using something like Theme.AppCompat.Light.NoActionBar you must change it to something like Theme.AppCompat.Light.DarkActionBar. This will add the actionbar to your activity.
Using the support toolbar library, add the toolbar to your activity xml layout then from your activity class get the view in OnCreate() and use setSupportActionBar(myToolbar). This will set your toolbar view as the actionbar where your menu will be attached to.
whenever i hit the imageview i get this error, this happens to me when i switch to test my app on different screen sizes, here is my xml
<ImageView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:layout_marginTop="20dp"
app:srcCompat="#drawable/search"/>
and here is the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ahmed.electionadmin, PID: 30789
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed.electionadmin/com.example.ahmed.electionadmin.Search}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
I have tried so many solutions that asks me to replace src with srccompat and so on, but still is working, ant help would be appreciated
To use srcCompat you need to add vectorDrawables.useSupportLibrary = true to your build.gradle file:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Or you can use src instead of srcCompat to resolve the error.
android:src="#drawable/search"
Replace this:
app:srcCompat="#drawable/search"
With:
android:src="#drawable/search"
Pay attention, not only src with srcCompat, also app with android.
Let me know if it works.
Explanation: You're not using a support library ImageView, but a regular one. app namespace is usually used for support library, same as srcCompat. So you needed to change those 2 things, not just one. That's why merely changing the srcCompat with src yielded no results.
i fixed it by uploading my image inside draawable no 24
you are doing like fitting image to different resolutions , you should have image with different resolutions like hdpi xhdpi , you you can use vector drawable image
I am new to android and following the Udacity Android developer course to learn.
I am on chapter 3 where they teach about the adding the settings in your app. I have added the SettingActivity from android studio. When I click on settings menu, my app crashes.
This is my pref_general.xml file
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:key="#string/pref_location_key"
android:defaultValue="#string/pref_location_default"
android:maxLines="1"
android:inputType="text"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="#string/pref_location_label" />
I have made these changes in SettingActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));}
I am getting following exception when I click the Setting menu
Process: com.example.android.sunshine.app, PID: 10131
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sunshine.app/com.example.android.sunshine.app.SettingsActivity}: java.lang.RuntimeException: Modern two-pane PreferenceActivity requires use of a PreferenceFragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.RuntimeException: Modern two-pane PreferenceActivity requires use of a PreferenceFragment
at android.preference.PreferenceActivity.requirePreferenceManager(PreferenceActivity.java:1441)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1511)
at com.example.android.sunshine.app.SettingsActivity.onCreate(SettingsActivity.java:124)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Any help? Sorry if its very naive thing
I was able to solve this.
I was changing the OnCreate of my SettingActivity instead of changing my OnCreate of SettingFragment which is static private class of SettingActivity.
#mayank agrawal
He meant that the changes are to be done in class which extends PreferenceFragment. i.e you'll have to either extend PreferenceFragment.
Although the code is quite explanatory on the Developers site, here I've taken some part of just to mention what he meant (by putting comments in the code).
public class PreferenceWithHeaders extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
//static class extending PreferenceFragment
public static class Prefs1Fragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(getActivity(),
R.xml.advanced_preferences, false);
// Here's where you've to load the preferences from an XML resource
addPreferencesFromResource(R.xml.fragmented_preferences);
}
}
private void private void bindPreferenceSummaryToValue(Preference preference) {
...
}
}
If your app supports API 10 or low then it's mandatory that you use Preference Activity. However you can also use that deprecated way on new API as well. Here's a nice explanation in details for the beginners.
(I too am learning. So if anybody expert find any mistake then please let me know.)
I tried to use onCreateOptionsMenu in my application. I followed developers blog and it didn't work of me.
When i use this code:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.homepage_actionbar, menu);
return super.onCreateOptionsMenu(menu);
}
I got this compile errors:
Multiple markers at this line
- Syntax error on token ")", ; expected
- Illegal modifier for parameter onCreateOptionsMenu; only final is
permitted
- Syntax error on token "(", ; expected
Multiple markers at this line
- Void methods cannot return
a value
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/add_option"
android:title="Add Item"
android:icon="#drawable/ic_launcher"
android:showAsAction="ifRoom"
/>
</menu>
Thank for helping
I assume your onCreateOptionsMenu method to be implemented in another method of your Activity.
Just move it at the "root" level of your Activity class.
Check your code, that error is probably due to the fact a curly bracket is missing you either { or } or as #codeMagic said you have your code running in a method which is wrong you must have it directly in the class.