1 在android编程中出现cannot be resolved or is not a field
1.1 问题描述
在自定义属性时,在自定义View中获取属性时,下语句R下面是红色的波浪线,
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomGifView);
且总是提示:
cannot be resolved or is not a field
1.2 解决办法
看了好久都没头绪,看网上一兄弟提供如下解决方法,特总结记录一下,希望下次遇见能顺利解决:
去除代码activity代码页面顶部中的 import android.R;这句
就可以消除红色波浪线的main cannot be resolved or is not a field类似这个错误了
2 自定义属性编译时(xml),提示“error: No resource identifier found for attribute 'xxxxt' in package”(SDK为4.2.2)
2.1 问题描述
为了显示动态的gif动画,继承View自己定义控件,自定义属性gif关联gif资源,但是在布局xml里面使用自定义属性时,提示:
error: No resource identifier found for attribute 'gif' in package 'com.example.androidtest.viewunit'
2.2 解决方法一
网上有人说,在使用自定义属性时,可以把自定义空间名称的冒号去掉,例如:
自定义属性(attrs.xml):
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomGifView"> <attr name="gif" format="reference" /> </declare-styleable> </resources>
布局文件中的使用:
<!--xmlns:customgifview 为自定义属性空间 --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:customgifview="http://schemas.android.com/apk/res/com.example.androidtest.viewunit" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tvInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/gif_movie_info"/> <!--customgifview:gif 使用自定义的属性 --> <com.example.androidtest.CustomGifView android:id="@+id/ctGifView" android:layout_width="100px" android:layout_height="120px" android:layout_gravity="center_horizontal" android:layout_below="@id/tvInfo" customgifview:gif="@drawable/black_dance"/> </RelativeLayout >
根据网友的提示,去掉引用自定义属性的冒号后,上述编译错误可以消除:
customgifview:gif="@drawable/black_dance" ==》改为: customgifviewgif="@drawable/black_dance"/
但是,运行时,发现自定义的gif图片并没有显示。
发现自定义控件在构造函数中,不能获取自定义属性的srcId (为0),所以才加载不了gif,
下面是自定义View类的构造函数:
/* 我这里是继承View自定义一gifView用于显示gif动画 */ public CustomGifView(Context context, AttributeSet attrs) { super(context, attrs); // 获取控件属性 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomGifView); // 获取自定义属性 int srcId = a.getResourceId(R.styleable.CustomGifView_gif, 0); Log.d(TAG, "srcId = " + srcId); if (srcId > 0) { // 解码gif mMovie = Movie.decodeStream(getResources().openRawResource(srcId)); } // 回收资源 a.recycle(); }
且,启动时,Logcat输出如下错误(应用程序不会崩掉):
07-18 06:46:11.661: E/AndroidRuntime(3549): FATAL EXCEPTION: main 07-18 06:46:11.661: E/AndroidRuntime(3549): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.app.LoadedApk.makeApplication(LoadedApk.java:504) 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364) 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.app.ActivityThread.access$1300(ActivityThread.java:141) 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294) 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.os.Handler.dispatchMessage(Handler.java:99) 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.os.Looper.loop(Looper.java:137) 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.app.ActivityThread.main(ActivityThread.java:5041) 07-18 06:46:11.661: E/AndroidRuntime(3549): at java.lang.reflect.Method.invokeNative(Native Method) 07-18 06:46:11.661: E/AndroidRuntime(3549): at java.lang.reflect.Method.invoke(Method.java:511) 07-18 06:46:11.661: E/AndroidRuntime(3549): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-18 06:46:11.661: E/AndroidRuntime(3549): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-18 06:46:11.661: E/AndroidRuntime(3549): at dalvik.system.NativeStart.main(Native Method) 07-18 06:46:11.661: E/AndroidRuntime(3549): Caused by: java.lang.NullPointerException 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379) 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322) 07-18 06:46:11.661: E/AndroidRuntime(3549): at android.app.LoadedApk.makeApplication(LoadedApk.java:496) 07-18 06:46:11.661: E/AndroidRuntime(3549): ... 11 more
猜测有可能与自定义控件的路径有关系,尝试将自定义控件放在Menifest中package标签指定的路径下:
package="com.example.androidtest"
同时,修改自定义属性空间路径:
<!-- 属性空间 --> xmlns:customgifview="http://schemas.android.com/apk/res/com.example.androidtest" <!-- 属性的使用 --> customgifview:gif="@drawable/black_dance"
clean后,重新编译运行,上面的异常信息没了,gif也能正常播放了~~
但是, 为什么这样修改的原因不明,只是瞎猫碰个死耗子,有知道的兄弟跟俺说下哈,先谢过了~
2.3 解决方法二
升级ADT。
具体内容如下:
使用控件的自定义属性,使用时填写命名空间。
我按照老样子:http://schemas.android.com/apk/控件地址
在编译的时候报错error: No resource identifier found for attribute 'xxxxt' in package
解决:代码看了n遍也没找出问题,最后一个老外回答对了问题。ADT升级以后,自定义控件的命名空间的路径访问给优化了改成
http://schemas.android.com/apk/res-auto 这样填充之后ADT会自动去相应的包路径下寻找
参考网址:
http://blog.csdn.net/qeqeqe236/article/details/8919597
这个是从网上找到的,没有具体实践,由于更换adt可能会引起其他问题, 考虑项目开发进度环境暂时不敢动了,有条件的可以尝试下。
下图是我工程的文件结构:
相关推荐
android-support-v4-v7-v13-v14-v17(官方最新完整版),官方最新版的,压缩包内包含android-support-v4、android-support-v7-appcompat,android-support-v7-cardview,android-support-v7-gridlayout,android-support-...
Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明 Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明 Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明
内容包含:android-support-v4_1.6.0_26_20120316.jar android-support-v4_1.6.0_26_20120623.jar android-support-v4_1.6.0_26_20120730.jar android-support-v4_1.6.0_26_20121109.jar android-support-v4_1.6.0_26...
最新android studio 2015/10/12日更新 2015/10/12 android-studio-bundle-141.2288178-windows
•Android---UI篇---Tab Layout(选项卡布局) • •Andorid---UI篇---TableLayout(表格布局) • •Android---UI篇---RelativeLayout(相对布局) • •Android---UI篇---GridView(网格布局) • •Android---UI篇-...
ksoap2-android-assembly-2.5.4-jar-with-dependencies - withTimeOut.jar 自己下载的2.5.4版本有webservice的timeout设置,可是没有效果, 经反编译发现只是设置了timeout的空方法。 故根据网上的方法将方法完善。 ...
android-sdk-windowsandroid-sdk-windowsandroid-sdk-windowsandroid-sdk-windowsandroid-sdk-windowsandroid-sdk-windowsandroid-sdk-windows
Android自定义控件---联系人列表A-Z排序 此demo的博客地址:http://blog.csdn.net/u012814441
ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar 要是需要最新的,下载地址: http://code.google.com/p/ksoap2-android/
Android开发-Volley-解析Json使用方法-完整Demo-AndroidStudio http://blog.csdn.net/iwanghang/article/details/52241233
修改后的jar包,原始的包,打包签名的时候会出现错误,因为里边有可系统自带的类重复的,这个已经删除了。。如果你只是用来连接webservice的话就直接用可以了,代码混淆里也不需要多余的操作。 如果你用到了...
将android-18解压到Android\android-sdk\platforms下。
将android-17.zip解压到 Android\android-sdk\platforms 文件夹下。
解决eclipse无法找到ContextCompat.checkSelfPermission,最新的android-support-v4.jar库文件,亲测可用。
Android x86 即运行于 x86 PC上的Android操作系统,目前已经支持大部分安卓程序。Android X86平台是由Beyounn和Cwhuang主持设计的。项目的主要目的在于为X86平台...资源来源:https://www.fosshub.com/Android-x86.html
安卓开发之串口通信android-serialport-api-master,虽然是从Github上面下载下来的,但是从下载到使用还是有很多问题,其中之一就是由于该api是今年前开发出来的,sdk版本过低导致现在的eclipse不能直接使用,关于此...
用android编写的便捷备忘录,能实现增,删,改,查的功能。有时间提示,里面有apk文件可以down到手机上面直接运行,还有就是android studio项目,直接导入可以使用
asmack-android-8-4.0.7jar 2015-02-20更新,现在asmack已经托管到github上,费了很长时间才找到,在这里分享给大家
android-support-v7-preference.jar
Android即时通讯--仿QQ即时聊天源码(含服务器),能够实现多客户端的即时聊天功能,即时通讯的基本原理讲解,注释相信,博客地址:http://blog.csdn.net/qq_20889581/article/details/50755449