0

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

我认为这个人如何使用 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)
4

1 回答 1

0

我是个白痴。请注意,在 onItemCLick 处理程序中,在选择 1 的情况下,onSearchRequested() 被调用然后跳出开关,最终调用 c.startActivity(i),并且 i 被初始化为 null。这就是空指针最终来自的地方。对新手的错误感到抱歉。-詹姆士

于 2012-06-28T16:28:53.223 回答