SmsManager Null Pointer Exception

蓝咒 提交于 2020-01-03 03:22:09

问题


I am trying to send a text message using smsManager from a BroadCast Receiver.I am getting this error says NullPointerException. I do not know what is wrong with this. I am sick of this error. here is my code segment

GPSTracker tracker = new GPSTracker(context);
            MyLocation temp = tracker.getCurrentLocation();
            String message = sp.getString("auto_text", "") + "\n"
                    + "Latitude: "+temp.getLatitude() + "\nLogitude: "+temp.getLongitude();
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
            smsManager.sendTextMessage(smsMessage.getOriginatingAddress(),
                    null, message, null, null);

String message is not null. it is displayed in toast . If I Replace message with

sp.getString("auto_text","")+" "+temp.getLatitude()+" "+temp.getLongitude();

then it is working fine.

MyLocation.java

import android.os.Parcel;
import android.os.Parcelable;

public class MyLocation implements Parcelable{

    double latitude,longitude;
    public MyLocation() {
    }
    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }
    @Override
    public String toString() {
        return getLatitude()+" , "+getLongitude();
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeDouble(latitude);
        dest.writeDouble(longitude);

    }
    public static final Parcelable.Creator<MyLocation> CREATOR = new Parcelable.Creator<MyLocation>() {
        public MyLocation createFromParcel(Parcel pc) {
            return new MyLocation(pc);
        }

        public MyLocation[] newArray(int size) {
            return new MyLocation[size];
        }
    };
    public MyLocation(Parcel pc){
        latitude = pc.readDouble();
        longitude  = pc.readDouble();

    }

}

LogCat

FATAL EXCEPTION: main
02-03 08:54:22.822: E/AndroidRuntime(3124): java.lang.RuntimeException: Unable to start receiver com.blogspot.uappmarket.locationbasedproject.Receiver.InCommingSmsReceiver: java.lang.NullPointerException
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.os.Looper.loop(Looper.java:130)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at java.lang.reflect.Method.invoke(Method.java:507)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at dalvik.system.NativeStart.main(Native Method)
02-03 08:54:22.822: E/AndroidRuntime(3124): Caused by: java.lang.NullPointerException
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.os.Parcel.readException(Parcel.java:1328)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.os.Parcel.readException(Parcel.java:1276)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:369)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at com.blogspot.uappmarket.locationbasedproject.Receiver.InCommingSmsReceiver.onReceive(InCommingSmsReceiver.java:33)
02-03 08:54:22.822: E/AndroidRuntime(3124):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)

回答1:


I just found a valid reason for this problem. That is : SmsManager.sendTextMessage() throws NullPointerException when SMS message is over size limit. i.e. max character limit is 159.




回答2:


Maybe Harpreet is right, in fact I ran into this situation just now.

you can just use the method SmsManager.divideMessage(String text) to let the system handle the length.




回答3:


The SmsManager.sendTextMessage() method also throws a NPE in the case that an invalid number is passed in as the destination. I found this out first hand when I was passing in a formatted phone number (e.g. "+1 999 999 9999"). The method did not throw a NPE when I stripped out the empty chars and removed the "+" before the country code.




回答4:


@harpreet aswer pointed me to the source of the error.

Note that SMS message length limit may vary depending on you mobile network operator.

Then you have two solutions:

1 - To send a shorter message.

-or-

2 - Split them in several messages (This may cause extra cost)

SmsManager sms = SmsManager.getDefault();
ArrayList<String> dividedText = sms.divideMessage(message);
for (String shortMessage : dividedText) {
    sms.sendTextMessage(phoneNumber,null,shortMessage,null,null);
    }


来源:https://stackoverflow.com/questions/14674890/smsmanager-null-pointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!