2015-05-08 74 views
2

我正在开发一个基于Eclipse的旧项目,并在Android Studio中打开它。最初使用1.7版本构建,现在我已经在1.8版本上,尝试1.7但没有成功。在迁移到Android Studio时投射

我的主要问题是,我得到了3个以下错误:

float size = (float) Float.parseFloat(v.getTag(R.string.size)); 

Error:

Error: Error:(228, 71) java: incompatible types: java.lang.Object cannot be converted to java.lang.String 

代码: INT POS =(INT)v.getTag(R.string.pos);

Error: Error:(229, 49) java: incompatible types: java.lang.Object cannot be converted to int 

Code:

int parentpos = (int) v.getTag(R.string.parentpos); 

Error:

Error: Error:(234, 63) java: incompatible types: java.lang.Object cannot be converted to int 

我知道它正试图将对象转换为整数。我尝试了几种可能的解决方案,但没有运气。代码中的任何可能的变化,我不明白的事情?

谢谢!

回答

2

v.getTag(R.string.size)返回Object。它可以是任何对象。您需要明确地将其转换为字符串

float size = (float) Float.parseFloat((String)(v.getTag(R.string.size))); 

第二和第三看起来很奇怪。铸造Objectint,应先造成Integer,然后再铸造对象上的intValue()

顺便说,Android的支持java的1.8

+0

的第一个和第三个解决,但第二个不是。我不知道是否有什么不对,但不应该,因为这个应用程序是大约两年前由我和一个朋友开发的,当时似乎没有问题。 –

+0

但第二个等于第三个。它不会发送,是吗? – Blackbelt

0

为了帮助你,代码的完全块,与第一和第三取代现在是:

public void onClick(View v) { 
     String type = (String) v.getTag(R.string.type); 
     float size = (float) Float.parseFloat((String)(v.getTag(R.string.size))); 
     int pos = (int) v.getTag(R.string.pos); 
     if (type.equals("white")) { 
      // Play sound! 
      Log.d("DEBUG", "White note, Size: " + size + ", Position (left to right): " + pos); 
     } else if (type.equals("black")) { 
      int parentpos = Integer.parseInt(v.getTag(R.string.parentpos).toString()); 
      // Play sound! 
      Log.d("DEBUG", "Black note, Size: " + size + ", Parent Position (left to right): " + parentpos + ", Position (top to bottom): " + pos); 

     } 
    } 
+0

虽然检测到第二个错误。 –

相关问题