0

我不知道相同代码在 Android 2.3 上运行正常的原因,但是当涉及到 android 3.1 平板电脑时,它会抛出 NullPointerExceptions ......这是代码:

public class CalleeButton extends Button implements OnClickListener, AddressAware {
private AddressText mAddress;
private OnClickListener externalClickListener;
public void setExternalClickListener(OnClickListener e) {externalClickListener = e;}
public void setAddressWidget(AddressText a) {a=mAddress;}

//set Uri SipUri ,so we can initialize the mAddress inside calleebutton --smartclassroom
//because every calleebutton will have one sip account/ext. this will replace the information for original mAddress --smartclassroom
public void setSip(String StudentName,String SipUri, AddressText a)
{
    Log.i("CalleeButton-35");
    super.setText(StudentName);
    //mAddress.setText(SipUri);
    Log.i("CalleeButton-38");
    mAddress.setContactAddress(SipUri, StudentName);
    Log.i("CalleeButton-40");
    setAddressWidget(a);
    Log.i("CalleeButton-42");

}

public CalleeButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOnClickListener(this);

    //must use this line or you got null pointer exception...smartclassroom
    Log.i("CalleeButton-50");
    mAddress = new AddressText(context, attrs);
    //mAddress = null;
    Log.i("CalleeButton-52");
}

public void onClick(View v) {
    try {
        if (!LinphoneManager.getInstance().acceptCallIfIncomingPending()) {
            if (mAddress.getText().length() >0) { 
            LinphoneManager.getInstance().newOutgoingCall(mAddress);
                //add new action here
                /*******************************************************/
                /*******************************************************/
            }
        }
    } catch (LinphoneCoreException e) {
        LinphoneManager.getInstance().terminateCall();
        onWrongDestinationAddress();
    };

    if (externalClickListener != null) externalClickListener.onClick(v);
}


protected void onWrongDestinationAddress() {
    Toast toast = Toast.makeText(getContext()
            ,String.format(getResources().getString(R.string.warning_wrong_destination_address),mAddress.getText().toString())
            ,Toast.LENGTH_LONG);
    toast.show();
}


}

实际上我在 Android 2.3 平台上得到了 NullPointerException 一次,但在添加之后:

mAddress = new AddressText(context, attrs);

然后它编译并顺利运行...直到我尝试在 android 3.1 平板电脑上安装相同的程序...很奇怪,不是吗?请让我了解我在这方面犯了什么错误,在此先感谢!logcat 的错误消息是:

04-25 23:58:13.308: ERROR/AndroidRuntime(6728): FATAL EXCEPTION: main
04-25 23:58:13.308: ERROR/AndroidRuntime(6728): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.linphone/org.linphone.LinphoneActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.linphone/org.linphone.DialerActivity}: java.lang.NullPointerException
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.os.Looper.loop(Looper.java:132)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.main(ActivityThread.java:4028)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at java.lang.reflect.Method.invoke(Method.java:491)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at dalvik.system.NativeStart.main(Native Method)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.linphone/org.linphone.DialerActivity}: java.lang.NullPointerException
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1592)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:130)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:342)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.widget.TabHost.setCurrentTab(TabHost.java:345)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.widget.TabHost.setCurrentTabByTag(TabHost.java:282)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at org.linphone.LinphoneActivity.gotToDialer(LinphoneActivity.java:481)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at org.linphone.LinphoneActivity.fillTabHost(LinphoneActivity.java:207)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at org.linphone.LinphoneActivity.onCreate(LinphoneActivity.java:126)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     ... 11 more
04-25 23:58:13.308: ERROR/AndroidRuntime(6728): Caused by: java.lang.NullPointerException
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at org.linphone.DialerActivity.onCreate(DialerActivity.java:128)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
04-25 23:58:13.308: ERROR/AndroidRuntime(6728):     ... 22 more 

在我看来,logcat msgs 告诉了我有关该功能的信息:

    mCallee1.setSip("Jack","sip:xxx@xxx.xxx.xxx.xxx",mAddress);

在 DailerActivity.java(您可以将其视为主要函数)中导致 NullPointerExcetion,然后我将代码跟踪到 CalleeButton.java,但仍然无法找到导致空指针异常的点...

4

1 回答 1

1

在该行 ( setSip()) 上设置断点,并检查 mCallee1 和 mAddress 变量。一个应该为空。那将是你的问题。然后只需追溯它并找出它没有被实例化的原因——或者如果 null 是变量的有效状态,只需在调用之前检查它是否为 null setSip()

于 2012-04-25T17:36:04.280 回答