使用模式/匹配器方法对 ANDROID 的正则表达式感到疯狂。我已经好几天没有成功了。是的,在寻求直接帮助之前,我一直在寻求帮助。好的,这是交易:我的传入流看起来像这样
UTC 时间(HhMmSs):154653
状态(A=OK,V=KO):V
纬度:4428.2011
方向(N/S):N
经度:00440.5161
方向(E/W):W
速度(节):000.5
方向(度) : 342.8
UTC 日期 (DdMmAa): 050407
磁变化:
变化 (E/W):
模式: A
我现在想要的只是纬度和经度值。
我的代码如下所示:
public class NmeaParser {
/**
* Tag used for log messages
*/
public String nmeaData;
public String lat; //latitude
public String lon; //longitude
private static final String LOG_TAG = "NPGPS";
public String longitude, latitude;
//find the text pattern
public static final Pattern NMEA_PATTERN = Pattern.
compile(
"\\d{6}" +
":\\s\\D" +
"\\w+\\S\\s\\d{4}.\\d+" +
":\\s\\D" +
"\\w+\\S\\s\\d{5}.\\d+" +
":\\s\\D" +
"\\d{3}.\\d{1}" +
"\\d{3}.\\d{1}" +
"\\d{6}" +
":\\s\\D" +
":\\s\\D"
//".*" //this works as a test
);
public String parseNmeaSentence(String gpsSentence) {
Matcher rmcMatcher = NMEA_PATTERN.matcher(gpsSentence);
//Log.i(LOG_TAG, "dataNP: "+ rmcMatcher);
if (rmcMatcher.find()) {
//Log.d("Matcher", "PATTERN MATCHES!");
//else
//Log.d("MATCHER", "PATTERN DOES NOT MATCH!");
//String time = rmcMatcher.group(1);
latitude = rmcMatcher.group(3);
nmeaData = latitude;
}
return nmeaData;
}
}
我不断收到这些错误:
09-17 11:37:36.936: E/AndroidRuntime(8756): FATAL EXCEPTION: main
09-17 11:37:36.936: E/AndroidRuntime(8756): java.lang.NullPointerException
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.AbsListView.obtainView(AbsListView.java:2058)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.ListView.onMeasure(ListView.java:1155)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-17 11:37:36.936: E/AndroidRuntime(8756): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.os.Looper.loop(Looper.java:137)
09-17 11:37:36.936: E/AndroidRuntime(8756): at android.app.ActivityThread.main(ActivityThread.java:4575)
09-17 11:37:36.936: E/AndroidRuntime(8756): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 11:37:36.936: E/AndroidRuntime(8756): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 11:37:36.936: E/AndroidRuntime(8756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-17 11:37:36.936: E/AndroidRuntime(8756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-17 11:37:36.936: E/AndroidRuntime(8756): at dalvik.system.NativeStart.main(Native Method)
显然,我在模式/匹配器方法中搞砸了我的正则表达式语法。任何帮助都非常感谢。谢谢!