[英]Cannot cast from Object to int in eclipse
在我的Android應用程序中,在下面的行中:
int add= (int) arg0.getTag();
出現錯誤:
Cannot cast from Object to int
當我更換int
到integer
低於線得到另一個錯誤:
sp = (Spinner) findViewById(add);
錯誤是:
The method findViewById(int) in the type Activity is not applicable for the arguments (R.integer)
請建議我,希望得到答復。
謝謝
無法從Object轉換為Int
因為當前強制轉換getTag()
響應,該響應將Object返回給int
使用Integer.parseInt
將字符串解析為Integer:
int add= Integer.parseInt(arg0.getTag().toString());
如果使用setTag
傳遞了emply或int無效的int字符串,還將解析行包裝在try-catch
塊中以處理NumberFormatException
要從對象獲取int值,您可以執行以下操作-
int i = ((Integer) arg0.getTag()).intValue();
並且在Android中,您不能使用普通整數作為參數來查找ViewById()。 為了掌握對班級微調器的引用,您可能已經定義了以下內容-
Layout.xml
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
在您的.java中
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
有關更多信息,請參閱Checkout Android微調器指南
嘗試使用這種方法? findViewByTag();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.