0

我正在尝试通过触摸启动语音识别,但问题是用户触摸屏幕时未调用 OnTouchListener

我试图在函数中放置断点,但它根本没有出现在函数中

在我的笔记本电脑上,Logcat 无法正常工作,因此我无法使用日志,因此无法使用 toast 来查看是否调用了该函数。

代码如下。任何人都可以请帮助为什么不调用此功能?

更新:

我在 OnCreate 中调用 setOnTouchListener,现在使用视图后,我的应用程序在启动时崩溃。

View view = findViewById(R.layout.voice_recog);
    view.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Toast toast = Toast.makeText(getApplicationContext(),"Touch recognised",Toast.LENGTH_LONG);
            toast.show();
            startVoiceRecognitionActivity();

            return false;
        }
    });

我发现问题在于分配视图...由于找不到源错误,应用程序崩溃...在线

    View view = findViewById(R.layout.voice_recog);
    //crashes on below line ....with Source not found error
    view.setOnTouchListener(new View.OnTouchListener() {
4

2 回答 2

0

我猜您没有将侦听器分配给任何视图,因为您只是在这里创建了侦听器。用于findViewById()获取要在其上运行此代码的任何视图的实例,然后调用:

yourView.setOnTouchListener(TouchListener);
于 2013-04-10T19:18:25.250 回答
0

在你的 onCreate 中试试这个:

getView().setOnTouchListener(myTouchListener);
于 2013-04-10T19:19:49.190 回答