【问题标题】:Can't affect values to a simple Double[] table which is always null - Comparaison between 2 codes不能影响始终为空的简单 Double[] 表的值 - 两个代码之间的比较
【发布时间】:2013-12-17 14:09:55
【问题描述】:

我问过类似的问题:Can't affect values to a simple Double[] table which is always null

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

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

/**
 * 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)

我认为我的代码如此深入,以至于我无法再看到自己的错误。这似乎与我之前的问题相同。但是我解决不了。

你能帮帮我吗?

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

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

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

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

【问题讨论】:

  • 在 codereview.stackexchange.com 上会更好吗?
  • @TimB 不,不会。代码审查是审查代码(改进代码风格和东西),而不是了解错误来自哪里。

标签: java gps nmea


【解决方案1】:

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

然而,一个明显的错误是在版本 2 中的这些行中:

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

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

根据您的版本 1 代码,算法应该检查 selectedposition.length&gt;1,如果是,则去尝试做一些事情。如果不是,请不要做任何事情。

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

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

除非输入正好是一个字符,否则永远不会被调用。

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

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

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

【讨论】:

  • Omg...我现在觉得自己很愚蠢...这个小小的疏忽是我的应用程序崩溃的原因。真的非常感谢@AlexWalker。
  • 完全没问题,长函数很容易出错!
猜你喜欢
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
相关资源
最近更新 更多