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