view.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)

转自: http://blog.csdn.net/brokge/article/details/8536906

setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息,在很多场合下都得到妙用。

setTag(Object tag)方法比较简单,这里主要谈一谈带两个参数的setTag方法。

官方的api文档中提到:“ The
specified key should be an id declared in the resources of the application to
ensure it is unique (see the ID resource
type
). Keys identified as belonging to the Android framework or not
associated with any package will cause an IllegalArgumentExceptionto
be thrown.”所以抛出IllegalArgumentException的原因就在于key不唯一,那么如何保证这种唯一性呢?定义一个final类型的int变量和硬编码一个值的方式都是行不通的。

privatestaticfinalint TAG_ONLINE_ID =1;
((Button) row.findViewById(R.id.btnPickContact)).setTag(TAG_ONLINE_ID,objContact.onlineid);
05-1820:29:38.044: ERROR/AndroidRuntime(5453): java.lang.IllegalArgumentException:The key must be an application-specific resource id.05-1820:29:38.044: ERROR/AndroidRuntime(5453):     at android.view.View.setTag(View.java:7704)05-1820:29:38.044: ERROR/AndroidRuntime(5453):     at com.mypkg.viewP.inflateRow(viewP.java:518)
如果只需要设置一个tag,那么直接调用setTag(Object tag)方法就可以轻松搞定,如果一定需要使用多个tag绑定,那么需要先在res/values/strings.xml中添加
<resources>
<item type="id" name="tag_first"></item>
<item type="id" name="tag_second"></item>
</resources>
使用的时候写成
imageView.setTag(R.id.tag_first,"Hello");
imageView.setTag(R.id.tag_second,"Success");
就可以了
取值的时候
String tag_first=v.getTag(R.id.tag_first).tostring();
就能取到值了!

view.setTag(key,Object) (java.lang.IllegalArgumentException: The
key must be an application-specific resource id.)

时间: 2024-10-11 06:14:46

view.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)的相关文章

java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting

将原有项目图片加载框架picasso改为glide,关于picasso和glide文档就自行查阅相关资料 显示 图片 例子 Glide.with(mContext).load(imageUrl).placeholder(defaultDrawable) .error(defaultDrawable).dontAnimate().into(view) 显示正常 因为项目中头像是圆形利用glide实用圆头像代码如下 Glide.with(mContext) .load(imageUrl) .dont

关于dialog引起的 java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager 错误的分析

在跑Monkey测试的时候出现了一个比较特别的问题,先来看看Log: 1 // CRASH: com.meizu.media.painter (pid 12491) 2 3 // Short Msg: java.lang.IllegalArgumentException 4 5 // Long Msg: java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{2

BottomSheetBehavior 之 java.lang.IllegalArgumentException: The view is not associated with BottomSheetBehavior

09-14 22:52:58.891 13674-13674/me.chunsheng.uberdriver E/AndroidRuntime: FATAL EXCEPTION: main Process: me.chunsheng.uberdriver, PID: 13674 java.lang.RuntimeException: Unable to start activity ComponentInfo{me.chunsheng.uberdriver/me.chunsheng.uberdr

Android java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be a non-static inn

AsyncTaskLoader: http://developer.Android.com/intl/zh-CN/reference/android/content/AsyncTaskLoader.html 错误信息: java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be a non-static inner member class: DownloadTask 原因: public

decorview that was originally added here or java.lang.IllegalArgumentException: View not attached to window manager

使用Dialog的时候,没少出现下面这两个报错 12-11 17:47:49.776: E/WindowManager(11461): android.view.WindowLeaked: Activity TestActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42204800 V.E..... R.....I. 0,0-640,164} that was originally

android——fragment长时间home或者锁屏java.lang.IllegalArgumentException:No view found for id for....

在项目中遇到了这个问题.头痛了很久,总是无法重现,也不能很好的解决.总是在息屏后一段时间,就会报java.lang.IllegalArgumentException:No view found for id for....的问题,然后程序就崩溃了.相信很多朋友也有和我一样遇到这个问题的.那么这个问题怎么去解决呢? 在仔细研究fragment的manager后,下面是我发现的东西 什么鬼?怎么会有两个fragmentManager.这两个东西有什么关系呢? API上面说了一大堆,我这里简单总结一下

selenium 使用键盘时 提示java.lang.IllegalArgumentException: Key Down / Up events only make sense for modifier keys.

输入某个内容后,使用enter键进行确认,最开始使用方式为: driver.findElement(By.xpath("//input[@name='supplier_name'][@id='js_productSupplier']")).sendKeys("输入内容"); Actions action=new Actions(driver); action.keyDown(Keys.ENTER).perform(); 运行时,提示java.lang.Illegal

java.lang.IllegalArgumentException: View not attached to window manager

公司项目线上bug: java.lang.IllegalArgumentException: View not attached to window manager at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:406) at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:308) at androi

java.lang.IllegalArgumentException: parameter must be a descendant of this view

异常信息: java.lang.IllegalArgumentException: parameter must be a descendant of this view 错误原因: 发生这个错误主要是ListView或者其它ViewGroup等容器控件因为滑动而暂时移除子View,但却没有移除该子View上面的焦点Focus,所以在ListView滑动返回到原来的位置的时候没有恢复成原来的View,导致了该异常的产生,具体的源码分析可以查看参考链接[2] 解决方法: 解决办法就是清除掉List