1

我有一个想要设置 onTouchListener 的 toggleButton。但是我遇到了一些错误,所以我认为我做的不对。

我已经声明了我的按钮。

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;
                }
            }

        });




    }

在此处输入图像描述

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

4

2 回答 2

0

Eclipse 有时会自己犯一些错误。

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

还尝试在课程开始时删除所有导入,然后通过按重新导入所有内容

ctrl + SHIFT + o(在 Mac 上为 cmd+shift+o)

另一种解决方案是放置一个

return false;

在 onTouch 方法的最后

于 2013-10-25T12:32:35.503 回答
0

您应该在关闭触摸方法上添加“}”。

于 2013-10-25T11:54:39.890 回答