我正在使用Twilio android SDK,当我点击“挂断”按钮时总是出现错误?拨号后无法断开呼叫?

我正在使用Twilio android SDK,当我点击“挂断”按钮时总是出现错误?拨号后无法断开呼叫?,android,twilio,Android,Twilio,我已成功集成了适用于android的Twilio SDK,但当我尝试使用它进行呼叫时,它未连接并在logcat中显示错误消息,我收到以下错误: 1) Failed to hangup call due to error code: 70015, message: pjsua_call_hangup(): Object already exists (PJ_EEXISTS) 2) Connection disconnected with error code 31000 and message

我已成功集成了适用于android的Twilio SDK,但当我尝试使用它进行呼叫时,它未连接并在logcat中显示错误消息,我收到以下错误:

1) Failed to hangup call due to error code: 70015, message: pjsua_call_hangup(): Object already exists (PJ_EEXISTS) 

2) Connection disconnected with error code 31000 and message Generic error 

this both errors occurs when i am calling disconnect method if i comment this method then my calling is working fine and i make call but if i call this method phone.disconnect(), i am getting error shown as above. 

This is my methods which i am using to make calls and to disconnect the calls. 
//////打电话

public void connect(String phoneNumber) { 
Map parameters = new HashMap(); 
parameters.put("PhoneNumber", phoneNumber); 
connection = device.connect(parameters, null /* ConnectionListener */); 
if (connection == null) 
Log.w(TAG, "Failed to create new connection"); 
} 
//断开电话

public void disconnect() { 
if (connection != null) { 
connection.disconnect(); 
connection = null; // will null out in onDisconnected() 
if (basicConnectionListener != null) 
basicConnectionListener.onConnectionDisconnecting(); 
} 
} 
在我的OnClick活动中:

public void onClick(View view) { 
if (view.getId() == R.id.dialButton) 
Toast.makeText(getApplicationContext(), "Dialing...", Toast.LENGTH_LONG).show(); 
phone.connect("PHONE NUMBER"); 
if (view.getId() == R.id.hangupButton) 
Toast.makeText(getApplicationContext(), "Call Disconnected...", Toast.LENGTH_LONG).show(); 
phone.disconnect(); 

Please suggest me and help me because i tried all possible thing for hangout but still not able to solve it.Thanks in advance. ![enter image description here][1]

我正在为安装共享我的工作类

这是对我来说非常有效的调用。我想分享我的类,以便任何人都可以参考它

这是处理twillio调用和断开调用的所有事件的基类

    import java.util.HashMap;
    import java.util.Map;

    import android.content.Context;
    import android.media.AudioManager;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Toast;

    import com.twilio.client.Connection;
    import com.twilio.client.Connection.State;
    import com.twilio.client.ConnectionListener;
    import com.twilio.client.Device;
    import com.twilio.client.DeviceListener;
    import com.twilio.client.PresenceEvent;
    import com.twilio.client.Twilio;

    public class MonkeyPhone implements Twilio.InitListener, DeviceListener, ConnectionListener {
        private static final String TAG = "MonkeyPhone";
        private Device device;
        private Connection connection;
        private final Context context;
        private BasicConnectionListener basicConnectionListener;
        private BasicDeviceListener basicDeviceListener;
        private Connection pendingIncomingConnection;
        InputMethodManager imm;
        private boolean speakerEnabled;
        private boolean muteEnabled;

        public interface BasicConnectionListener {
            public void onIncomingConnectionDisconnected();

            public void onConnectionConnecting();

            public void onConnectionConnected();

            public void onConnectionFailedConnecting(Exception error);

            public void onConnectionDisconnecting();

            public void onConnectionDisconnected();

            public void onConnectionFailed(Exception error);
        }

        public interface BasicDeviceListener {
            public void onDeviceStartedListening();

            public void onDeviceStoppedListening(Exception error);
        }

        public MonkeyPhone(Context context) {
            this.context = context;
            Twilio.initialize(context, this /* Twilio.InitListener */);
        }

        public void setListeners(BasicConnectionListener basicConnectionListener, BasicDeviceListener basicDeviceListener) {
            this.basicConnectionListener = basicConnectionListener;
            this.basicDeviceListener = basicDeviceListener;
        }

        /* Twilio.InitListener method */
        @Override
        public void onInitialized() {
            Log.d(TAG, "Twilio SDK is ready");
            try {
                // String capabilityToken =
                // HttpHelper.httpGet("http://------/mobile/auth.php");
                String capabilityToken = HttpHelper.httpGet("http:------/auth.php");

                device = Twilio.createDevice(capabilityToken, null /* DeviceListener */);
            } catch (Exception e) {
                Log.e(TAG, "Failed to obtain capability token: " + e.getLocalizedMessage());
            }
        }

        /* Twilio.InitListener method */
        @Override
        public void onError(Exception e) {
            Log.e(TAG, "Twilio SDK couldn't start: " + e.getLocalizedMessage());
        }

        @Override
        protected void finalize() {
            if (device != null)
                device.release();
            if (connection != null)
                connection.disconnect();
        }

        // To Make Calls

        public void connect(String phoneNumber) {
            Toast toast = Toast.makeText(context, "Dialing...", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("PhoneNumber", phoneNumber);
            String capabilityToken;
            try {
                // capabilityToken =
                // HttpHelper.httpGet("http://------/mobile/auth.php");
                capabilityToken = HttpHelper.httpGet("http://------/mobile/auth.php");

                device = Twilio.createDevice(capabilityToken, null /* DeviceListener */);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            try {
                device.disconnectAll();
            } catch (Exception e) {
                e.printStackTrace();
            }
            connection = device.connect(parameters, null /* ConnectionListener */);
            if (connection == null) {
                Log.w(TAG, "Failed to create new connection");
            }
        }

        // To Disconnect Phone
        public void disconnect() {
            Toast toast = Toast.makeText(context, "Call Disconnected...", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();

            if (connection != null) {
                connection.disconnect();
                connection = null; // will null out in onDisconnected()

                if (basicConnectionListener != null)
                    basicConnectionListener.onConnectionDisconnecting();
            }
        }

        public void setSpeakerEnabled(boolean speakerEnabled) {
            if (speakerEnabled != this.speakerEnabled) {
                this.speakerEnabled = speakerEnabled;
                updateAudioRoute();
            }
        }

        private void updateAudioRoute() {
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setSpeakerphoneOn(speakerEnabled);
        }

        public void setMuteEnabled(boolean muteEnabled) {
            if (muteEnabled != this.muteEnabled) {
                this.muteEnabled = muteEnabled;
                updateAudioRouteForMute();
            }
        }

        private void updateAudioRouteForMute() {
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setMicrophoneMute(muteEnabled);
        }

        public State status() {
            connection.getState();
            State statusHere = connection.getState();
            return statusHere;
        }

        @Override
        public void onConnected(Connection arg0) {
            updateAudioRoute();
            updateAudioRouteForMute();
            if (basicConnectionListener != null) {
                basicConnectionListener.onConnectionConnected();
            }
        }

        @Override
        public void onConnecting(Connection arg0) {
            if (basicConnectionListener != null) {
                basicConnectionListener.onConnectionConnecting();
            }
        }

        @Override
        public void onDisconnected(Connection inConnection) {
            if (inConnection == connection) {
                connection = null;
                if (basicConnectionListener != null)
                    basicConnectionListener.onConnectionDisconnected();
            } else if (inConnection == pendingIncomingConnection) {
                pendingIncomingConnection = null;
                if (basicConnectionListener != null)
                    basicConnectionListener.onIncomingConnectionDisconnected();
            }
        }

        @Override
        public void onDisconnected(Connection inConnection, int arg1, String inErrorMessage) {
            if (inConnection == connection) {
                connection = null;
                if (basicConnectionListener != null)
                    basicConnectionListener.onConnectionFailedConnecting(new Exception(inErrorMessage));
            }

        }

        @Override
        public void onPresenceChanged(Device arg0, PresenceEvent arg1) {

        }

        @Override
        public void onStartListening(Device arg0) {
            if (basicDeviceListener != null) {
                basicDeviceListener.onDeviceStartedListening();
            }
        }

        @Override
        public void onStopListening(Device arg0) {
            if (basicDeviceListener != null) {
                basicDeviceListener.onDeviceStoppedListening(null);
            }
        }

        @Override
        public void onStopListening(Device arg0, int arg1, String arg2) {

        }

        @Override
        public boolean receivePresenceEvents(Device arg0) {
            return false;
        }

    }

嗨,我也面临同样的问题。如果您得到了解决方案,请将其发布在此处有时我的应用程序会出现在
connection=device.connect(参数,null/*ConnectionListener*/)上
你能帮我一下吗?@AnshulTyagi您好,有时候会发生这种情况,因为设备无法创建新连接。所以它会崩溃。请尝试使用我创建的上述类,它在所有情况下都非常适合我。谢谢。我已经测试了15次,它不再崩溃了。我刚刚在try-catch块
device=Twilio.createDevice(capabilityToken,null/*DeviceListener*/)中添加了这一行@AnshulTyagi很高兴听到我的答案对你有用。如果你觉得我的答案有用,你可以接受。谢谢:)@Nidhi嘿,这个代码对我来说很有魅力,我想你在Twillio帐户上的代币有问题。请检查你的代币和帐户设置,这可能对你有帮助。
  private MonkeyPhone phone;
  //On create
  phone = new MonkeyPhone(getApplicationContext());

  //On Call Button Click

  @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btnCallHere) {

            phone.connect(newContact);  

        } else if (view.getId() == R.id.btnDisconnectHere) {

            phone.disconnect();

        }