2013-07-17 43 views
1

這裏是代碼越來越searchmanagerinstance並調用觸發搜索:的Android SearchManager的triggersearch給空指針異常

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) 
+0

嘗試'組件名稱(getApplicationContext ().getPackageName(),MainActivity.class)' – Simon

+0

使用SearchManager試一試s =(SearchManager)MainAcitivity.this.getSystemService(Context.SEARCH_SERVICE); – Nas

+0

有沒有構造函數匹配,最接近會是這樣的: 新的ComponentName(getApplicationContext(),MainActivity.class) 仍然有NPE –

回答

1

使用ComponentName時,您需要使用完整路徑。我將用下面的Strings進行演示:

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