1

我正在尝试使用smsManager从发送短信BroadCast Receiver。我收到此错误消息NullPointerException。我不知道这有什么问题。我厌倦了这个错误。这是我的代码段

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

字符串消息不为空。它显示在 toast 中。如果我将消息替换为

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

然后它工作正常。

我的位置.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();

    }

}

日志猫

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)
4

4 回答 4

1

我刚刚找到了这个问题的正当理由。即:SmsManager.sendTextMessage() throws NullPointerException当 SMS 消息超过大小限制时。即最大字符限制为 159。

于 2013-03-25T12:43:19.850 回答
0

也许Harpreet是对的,事实上我刚才也遇到了这种情况。

您可以只使用方法SmsManager.divideMessage(String text)让系统处理长度。

于 2013-08-31T16:12:43.707 回答
0

SmsManager.sendTextMessage() 方法还会在传入无效数字作为目标的情况下引发 NPE。当我传递一个格式化的电话号码(例如“+1 999 999 9999”)时,我第一手发现了这一点。当我去掉空字符并删除国家代码前的“+”时,该方法没有抛出 NPE。

于 2015-08-24T21:44:37.917 回答
0

@harpreet aswer 向我指出了错误的根源。

请注意,SMS 消息长度限制可能因您的移动网络运营商而异。

那么你有两个解决方案:

1 - 发送较短的消息。

-或者-

2 - 将它们分成几条消息(这可能会导致额外费用)

SmsManager sms = SmsManager.getDefault();
ArrayList<String> dividedText = sms.divideMessage(message);
for (String shortMessage : dividedText) {
    sms.sendTextMessage(phoneNumber,null,shortMessage,null,null);
    }
于 2015-10-21T16:09:39.467 回答