如何实现onTouchListener?

时间:2013-10-25 11:46:18

标签: android

我有一个toggleButton,我希望在onTouchListener上设置。我收到了一些错误,所以我认为我没做错。

我已经宣布了我的按钮。

private ToggleButton pushBtn;

我已设置我的活动来实现View.onTouchListener。

public class InCallActivity extends SherlockFragmentActivity implements  View.OnTouchListener {

在onCreate中我写过:

pushBtn = (ToggleButton) findViewById(R.id.PTT_button3);
pushBtn.setOnTouchListener(this);

然后我尝试在类::

中使用监听器
pushBtn.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        // if more than one call, change this code
        int callId = 0;
        for (SipCallSession callInfo : callsInfo)
        {
            callId = callInfo.getCallId();
            Log.e(TAG, "" + callInfo.getCallId());

        }
        final int id = callId;
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {

                // press
                pushBtn.setBackgroundResource(R.drawable.btn_blue_glossy);
                pushBtn.setChecked(true);
                OnDtmf(id, 17, 10);
                OnDtmf(id, 16, 9);

                return true;
            }
            case MotionEvent.ACTION_UP:
            {

                // release
                pushBtn.setBackgroundResource(R.drawable.btn_lightblue_glossy);
                pushBtn.setChecked(false);
                OnDtmf(id, 18, 11);
                OnDtmf(id, 18, 11);

                return true;
            }
            default:
                return false;
        }
    }

});

我有多个错误,它说onTouch没有实现,但我已经这样做了? 我也把听众放在了课堂上,现在它在说出之前就抱怨了这个方法:

Syntax error on token "}", delete this token

但是在我添加监听器之前,这是好的,侦听器必须在某个方法中吗?

在我的听众结束时,我收到错误:

Syntax error, insert "}" to complete MethodBody

但我现在知道为什么。

编辑更新

这是我的onCreate,我得到了nullpointerexception

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        //handler.setActivityInstance(this);
        Log.d(THIS_FILE, "Create in call");
        setContentView(R.layout.in_call_main);

        SipCallSession initialSession = getIntent().getParcelableExtra(SipManager.EXTRA_CALL_INFO);
        synchronized (callMutex) {
            callsInfo = new SipCallSession[1];
            callsInfo[0] = initialSession;
        }

        bindService(new Intent(this, SipService.class), connection, Context.BIND_AUTO_CREATE);
        prefsWrapper = new PreferencesProviderWrapper(this);

        // Log.d(THIS_FILE, "Creating call handler for " +
        // callInfo.getCallId()+" state "+callInfo.getRemoteContact());
        powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
                "com.csipsimple.onIncomingCall");
        wakeLock.setReferenceCounted(false);


        takeKeyEvents(true);

        // Cache findViews
        mainFrame = (ViewGroup) findViewById(R.id.mainFrame);
        inCallControls = (InCallControls) findViewById(R.id.inCallControls);
        inCallAnswerControls = (InCallAnswerControls) findViewById(R.id.inCallAnswerControls);
        activeCallsGrid = (InCallInfoGrid) findViewById(R.id.activeCallsGrid);
        heldCallsGrid = (InCallInfoGrid) findViewById(R.id.heldCallsGrid);
        pushBtn = (ToggleButton) findViewById(R.id.PTT_button3);
        //pushBtn.setOnTouchListener((OnTouchListener) this);
        attachVideoPreview();

        inCallControls.setOnTriggerListener(this);
        inCallAnswerControls.setOnTriggerListener(this);

        if(activeCallsAdapter == null) {
            activeCallsAdapter = new CallsAdapter(true);
        }
        activeCallsGrid.setAdapter(activeCallsAdapter);


        if(heldCallsAdapter == null) {
            heldCallsAdapter = new CallsAdapter(false);
        }
        heldCallsGrid.setAdapter(heldCallsAdapter);


        ScreenLocker lockOverlay = (ScreenLocker) findViewById(R.id.lockerOverlay);
        lockOverlay.setActivity(this);
        lockOverlay.setOnLeftRightListener(this);

        /*
        middleAddCall = (Button) findViewById(R.id.add_call_button);
        middleAddCall.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                onTrigger(ADD_CALL, null);
            }
        });
        if (!prefsWrapper.getPreferenceBooleanValue(SipConfigManager.SUPPORT_MULTIPLE_CALLS)) {
            middleAddCall.setEnabled(false);
            middleAddCall.setText(R.string.not_configured_multiple_calls);
        }
        */

        // Listen to media & sip events to update the UI
        registerReceiver(callStateReceiver, new IntentFilter(SipManager.ACTION_SIP_CALL_CHANGED));
        registerReceiver(callStateReceiver, new IntentFilter(SipManager.ACTION_SIP_MEDIA_CHANGED));
        registerReceiver(callStateReceiver, new IntentFilter(SipManager.ACTION_ZRTP_SHOW_SAS));

        proximityManager = new CallProximityManager(this, this, lockOverlay);
        keyguardManager = KeyguardWrapper.getKeyguardManager(this);

        dialFeedback = new DialingFeedback(this, true);

        if (prefsWrapper.getPreferenceBooleanValue(SipConfigManager.PREVENT_SCREEN_ROTATION)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        if (quitTimer == null) {
            quitTimer = new Timer("Quit-timer");
        }


        useAutoDetectSpeaker = prefsWrapper.getPreferenceBooleanValue(SipConfigManager.AUTO_DETECT_SPEAKER);

        applyTheme();
        proximityManager.startTracking();

        inCallControls.setCallState(initialSession);
        inCallAnswerControls.setCallState(initialSession);




        pushBtn.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //if more than one call, change this code
              int callId = 0;
              for (SipCallSession callInfo : callsInfo) {
                  callId = callInfo.getCallId();
                  Log.e(TAG, ""+callInfo.getCallId());

              }
              final int id= callId;
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {

                    //press
                    pushBtn.setBackgroundResource(R.drawable.btn_blue_glossy);
                    pushBtn.setChecked(true);
                    OnDtmf(id, 17, 10);
                    OnDtmf(id, 16, 9);

                    return true;
                }
                case MotionEvent.ACTION_UP: {

                    //release
                    pushBtn.setBackgroundResource(R.drawable.btn_lightblue_glossy);
                    pushBtn.setChecked(false);
                    OnDtmf(id, 18, 11); 
                    OnDtmf(id, 18, 11);

                    return true;
                }
                default:
                    return false;
                }
            }

        });




    }

enter image description here

空指针250的行是pushBtn.setOnTouchListener(new OnTouchListener() {

2 个答案:

答案 0 :(得分:0)

你应该在关闭触摸方法上添加'}'。

答案 1 :(得分:0)

Eclipse有时会犯一些错误。

您的代码似乎正确,请尝试删除所有代码,然后再将其粘贴到同一位置。

还尝试删除课程开头的所有导入,然后按

重新导入所有内容

ctrl + SHIFT + o(cmd + shift + o在mac上)

另一种解决方案是放一个

return false;

在onTouch方法的最后