创建SIP客户端时出现空指针异常

时间:2014-08-27 22:03:53

标签: android sip

我试图创建一个SIP客户端,参考Android SIP演示。我已经创建了自己的类,但是当我尝试执行它时,我得到空指针异常。源代码是:

package com.example.sipclient;
import java.text.ParseException;
import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipRegistrationListener;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity{

EditText etUsername,etPassword,etIP;
Button bProfileBuilder,bRegisterProfile,bCloseProfile;

Intent intent = new Intent();
PendingIntent pendingIntent = null;

public SipManager mSipManager = null;
public SipProfile mSipProfile = null;
SipProfile.Builder builder = null;

boolean build=false,register=false,close=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Instantiate SIP Manager
    fSipInstantiate();

    etUsername = (EditText)findViewById(R.id.username);
    etPassword = (EditText)findViewById(R.id.password);
    etIP = (EditText)findViewById(R.id.ip);

    etUsername.setText("abhinav");
    etPassword.setText("abhinav");
    etIP.setText("192.168.0.1");

    bProfileBuilder = (Button)findViewById(R.id.buildProfile);
    bRegisterProfile = (Button)findViewById(R.id.registerProfile);
    bCloseProfile = (Button)findViewById(R.id.closeProfile);

    bProfileBuilder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Build SIP Profile
            fSipProfileBuild();
            build=true;
        }
    });

    bRegisterProfile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Register SIP Profile to server
            if(build=true)
            {
                fSipRegister();
                register=true;
                close=false;
            }
            else
                genToast("Build Profile First");
        }
    });

    bCloseProfile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Close SIP Profile
            if(register==true)
            {
                fSipProfileClose();
                close=true;
                register=false;
                genToast("Profile Closed Successfully");
            }
            else
                genToast("Profile Already Closed");
        }
    });
}

public void fSipInstantiate(){
    if(mSipManager == null){
        mSipManager = SipManager.newInstance(this);
        genToast("Instantiate Successful");
        return;
    }   
    genToast("Instantiate Failed");
    return;
}

public void fSipProfileBuild(){
    try {
        builder = new SipProfile.Builder(etUsername.getText().toString(),etIP.getText().toString());
        builder.setPassword(etPassword.getText().toString());
        mSipProfile = builder.build();
        genToast("Profile Build Successful");
    } catch (ParseException e) {
        genToast("Profile Build Failed");
        e.printStackTrace();
    }
    fSetIntentFilter();
    return;
}

public void fSetIntentFilter(){
    //Set Up an Intent filter to recieve calls
    try {
        intent.setAction("android.SipDemo.INCOMING_CALL");
        pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, Intent.FILL_IN_DATA);
        mSipManager.open(mSipProfile,pendingIntent,null);
        genToast("Intent Filter Set");
    }catch (SipException e) {
        // TODO Auto-generated catch block
        genToast("Intent Filter failed");
        e.printStackTrace();
    }
    return;
}

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

            @Override
            public void onRegistrationFailed(String localProfileUri, int errorCode,
                    String errorMessage) {
                bRegisterProfile.setText("Failed");
            }

            @Override
            public void onRegistrationDone(String localProfileUri, long expiryTime) {
                bRegisterProfile.setText("Ready");  
            }

            @Override
            public void onRegistering(String localProfileUri) {
                bRegisterProfile.setText("Registering");
            }
        });
    } catch (SipException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void fSipProfileClose() {
    if (mSipManager == null) {
        return;
    }
    try {
        if (mSipProfile != null) {
            mSipManager.close(mSipProfile.getUriString());
            genToast("Profile Closed Successfully");
            bRegisterProfile.setText("Register Profile");
        }
    } catch (Exception ee) {
        genToast("Failed to Close Profile");
    }
}

public void genToast(String TText){
    Toast toast = Toast.makeText(getApplicationContext(), TText, Toast.LENGTH_SHORT);
    toast.show();
    return;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

在这里,按下按钮" bProfileBuilder",我得到一个空指针异常。当我按下按钮" bRegisterProfile"时,会发生同样的事情。我似乎无法弄清楚出了什么问题。 StackTrace显示:

08-28 03:28:58.756: W/dalvikvm(21034): threadid=1: thread exiting with uncaught  exception (group=0x41b139a8)
08-28 03:28:58.791: E/AndroidRuntime(21034): FATAL EXCEPTION: main
08-28 03:28:58.791: E/AndroidRuntime(21034): java.lang.NullPointerException
08-28 03:28:58.791: E/AndroidRuntime(21034):    at com.example.sipclient.MainActivity.fSetIntentFilter(MainActivity.java:127)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at com.example.sipclient.MainActivity.fSipProfileBuild(MainActivity.java:118)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at com.example.sipclient.MainActivity$1.onClick(MainActivity.java:59)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at android.view.View.performClick(View.java:4212)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at android.view.View$PerformClick.run(View.java:17476)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at android.os.Handler.handleCallback(Handler.java:800)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at android.os.Handler.dispatchMessage(Handler.java:100)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at android.os.Looper.loop(Looper.java:194)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at android.app.ActivityThread.main(ActivityThread.java:5371)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at java.lang.reflect.Method.invokeNative(Native Method)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at java.lang.reflect.Method.invoke(Method.java:525)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-28 03:28:58.791: E/AndroidRuntime(21034):    at dalvik.system.NativeStart.main(Native Method)

请帮助。

1 个答案:

答案 0 :(得分:0)

我觉得这不会导致空指针异常,但是你的寄存器配置文件OnClickListener中有这个。

 if(build=true)
        {
            fSipRegister();
            register=true;
            close=false;
        }

我只是想指出这一点,万一你错过了。

另外,您是否尝试在其他设备上运行应用程序? mSipManager.open()的第三个参数是监听器,根据android引用(http://developer.android.com/reference/android/net/sip/SipManager.html),它可以被赋予null值。

当我尝试使用SipDemo代码时,我可以在除一个Google Nexus 10.1之外的多个设备上运行它,因为它总是停在该行并告诉我有一个空指针异常。如果你已经在其他设备上试用了你的应用程序,我不确定问题是什么。