我的应用程序不断崩溃的onReceive基本上即时发送GPS坐标到另一个Android设备和进出口试图从那些套GPS的建立线的方程式,直到他将得到这一行,进出口使用上一段时间(true)循环坐标而循环,因为我需要等待,直到他将得到坐标来解决直线的方程。
My app keeps crashing onReceive Basically im sending gps coordinates to another android device and Im trying to build an equation of line from those sets of gps coordinates on a while(true) loop untill he will get to this line, Im using the while loop because I need to wait untill he will get coordinates to solve the equation of line.
但它总是死机。
这就是我的onReceive code:
thats my OnReceive code :
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:
and thats my 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循环......需要你的帮助!
I guess its crashing because the while loop ... Need your help!
您的GoogleMap对象是空的。
Your googleMap object is null.
您只宣称它
GoogleMap googleMap;
但从来没有实例化。所以,当你试图去做的话,
but never instantiated. So then when you try to do,
googleMap.getMyLocation();
您遇到空指针异常。