0

I don't know the reason why the same code runs OK on Android 2.3 but when it comes to android 3.1 tablet, it throws NullPointerExceptions...here's the code:

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();
}


}

Actually I got NullPointerException on Android 2.3 Platform once, but after adding:

mAddress = new AddressText(context, attrs);

then it compiles and runs smoothly...until I try to install the same program on android 3.1 tablet...weird, isn't it ? Please let me learn what mistakes I have made on this, thanks in advance! the error msgs for logcat are:

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 

From my opinion,the logcat msgs told me about the function:

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

in DailerActivity.java(which you can treat it as main function here) caused the NullPointerExcetion, then I trace the code to CalleeButton.java, but still can't get find the point where caused the null pointer exception...

shanwu
  • 1,493
  • 6
  • 35
  • 45

1 Answers1

1

Set a breakpoint on that line (setSip()), and check the mCallee1 and mAddress variables. One should be null. That will be your problem. Then just trace it back and find out why it isn't being instantiated -- or if null is a valid state for the variable, just check it for null before calling setSip().

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • after setup the exception breakpoint, then I debug with eclipse. I found that the value of variable mCallee1 is null. I can't find out the reason why... variable type of mCallee1(which is CalleeButton) is very similar to variable type of mCall(which is CallButton). I tried many different ways try to get around it, but the value is still the same, null... – shanwu Apr 26 '12 at 03:34
  • Edit in the code where you are running into this. You don't have anything involving an `mCallee1` variable in what's posted. – Kevin Coppock Apr 26 '12 at 12:41
  • 1
    I found it!!! it's because my layout problem. My android 3.0 tablet only uses landscape layout which I ignored to initialize those xml layout files. – shanwu May 29 '12 at 09:46