无法影响始终为null的简单Double []表的值-2个代码之间的比较 [英] Can't affect values to a simple Double[] table which is always null - Comparaison between 2 codes

查看:95
本文介绍了无法影响始终为null的简单Double []表的值-2个代码之间的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过类似的问题:

I've asked a similar question about it : Can't affect values to a simple Double[] table which is always null

我有一个包含NMEA帧的TXT文件.我检索$ GPGGA和$ GPRMC框架的纬度和经度.然后,我将经度和纬度转换为十进制度数.我的代码的第一个版本运行良好.然后我做了第二个,结构化了一些,但是没有用.

I have a TXT file containing NMEA frames. I retrieve the latitude and the longitude of $GPGGA and $GPRMC frames. Then I convert the latitude and longitude into Decimal Degrees. The first version of my code is working great. Then I made a second one, more structured, but which doesn't work.

这是我的代码的第二个版本,该版本不起作用:

/**
 * Updating position on Google Maps
 * @param values
 */
public void showMyPosition(String values)
{
    String[]selectedposition=values.split(",");
    Double[]decimalcoordinates=new Double[2];
    if(selectedposition.length>1 || selectedposition[0].equals("$GPGGA"))
    {
        int[]coordinatesposition=new int[]{2,4};
        decimalcoordinates = convertNmeaToDecimal(selectedposition, coordinatesposition);
    }
    else if(selectedposition.length>1 || selectedposition[0].equals("$GPRMC"))
    {
        int[]coordinatesposition=new int[]{3,5};
        decimalcoordinates = convertNmeaToDecimal(selectedposition, coordinatesposition);
    }

    LatLng position=new LatLng(decimalcoordinates[0],decimalcoordinates[1]);
    googlemap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
    marker=googlemap.addMarker(new MarkerOptions().title("You are here !").position(position));
    googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 19));
}   

/**
 * Convert NMEA coordinates into Decimal degrees coordinates.
 * @param nmeaframes 
 *      Contains all the NMEA frame
 * @param coordinatesposition
 *      Contains the position of the latitude and longitude into nmeaframes[]
 * @return decimalcoordinates*      
 */
public Double[] convertNmeaToDecimal(String[]nmeaframes, int[]coordinatesposition)
{
    Double nmeacoordinates=null;
    Double[]decimalcoordinates=new Double[2];

    for(int i=0; i<2; i++)
    {
        nmeacoordinates=Double.parseDouble(nmeaframes[coordinatesposition[i]]);
        decimalcoordinates[i]=Math.floor(nmeacoordinates/100)+(nmeacoordinates-Math.floor(nmeacoordinates/100)*100)/60;
    }
    return decimalcoordinates;
}

这是我的LogCat:

12-17 14:47:03.476: E/AndroidRuntime(6769): FATAL EXCEPTION: main
12-17 14:47:03.476: E/AndroidRuntime(6769): java.lang.NumberFormatException: Invalid double: "A"
12-17 14:47:03.476: E/AndroidRuntime(6769):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)

我认为自己的代码是如此,以至于我再也看不到自己的错误.这似乎与我的先例问题相同.但是我解决不了.

I think I'm so in my code, that I can no longer see my mistakes. It seems to be the same as my precedent question. But I can't solve it.

你能帮我吗?

编辑-这是一个非常粗心的错误...感谢Alex Walker帮助我指出我的错误

要提供原始功能,您需要将两个||运算符都更改为&&.这样,它将根据需要检查这两种情况.也就是说,使用:

To give the original functionality, you need to change both of those || operators to &&. That way it will check both cases as required. That is, use:

if(selectedposition.length>1 && selectedposition[0].equals("$GPGGA"))

else if(selectedposition.length>1 && selectedposition[0].equals("$GPRMC"))

推荐答案

这些代码示例非常长且非常相似,因此很难确定实际问题是什么.

These code samples are extremely long and very similar, so it's painfully difficult to pin down what the actual problem is.

但是,一个明显的错误确实出在版本2的这些行中:

However one obvious mistake that does stand out is in these lines from version 2:

if(selectedposition.length>1 || selectedposition[0].equals("$GPGGA"))

else if(selectedposition.length>1 || selectedposition[0].equals("$GPRMC"))

根据您的版本1代码,该算法应检查selectedposition.length>1,如果是,则尝试进行操作.如果不是,则什么也不要做.

According to your version 1 code, the algorithm should check that selectedposition.length>1, and if it is, then go and try to do stuff. If it isn't, don't do anything.

但是在上面列出的版本2代码中,该块

But in the version 2 code listed above, the block

else if(selectedposition.length>1 || selectedposition[0].equals("$GPRMC"))

除非输入正好是一个字符长,否则将不会调用

.

will NEVER be called unless the input is exactly one character long.

要提供原始功能,您需要将两个||运算符都更改为&&.这样,它将根据需要检查这两种情况.也就是说,使用:

To give the original functionality, you need to change both of those || operators to &&. That way it will check both cases as required. That is, use:

if(selectedposition.length>1 && selectedposition[0].equals("$GPGGA"))

else if(selectedposition.length>1 && selectedposition[0].equals("$GPRMC"))

这篇关于无法影响始终为null的简单Double []表的值-2个代码之间的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆