gpt4 book ai didi

android - SmsManager 空指针异常

转载 作者:行者123 更新时间:2023-11-30 03:47:48 26 4
gpt4 key购买 nike

我正在尝试使用 smsManagerBroadCast 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)

最佳答案

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

关于android - SmsManager 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14674890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com