我的应用程序一直在崩溃onReceive基本上我发送gps坐标到另一个Android设备我试图在一段时间(真实)循环中从这些gps坐标集构建一个线方程,直到他将到达这一行,我使用while循环因为我需要等待,直到他会得到坐标来解决线的等式。
但它一直在崩溃。
那是我的OnReceive代码:
package com.example.yeah;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.android.gms.maps.GoogleMap;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsListener extends BroadcastReceiver {
GoogleMap googleMap;
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
//show first message
Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
String s=smsMessage[0].getMessageBody();
Matcher m = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(s);
double[] d = new double[1000] ;
int p=0;
while (m.find())
{
double k = Double.parseDouble(m.group(1));
d[p]=k;
p++;
}
int j,i=1,t,success=0;
double line;
double[] ship = new double[1000] ;
double[] b = new double[1000] ;
double lat;
double lng;
ship[0]=SlopeCalc(d[2],d[0],d[3],d[1]);
b[0]=d[0]-ship[0]*d[1];
if (p>3)
{
for(j=2;j<p;j++)
{
if(j+2<p){
ship[i]=SlopeCalc(d[j+2],d[j-2+2],d[j+1+2],d[j-1+2]);
b[i]=d[j]-(ship[i]*d[j+1]);
j++;
i++;
}
else{
break;
}
}
}
while(true)
{
Location lm = googleMap.getMyLocation();
lat=lm.getLatitude();
lng=lm.getLongitude();
for (t=0;t<i;t++){
line=ship[t]*lng+b[t]-lat;
if (line==0){
success=1;
break;
}
}
if (success==1){
break;
}
}
Toast poast = Toast.makeText(context, "I FOUND U", Toast.LENGTH_LONG);
poast.show();
abortBroadcast();
}
public static double SlopeCalc(double y2,double y1, double x2,double x1){
double sou;
sou=(y2-y1)/(x2-x1);
return sou;
}
}
那就是我的logcat:
05-09 21:44:24.165: E/Gsm/SmsMessage(2483): hasUserDataHeader : false
05-09 21:44:24.190: E/AndroidRuntime(2483): FATAL EXCEPTION: main
05-09 21:44:24.190: E/AndroidRuntime(2483): java.lang.RuntimeException: Unable to start receiver com.example.yeah.SmsListener: java.lang.NullPointerException
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.os.Looper.loop(Looper.java:130)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-09 21:44:24.190: E/AndroidRuntime(2483): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 21:44:24.190: E/AndroidRuntime(2483): at java.lang.reflect.Method.invoke(Method.java:507)
05-09 21:44:24.190: E/AndroidRuntime(2483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-09 21:44:24.190: E/AndroidRuntime(2483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-09 21:44:24.190: E/AndroidRuntime(2483): at dalvik.system.NativeStart.main(Native Method)
05-09 21:44:24.190: E/AndroidRuntime(2483): Caused by: java.lang.NullPointerException
05-09 21:44:24.190: E/AndroidRuntime(2483): at com.example.yeah.SmsListener.onReceive(SmsListener.java:90)
05-09 21:44:24.190: E/AndroidRuntime(2483): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)
05-09 21:44:24.190: E/AndroidRuntime(2483): ... 10 more
我猜它崩溃是因为while循环...需要你的帮助!
答案 0 :(得分:2)
您的googleMap对象为空。
您只是声明了
GoogleMap googleMap;
但从未实例化。那么当你尝试做的时候,
googleMap.getMyLocation();
你遇到Null Pointer Exception。
答案 1 :(得分:2)
它不是无限循环。它是第90行的NullPointerException。我在这里猜测:
Location lm = googleMap.getMyLocation();
在尝试调用其中一种方法之前,请务必为googleMap指定一个值。