我的Android应用程序强制关闭与吐司和动作栏搜索

时间:2013-06-04 18:34:42

标签: android android-actionbar toast

我的应用程序只有一个动作栏搜索(不工作)在我的android galaxy nexus上成功加载。当我真正实现搜索时,通过检索搜索并弹出一个Toast,它会在启动时崩溃。

我的日食错误律师表明了这一点:

06-04 14:33:06.902: D/libEGL(11017): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
06-04 14:33:06.956: D/libEGL(11017): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
06-04 14:33:06.987: D/libEGL(11017): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
06-04 14:33:07.112: D/OpenGLRenderer(11017): Enabling debug mode 0
06-04 14:33:07.237: D/AndroidRuntime(11017): Shutting down VM
06-04 14:33:07.237: W/dalvikvm(11017): threadid=1: thread exiting with uncaught exception (group=0x4121c930)
06-04 14:33:07.253: E/AndroidRuntime(11017): FATAL EXCEPTION: main
06-04 14:33:07.253: E/AndroidRuntime(11017): java.lang.ClassCastException: com.example.beerportfoliopro.MainActivity cannot be cast to android.widget.SearchView$OnQueryTextListener
06-04 14:33:07.253: E/AndroidRuntime(11017):    at com.example.beerportfoliopro.MainActivity.onCreateOptionsMenu(MainActivity.java:25)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:460)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:822)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:253)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at android.view.Choreographer.doFrame(Choreographer.java:531)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at android.os.Handler.handleCallback(Handler.java:725)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at android.os.Looper.loop(Looper.java:137)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at android.app.ActivityThread.main(ActivityThread.java:5226)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at java.lang.reflect.Method.invokeNative(Native Method)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at java.lang.reflect.Method.invoke(Method.java:511)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
06-04 14:33:07.253: E/AndroidRuntime(11017):    at dalvik.system.NativeStart.main(Native Method)

我执行此操作的主要内容如下:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Toast;


public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);

                SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
                searchView.setOnQueryTextListener((OnQueryTextListener) this);
                return true;
        }

        //get entered search and pop up a toast to show query entered
        public boolean onQueryTextSubmit (String query) {
            Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();

            return true;
        }



}

2 个答案:

答案 0 :(得分:3)

searchView.setOnQueryTextListener((OnQueryTextListener) this);

在这一行中你试图将一个活动转换为你的OnQueryTextListener类。这是非常错误的。

而不是将侦听器对象作为参数发送

答案 1 :(得分:0)

更改您的搜索视图代码,如下所示。

SearchView searchView =(SearchView)menu.findItem(R.id.menu_search).getActionView();

    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            return false;
        }
    });