【问题标题】:Android searchmanager triggersearch giving null pointer exceptionAndroid searchmanager triggersearch给出空指针异常
【发布时间】:2013-07-17 06:54:59
【问题描述】:

以下是获取 searchmanagerinstance 并调用触发器搜索的代码: 使用 startsearch 调用而不是 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)

【问题讨论】:

  • 试试ComponentName(getApplicationContext().getPackageName(), MainActivity.class )
  • 试试 SearchManager s = (SearchManager) MainAcitivity.this.getSystemService(Context.SEARCH_SERVICE);
  • 没有与之匹配的构造函数,最接近的应该是这样的: new ComponentName(getApplicationContext(), MainActivity.class) Still got NPE

标签: android android-searchmanager android-search


【解决方案1】:

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

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

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

编辑:不管上面的,有一个bug logged in the issue track here

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-03
    • 2014-09-06
    • 2014-07-19
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多