Android SIP堆栈:寄存器上的NullPointerException

我一直在运行2.3.3的HTC Desire Z上编写代码并且工作正常 – 现在我来测试另一台设备(Nexus One运行2.3.4),它崩溃了.这是代码:

if (me == null || sipManager == null || listener == null){
    Log.e("OH","NO");
}

sipManager.register(me, 30, listener);

我传递给sipManager.register的东西都不是null(永远不会调用日志),但这是我的堆栈跟踪:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to create service com.myapp.Service: java.lang.NullPointerException
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1955)
    at android.app.ActivityThread.access$2500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at android.net.sip.SipManager.register(SipManager.java:474)
    at com.myapp.Service.onCreate(Service.java:159)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1945)
    ... 10 more

看看Android源代码似乎也没有显示任何明显的东西 – 我传递给它的所有内容在另一台设备上都有效.

两台设备都运行2.3并且都启用了SIP堆栈:

if (SipManager.isVoipSupported(this) && SipManager.isApiSupported(this)){

编辑:

这是一个复制问题的完整活动:

public class SIPTest extends Activity {

    public SipManager sipManager = null;
    SipProfile me = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        if (SipManager.isVoipSupported(this) && SipManager.isApiSupported(this)){
            // SIP is supported, let's go!
            Log.e("SIP", "SUPPORTED");
            sipManager = SipManager.newInstance(this);


            String username = "username";
            String password = "password";
            String domain = "example.org";

            try {
                SipProfile.Builder builder = new SipProfile.Builder(username, domain);
                builder.setPassword(password);
                builder.setDisplayName(username);
                builder.setProfileName(username + "@" + domain);
                me = builder.build();

                sipManager.register(me, 30, new SipRegistrationListener() {
                        public void onRegistering(String localProfileUri) {
                            Log.e("SIP","Registering with SIP Server...");
                        }

                        public void onRegistrationDone(String localProfileUri, long expiryTime) {
                            Log.e("SIP","Ready!");
                        }

                        public void onRegistrationFailed(String localProfileUri, int errorCode,
                                String errorMessage) {
                            Log.e("SIP","Registration failed. " + errorMessage + " ("+errorCode+") - " + localProfileUri);
                        }
                    });
            } catch (ParseException pe) {
                Log.e("SIP","Connection Error.");
            } catch (SipException se) {
                Log.e("SIP","Connection error.");
            }

        }

    }
}

解决方法:

你应该放置sipManager.open(我);在执行sipManager.register()之前,需要创建新的sip会话.

照顾自己!

上一篇:使用pjsua2 Android保持和取消保持呼叫


下一篇:java – 如何正确地重新注册sip服务器?