0
我有一個奇怪的問題。我試圖在我的應用程序中實現搜索。如果我按搜索按鈕,或者如果我從onStart()函數調用onSearchRequested(),則會顯示搜索屏幕。但是如果我從OnItemClickListener的onItemClick函數調用onSearchReqeusted(),我會得到一個NullPointerException。我也嘗試獲取搜索管理器的實例,並直接調用startSearch函數,但它也失敗。從OnItemClickListener調用搜索對話框
我認爲這個人How do I invoke the search dialog using onSearchRequested()有同樣的問題,它沒有回答,但我不知道。
這裏是我的OnStart方法:
@Override
protected void onStart() {
super.onStart();
this.setContentView(R.layout.mainactivity);
ListView lv = (ListView)this.findViewById(R.id.MainActivityList);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, actions));
onSearchRequested(); //This works...
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = null;
Context c = MainActivity.this;
//position is the index of actions above.
switch(position)
{
case 0:
i = new Intent(c,MainCategoryActivity.class);
break;
case 1:
//SearchManager sm = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
//sm.startSearch(null, false, new ComponentName(MainActivity.this, MainActivity.class) , null, false);
onSearchRequested(); //NullPointerException
break;
case 2:
i = new Intent(c,SelectCityActivity.class);
break;
case 3:
i = new Intent(c,SavedSearchesActivity.class);
break;
}
c.startActivity(i);
}});
}
這裏是例外日誌:
06-28 16:09:22.368: E/AndroidRuntime(1702): FATAL EXCEPTION: main
06-28 16:09:22.368: E/AndroidRuntime(1702): java.lang.NullPointerException
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.app.Activity.startActivityForResult(Activity.java:3190)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.app.Activity.startActivity(Activity.java:3297)
06-28 16:09:22.368: E/AndroidRuntime(1702): at com.TheGreensheet.GreensheetReader.MainActivity$1.onItemClick(MainActivity.java:47)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.widget.AbsListView$1.run(AbsListView.java:3168)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.os.Handler.handleCallback(Handler.java:605)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.os.Looper.loop(Looper.java:137)
06-28 16:09:22.368: E/AndroidRuntime(1702): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-28 16:09:22.368: E/AndroidRuntime(1702): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 16:09:22.368: E/AndroidRuntime(1702): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 16:09:22.368: E/AndroidRuntime(1702): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-28 16:09:22.368: E/AndroidRuntime(1702): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-28 16:09:22.368: E/AndroidRuntime(1702): at dalvik.system.NativeStart.main(Native Method)
在此處添加異常日誌 – AAnkit
我還嘗試創建一個處理程序,並讓onclick事件向它傳遞消息以請求搜索對話框,但它有相同的問題(空指針)。 – Jbecwar
所以你現在明白了......未來開發的一切都是最好的...... :) – AAnkit