您好我的问题很简单,我每次都会收到无效的浮动类型。
请检查我的代码段!
String v2 = OBJParser.v.toString();
String faces2 = OBJParser.faces.toString();
float vertices[] = { Float.valueOf(v2) };
/** The initial texture coordinates (u, v) */
float texture[] = { Float.valueOf(v2) };
/** The initial indices definition */
byte indices[] = { Byte.valueOf(faces2) };
public TDModel() {
//
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices); // <--- Invalid Float
vertexBuffer.position(0);
//
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture); // <--- Invalid Float
textureBuffer.position(0);
}
这是我的LogCat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.objLoader/com.example.objLoader.ObjLoaderApp}:
java.lang.NumberFormatException: Invalid float: "[6.083834, 0.0, 6.083834, -6.083834, 0.0, 6.083834, 6.083834, 0.0, -6.083834, -6.083834, 0.0, -6.083834, 0.89693, -0.116701, -1.078061, 0.736314, -0.076033, 1.066762, -1.052088, -0.0646, 0.954513, -0.692441, -0.114299, -0.835835, 1.303753, 1.083753, -1.023542, 0.444005, 1.032712, 1.513821, -1.019775, 1.17495, 1.105961, -0.681862, 1.180458, -1.481573, 1.045407, -0.157597, -0.147996, 0.067265, -0.072713, -0.783522, 1.200361, 0.538245, -1.221685, -0.154563, -0.1145, 1.228987, 1.155268, 0.478039, 1.381585, -1.094633, -0.254018, -0.118459, -1.64806, 0.452009, 0.853586, -1.185727, 0.481559, -1.133555, 0.975431, 1.246559, 0.047895, 0.105258, 1.12682, -1.585067, -0.344848, 1.15238, 1.73, -1.177093, 1.303243, 0.009798, -0.015789, -0.312706, -0.060242, 0.046107, 1.351565, -0.25373, 1.175182, 0.692469, 0.169032, 0.260077, 0.45264, 1.365451, -1.648881, 0.429819, -0.064031, -0.199824, 0.33682, -1.536424, 1.008866, -0.126459, 0.470257, -0.321458, -0.060305, -0.702046, 1.385825, 0.807576, -1.181814, -0.537873, -0.057997, 1.154378, 0.936321, 0.677546, 1.426494, -0.862364, -0.219203, -0.537108, -1.281588, 0.883991, 1.218077, -0.974527, 0.926074, -1.431442, 0.808457, 1.346412, 0.902078, -0.174992, 1.147173, -1.801163, -0.610285, 1.147948, 1.327881, -1.356023, 1.245036, -0.716849, 1.042022, -0.190494, -0.637866, 0.468949, -0.142859, -1.026806, 1.081272, 0.175115, -1.287556, 0.243586, -0.141476, 1.191072, 1.014587, 0.183491, 1.312948, -1.155731, -0.217847, 0.459469, -1.571341, 0.110279, 0.770709, -1.063276, 0.074521, -1.015607, 1.231954, 1.216866, -0.594471, 0.770454, 1.122192, -1.299703, -0.065279, 1.102928, 1.796044, -1.17643, 1.281231, 0.64638, -0.476225, -0.274043, -0.03739, 0.480423, -0.312115, -0.11106, 0.172793, -0.188473, -0.53246, -0.106274, -0.274351, 0.595963, -0.19801, 1.36038, 0.630818, 0.062998, 1.276304, -0.994314, 0.611501, 1.352344, -0.29078, -0.603343, 1.343584, -0.011276, 1.267461, 0.63073, 1.008356, 1.267107, 0.600208, -0.689727, 1.268899, 0.241091, -0.063884, 1.050912, 1.068503, 0.197512, -1.04357, 0.402928, 1.289593, 0.667029, 0.439932, 1.448684, -0.13512, 0.156677, 1.50502, -0.06687, 0.65206, 1.602193, -1.759897, 0.319614, -0.724614, -1.646483, 0.462612, 0.459024, -1.548443, -0.153801, -0.255587, -1.483055, 1.068517, 0.04533, -0.775458, 0.497559, -1.402503, 0.556317, 0.449266, -1.496503, 0.153226, 0.839249, -1.83223, -0.180515, 0.060946, -1.233899, -0.63092, -0.226328, 0.592326, -0.677921, 1.329703, 0.698183, 1.11593, 1.067893, 0.980967, -0.944509, 0.743625, 1.392342, -1.71944, 0.916506, -0.699776, -0.751048, 0.149059, -1.123006, -0.359688, -0.219676, -0.596748, 0.548058, -0.284113, -0.657622, 0.383454, -0.298149, 0.53317, 0.342781, 1.370886, 0.863692, 0.801564, 1.333314, -0.716665, -0.544178, 1.290048, -0.889409, 1.347667, 0.189763, 0.746821, 1.323792, 0.127379, -0.85606, 1.423543, 0.897206, -0.707035, -0.706694, 0.190636, 1.346937, 0.494274, 0.153436, 1.399456, 0.524189, 0.69141, 1.410453, -1.346437, -0.078697, -0.808084, -1.55038, 0.033513, 0.487642, -1.434319, 1.0171, 0.746778, -0.263198, 0.91678, -1.716774, 0.802037, 0.850318, -1.528586, 0.512712, 0.114694, -1.198383]"
编辑代码:
Float[] vertices = OBJParser.v.toArray(new Float[OBJParser.v.size()]);
/** The initial texture coordinates (u, v) */
Float[] texture = OBJParser.v.toArray(new Float[OBJParser.v.size()]);
/** The initial indices definition */
Float[] indices = OBJParser.faces.toArray(new Float[OBJParser.faces.size()]);
public TDModel() {
//
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
for (float verticesFloat : vertices){
vertexBuffer.put(verticesFloat);
}
vertexBuffer.position(0);
//
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
for (float textureFloat : texture){
textureBuffer.put(textureFloat);
}
textureBuffer.position(0);
}
相同的LogCat错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.objLoader/com.example.objLoader.ObjLoaderApp}: java.lang.NumberFormatException: Invalid float: "[1.0, 0.0, 2.0, 1.0, 2.0, 3.0, 78.0, 33.0, 6.0, 78.0, 6.0, 47.0, 79.0, 53.0, 10.0, 79.0, 10.0, 40.0, 80.0, 38.0, 9.0, 80.0, 9.0, 34.0, 81.0, 40.0, 10.0, 81.0, 10.0, 36.0, 82.0, 41.0, 11.0, 82.0, 11.0, 37.0, 83.0, 31.0, 7.0, 83.0, 7.0, 49.0, 84.0, 54.0, 17.0, 84.0, 17.0, 35.0, 85.0, 55.0, 24.0, 85.0, 24.0, 56.0, 86.0, 45.0, 15.0, 86.0, 15.0, 57.0, 87.0, 58.0, 22.0, 87.0, 22.0, 52.0, 88.0, 59.0, 25.0, 88.0, 25.0, 60.0, 89.0, 41.0, 23.0, 89.0, 23.0, 61.0, 90.0, 62.0, 16.0, 90.0, 16.0, 46.0, 91.0, 63.0, 26.0, 91.0, 26.0, 64.0, 92.0, 50.0, 20.0, 92.0, 20.0, 65.0, 93.0, 66.0, 18.0, 93.0, 18.0, 48.0, 94.0, 67.0, 27.0, 94.0, 27.0, 68.0, 95.0, 52.0, 22.0, 95.0, 22.0, 69.0, 96.0, 70.0, 19.0, 96.0, 19.0, 49.0, 97.0, 71.0, 28.0, 97.0, 28.0, 72.0, 98.0, 53.0, 23.0, 98.0, 23.0, 73.0, 99.0, 74.0, 19.0, 99.0, 19.0, 37.0, 100.0, 75.0, 29.0, 100.0, 29.0, 76.0, 101.0, 43.0, 13.0, 101.0, 13.0, 77.0, 54.0, 78.0, 47.0, 54.0, 47.0, 17.0, 24.0, 57.0, 78.0, 24.0, 78.0, 54.0, 57.0, 15.0, 33.0, 57.0, 33.0, 78.0, 58.0, 79.0, 40.0, 58.0, 40.0, 22.0, 25.0, 61.0, 79.0, 25.0, 79.0, 58.0, 61.0, 23.0, 53.0, 61.0, 53.0, 79.0, 62.0, 80.0, 34.0, 62.0, 34.0, 16.0, 26.0, 65.0, 80.0, 26.0, 80.0, 62.0, 65.0, 20.0, 38.0, 65.0, 38.0, 80.0, 66.0, 81.0, 36.0, 66.0, 36.0, 18.0, 27.0, 69.0, 81.0, 27.0, 81.0, 66.0, 69.0, 22.0, 40.0, 69.0, 40.0, 81.0, 70.0, 82.0, 37.0, 70.0, 37.0, 19.0, 28.0, 73.0, 82.0, 28.0, 82.0, 70.0, 73.0, 23.0, 41.0, 73.0, 41.0, 82.0, 74.0, 83.0, 49.0, 74.0, 49.0, 19.0, 29.0, 77.0, 83.0, 29.0, 83.0, 74.0, 77.0, 13.0, 31.0, 77.0, 31.0, 83.0, 31.0, 84.0, 35.0, 31.0, 35.0, 7.0, 13.0, 56.0, 84.0, 13.0, 84.0, 31.0, 56.0, 24.0, 54.0, 56.0, 54.0, 84.0, 43.0, 85.0, 56.0, 43.0, 56.0, 13.0, 4.0, 42.0, 85.0, 4.0, 85.0, 43.0, 42.0, 12.0, 55.0, 42.0, 55.0, 85.0, 55.0, 86.0, 57.0, 55.0, 57.0, 24.0, 12.0, 30.0, 86.0, 12.0, 86.0, 55.0, 30.0, 5.0, 45.0, 30.0, 45.0, 86.0, 38.0, 87.0, 52.0, 38.0, 52.0, 9.0, 20.0, 60.0, 87.0, 20.0, 87.0, 38.0, 60.0, 25.0, 58.0, 60.0, 58.0, 87.0, 50.0, 88.0, 60.0, 50.0, 60.0, 20.0, 8.0, 51.0, 88.0, 8.0, 88.0, 50.0, 51.0, 21.0, 59.0, 51.0, 59.0, 88.0, 59.0, 89.0, 61.0, 59.0, 61.0, 25.0, 21.0, 39.0, 89.0, 21.0, 89.0, 59.0, 39.0, 11.0, 41.0, 39.0, 41.0, 89.0, 30.0, 90.0, 46.0, 30.0, 46.0, 5.0, 12.0, 64.0, 90.0, 12.0, 90.0, 30.0, 64.0, 26.0, 62.0, 64.0, 62.0, 90.0, 42.0, 91.0, 64.0, 42.0, 64.0, 12.0, 4.0, 44.0, 91.0, 4.0, 91.0, 42.0, 44.0, 14.0, 63.0, 44.0, 63.0, 91.0, 63.0, 92.0, 65.0, 63.0, 65.0, 26.0, 14.0, 32.0, 92.0, 14.0, 92.0, 63.0, 32.0, 8.0, 50.0, 32.0, 50.0, 92.0, 33.0, 93.0, 48.0, 33.0, 48.0, 6.0, 15.0, 68.0, 93.0, 15.0, 93.0, 33.0, 68.0, 27.0, 66.0, 68.0, 66.0, 93.0, 45.0, 94.0, 68.0, 45.0, 68.0, 15.0, 5.0, 46.0, 94.0, 5.0, 94.0, 45.0, 46.0, 16.0, 67.0, 46.0, 67.0, 94.0, 67.0, 95.0, 69.0, 67.0, 69.0, 27.0, 16.0, 34.0, 95.0, 16.0, 95.0, 67.0, 34.0, 9.0, 52.0, 34.0, 52.0, 95.0, 35.0, 96.0, 49.0, 35.0, 49.0, 7.0, 17.0, 72.0, 96.0, 17.0, 96.0, 35.0, 72.0, 28.0, 70.0, 72.0, 70.0, 96.0, 47.0, 97.0, 72.0, 47.0, 72.0, 17.0, 6.0, 48.0, 97.0, 6.0, 97.0, 47.0, 48.0, 18.0, 71.0, 48.0, 71.0, 97.0, 71.0, 98.0, 73.0, 71.0, 73.0, 28.0, 18.0, 36.0, 98.0, 18.0, 98.0, 71.0, 36.0, 10.0, 53.0, 36.0, 53.0, 98.0, 39.0, 99.0, 37.0, 39.0, 37.0, 11.0, 21.0, 76.0, 99.0, 21.0, 99.0, 39.0, 76.0, 29.0, 74.0, 76.0, 74.0, 99.0, 51.0, 100.0, 76.0, 51.0, 76.0, 21.0, 8.0, 32.0, 100.0, 8.0, 100.0, 51.0, 32.0, 14.0, 75.0, 32.0, 75.0, 100.0, 75.0, 101.0, 77.0, 75.0, 77.0, 29.0, 14.0, 44.0, 101.0, 14.0, 101.0, 75.0, 44.0, 4.0, 43.0, 44.0, 43.0, 101.0]"
:)感谢您的帮助!我希望你会发现错误。 ;)
private void processVLine(String line){
String[] tokens=line.split("[ ]+"); //split the line at the spaces
int c=tokens.length;
for(int i=1; i<c; i++){ //add the vertex to the vertex array
v.add(Float.valueOf(tokens[i]));
}
}
在processVLine片段中的是我分配给他的错误!请帮帮我;)
答案 0 :(得分:0)
您的问题出现在这一行上(随后,在您初始化其他浮点数组的行上):
float vertices[] = { Float.valueOf(v2) };
原因是,v2是一个String,括号中的值,例如[1.0]
。因此,当您调用Float.valueOf()时,由于括号而失败。
以下是一个例子:
public class TestMe {
public static void main(String[] args) {
Vector<Float> v=new Vector<Float>();
v.add(Float.valueOf("1.0f"));
String v2 = v.toString();
System.out.println("v.toString() is " + v2);
// This is where it fails, because v2 is "[1.0]"
// float[] floats = {Float.valueOf(v2)};
Float[] floats = v.toArray(new Float[v.size()]);
for (Float f : floats){
System.out.println(f);
}
}
}
输出为:
v.toString() is [1.0]
1.0
如果要将浮点数存储在Vector中,请确保使用.toArray()
而不是.toString()
将其转换为浮点数组。