Arjun Ramesh
Arjun Ramesh

Reputation: 189

Android searchmanager triggersearch giving null pointer exception

Here is the code getting searchmanagerinstance and calling trigger search: The same works fine with startsearch call not with triggersearch

String searchQuery = "dollar in rupees\n";
SearchManager s = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
// s.startSearch(searchQuery, false , null, null, true);
s.triggerSearch(searchQuery, new ComponentName(getApplicationContext().getPackageName(), "MainAcitivity" ), null);

This is the error i get when this code is executed:

07-17 12:17:04.650: E/AndroidRuntime(1265): java.lang.IllegalStateException: Could not execute method of the activity
07-17 12:17:04.650: E/AndroidRuntime(1265):     at android.view.View$1.onClick(View.java:3591)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at android.view.View.performClick(View.java:4084)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at android.view.View$PerformClick.run(View.java:16966)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at android.os.Handler.handleCallback(Handler.java:615)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at android.os.Looper.loop(Looper.java:137)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at java.lang.reflect.Method.invokeNative(Native Method)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at java.lang.reflect.Method.invoke(Method.java:511)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at dalvik.system.NativeStart.main(Native Method)
07-17 12:17:04.650: E/AndroidRuntime(1265): Caused by: java.lang.reflect.InvocationTargetException
07-17 12:17:04.650: E/AndroidRuntime(1265):     at java.lang.reflect.Method.invokeNative(Native Method)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at java.lang.reflect.Method.invoke(Method.java:511)
07-17 12:17:04.650: E/AndroidRuntime(1265):     at android.view.View$1.onClick(View.java:3586)
07-17 12:17:04.650: E/AndroidRuntime(1265):     ... 11 more
07-17 12:17:04.650: E/AndroidRuntime(1265): Caused by: java.lang.NullPointerException
07-17 12:17:04.650: E/AndroidRuntime(1265):     at android.app.SearchManager.triggerSearch(SearchManager.java:635)

Upvotes: 1

Views: 387

Answers (1)

brandall
brandall

Reputation: 6144

When using ComponentName you need to use the full path. I'll demonstrate with Strings below:

new ComponentName("com.example.package", "com.example.package.MainActivity");

You cannot just declare the Activity/Class name alone in the second parameter. At least not in the way in which your code shows.

EDIT: Regardless of the above, there is a bug logged in the issue track here

EDIT: Even using reflection to get around the null field, I can't get this to work

Field field = searchManager.getClass().getDeclaredField("mAssociatedPackage");
field.setAccessible(true);
field.set(searchManager, "some String value here");
Object value = field.get(searchManager);
// Check value here

Upvotes: 1

Related Questions