从OnItemClickListener调用搜索对话框

时间:2012-06-28 15:25:54

标签: android

我遇到了一个奇怪的问题。我正在尝试在我的应用中实施搜索。如果我按下搜索按钮或者如果我从onStart()函数调用onSearchRequested(),则会显示搜索屏幕。但是如果我从OnItemClickListener的onItemClick函数调用onSearchReqeusted(),我会得到一个NullPointerException。我还尝试获取搜索管理器的实例,并直接调用startSearch函数,但它也失败了。

我认为这个人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)

1 个答案:

答案 0 :(得分:0)

我是个白痴。请注意,在onItemCLick处理程序中,在选择1的情况下,onSearchRequested()被调用然后中断交换机,最终调用c.startActivity(i),并且我被初始化为null。这就是空指针最终来自的地方。对不起新手的错误。 -James