首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 异常解决办法 -积累中

2013-07-20 
android 错误解决方法 ---积累中1 1.1 问题描述去除代码activity代码页面顶部中的 import android.R这句

android 错误解决方法 ---积累中
1 1.1 问题描述

去除代码activity代码页面顶部中的 import android.R;这句
就可以消除红色波浪线的main cannot be resolved or is not a field类似这个错误了

?

<?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: main07-18 06:46:11.661: E/AndroidRuntime(3549): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException07-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.NullPointerException07-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 异常解决办法 -积累中
?

?

?

?

?