Reputation: 15734
I am using ActionBarSherlock
and Support Library v4. This is in a `SherlockFragmentActivity'.
I am getting a Nullpointer on line 269, referenced below. I have verified SearchView is null, not SearchManager. Though, menu
is also null as well, I am not sure if that is normal? Important: It is ONLY null in my Android 4.0 and 4.1 tests. In 2.3, it works great!
Here is my SearchView
Code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView(); // line 269
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(true);
return true;
}
I have this as my menu item now.
<item
android:id="@+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="always"
android:title="search"/>
LogCat
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.---.---.master.MainFragmentActivity
.setupSearchView(MainFragmentActivity.java:269)
at com.---.---.master.MainFragmentActivity.onCreateOptionsMenu(
MainFragmentActivity.java:258)
at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:45)
at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(
ActionBarSherlock.java:559)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(
ActionBarSherlockCompat.java:479)
at com.actionbarsherlock.internal.ActionBarSherlockCompat
.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:272)
at com.actionbarsherlock.internal.ActionBarSherlockCompat$1.run(
ActionBarSherlockCompat.java:984)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(
ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
imports:
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.SearchView;
import android.app.SearchManager;
Again, I get Null Pointer in ONLY the 4.0 emulator and 4.1 device. App runs great in 2.3. At one time, pre-ActionBarSherlock, using the SAME code, except importing SearchView
through the normal way (not Sherlock) it worked great in 4.0. I am trying to make my app backwards compatible and is broke the newer Android versions, but works great in the older.
EDIT: It was actually Null in 2.3 and Cast Issue in 4.0+ when the menu item had android:actionViewClass="android.app.SearchView"
Then I changed it to android:actionViewClass="com.actionbarsherlock.widget.SearchView"
and now FIXED in 2.3 and Null in 4.0+
UPDATE: I appeared to fix it with a hack:
I added a menu-v11
folder added a duplicate file of menu.xml with one item change only: android:actionViewClass="android.app.SearchView"
, otherwise it is identical to the other menu.xml file.
In Java, I did this:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(true);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setupNewSearchView(searchItem, searchManager);
}
return true;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupNewSearchView(MenuItem searchItem,
SearchManager searchManager) {
android.widget.SearchView searchView =
(android.widget.SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(true);
}
Well, it works, but I'm not putting it as an answer yet because it seems like hack way of doing it. Open to better ideas?
Upvotes: 4
Views: 4142
Reputation: 1
Add a theme attribute to your manifest file under the tags of each of the activities that you plan to use an ActionBar
. You can create your own custom theme or use one of the default offered by ActionBarSherlock
.
<activity
android:name="com.example.MyActivityWithActionBar"
...
android:theme="@style/Theme.Sherlock.Light">
</activity>
More information on:
Upvotes: 0
Reputation: 6799
There is a problem with missing attrs for v14 (and v11?) in ActionBarSherlock (or ActionBarSherlock + HoloEverywhere + Theme.Sherlock.Light.DarkActionBar ?).
Simple solution and details in the issue thread…
Upvotes: 2
Reputation: 17292
You are trying to use two completely different classes, both named SearchView but in different packages. You cannot simply cast an object from one class into another class. com.actionbarsherlock.widget.Searchview is different than android.widget.SearchView. Therefore, searchView
is null on line 269.
It would be relevant for us to see the imports from the top of your Java file.
Probably, the view referenced by R.id.menu_search is an android.widget.SearchView and your code expects it to be a com.actionbarsherlock.widget.Searchview.
Upvotes: 0