SmsManager空指针异常

时间:2013-02-03 17:01:37

标签: android smsmanager

我正在尝试使用BroadCast Receiver中的 smsManager 发送短信。我收到此错误说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);

字符串消息不为空。它以吐司形式显示。 如果我用

替换消息
sp.getString("auto_text","")+" "+temp.getLatitude()+" "+temp.getLongitude();

然后它工作正常。

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)

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

@harpreet aswer指出我错误的来源。

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

然后你有两个解决方案:

1 - 发送更短的消息。

-OR -

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

    private static void PerformFileTrim(string filename)
    {
        var fileSize = (new System.IO.FileInfo(filename)).Length;

        if (fileSize > 5000000)
        {
            var text = File.ReadAllText(filename);
            var amountToCull = (int)(text.Length * 0.33);
            amountToCull = text.IndexOf('\n', amountToCull);
            var trimmedText = text.Substring(amountToCull + 1);
            File.WriteAllText(filename, trimmedText);
        }
    }