SIP Connection - Android Java Softphone

5

My intention is to create a Softphone, for this I'm consulting the google libraries and doing some testing, but I packed in a part.

I do not want to do start-up functions, or multiple screens, or anything complicated. I just want to make a call.

I created the instances as google says and so on. Below is my code.

MainActivity.java

package br.com.coligarse.testevoip;

import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipRegistrationListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.text.ParseException;

public class MainActivity extends AppCompatActivity {
    TextView status;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SipManager mSipManager = null;

        if (mSipManager == null) {
            mSipManager = SipManager.newInstance(this);
        }
        SipProfile mSipProfile = null;


        SipProfile.Builder builder = null;
        try {
            builder = new SipProfile.Builder("100", "voip.servidor.net.br:5082");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        builder.setPassword("123456");
        mSipProfile = builder.build();

        try {
            mSipManager.setRegistrationListener(mSipProfile.getUriString(), new SipRegistrationListener() {

                public void onRegistering(String localProfileUri) {
                    updateStatus("Registering with SIP Server...");
                }

                public void onRegistrationDone(String localProfileUri, long expiryTime) {
                    updateStatus("Ready");
                }

                public void onRegistrationFailed(String localProfileUri, int errorCode,
                                                 String errorMessage) {
                    updateStatus("Registration failed.  Please check settings.");
                }

            });
        } catch (SipException e) {
            e.printStackTrace();
        }

    }

    public void updateStatus(String msg){
        status = (TextView) findViewById(R.id.status);
        status.setText(msg);
    }
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.coligarse.testevoip">
    <receiver android:name=".IncomingCallReceiver" android:label="Call Receiver"/>
    ...
    <uses-sdk android:minSdkVersion="9" />
    <uses-permission android:name="android.permission.USE_SIP" />
    <uses-permission android:name="android.permission.INTERNET" />
    ...
    <uses-feature android:name="android.hardware.sip.voip" android:required="true" />
    <uses-feature android:name="android.hardware.wifi" android:required="true" />
    <uses-feature android:name="android.hardware.microphone" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="br.com.coligarse.testevoip.MainActivity">

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

When I run this code. It opens the app in white and in the sequence it says it has stopped working and closes.

Could someone tell me where I'm going wrong or give me an orientation on how to continue?

I really doubt how to proceed. I've read a lot of documents, but I'm a beginner and I'm not finding a certain romo. My intention is to develop everything in android studio through the SDK without using external plugins or NDK.

LOGCAT

 --------- beginning of crash
 09-12 13:06:47.543 2416-2416/br.com.coligarse.testevoip E/AndroidRuntime: FATAL EXCEPTION: main
 Process: br.com.coligarse.testevoip, PID: 2416
 java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.coligarse.testevoip/br.com.coligarse.testevoip.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.sip.SipManager.setRegistrationListener(java.lang.String, android.net.sip.SipRegistrationListener)' on a null object reference
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
 at android.app.ActivityThread.-wrap11(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.sip.SipManager.setRegistrationListener(java.lang.String, android.net.sip.SipRegistrationListener)' on a null object reference
 at br.com.coligarse.testevoip.MainActivity.onCreate(MainActivity.java:38)
 at android.app.Activity.performCreate(Activity.java:6237)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    
asked by anonymous 10.09.2016 / 01:22

1 answer

1

Logcat says:

  
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.sip.SipManager.setRegistrationListener(java.lang.String, android.net.sip.SipRegistrationListener)' on a null object reference

That is, mSipManager was null .

See method documentation SipManager.newInstance(Context) :

  

newInstance

     

SipManager newInstance (Context context)

     

Creates a manager instance. Returns null if SIP API is not supported.

     

Parameters

     

context Context: application context for creating the manager object

     

Returns

     

SipManager the manager instance or null if SIP API is not supported

Translating to Portuguese:

  

newInstance

     

SipManager newInstance (Context context)

     

Creates a manager instance. Returns null if the SIP API is not supported.

     

Parameters

     

context Context: application context for creating the manager object

     

Returns

     

SipManager the manager instance or null if the SIP API is not supported

That is, the newInstance(Context) method can return null . You do not treat this in your code. See:

    SipManager mSipManager = null;

    // SEMPRE vai entrar.
    if (mSipManager == null) {
        // Pode retornar null.
        mSipManager = SipManager.newInstance(this);
    }

    // ...

    try {
        // Se o newInstance retornou null, vai dar NullPointerException.
        mSipManager.setRegistrationListener(/* ... */);

That is, if SipManager.newInstance(Context) returns null , its mSipManager.setRegistrationListener will give NullPointerException .

The solution to this is to first check that SipManager.newInstance(Context) returns null , and if so treat accordingly.

Secondly, I think you should use InCallService instead of AppCompatActivity .

    
12.09.2016 / 15:39