1

这是获取 searchmanagerinstance 并调用触发器搜索的代码: startsearch call 与 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);

这是执行此代码时出现的错误:

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)
4

1 回答 1

1

使用ComponentName时,您需要使用完整路径。我将在下面使用字符串进行演示:

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

您不能只在第二个参数中单独声明 Activity/Class 名称。至少不是以您的代码显示的方式。

编辑:无论上述情况如何,这里的问题跟踪中都记录了一个错误

编辑:即使使用反射来绕过空字段,我也无法让它工作

Field field = searchManager.getClass().getDeclaredField("mAssociatedPackage");
field.setAccessible(true);
field.set(searchManager, "some String value here");
Object value = field.get(searchManager);
// Check value here
于 2013-08-23T12:59:57.460 回答