---
title: "android 错误解决方法 ---积累中"
author: "uuFlower"
date: "2013-07-18T10:19:08Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2013-07-18T15:11:07Z"
id: 1909067
url: "https://uuflower.iteye.com/blog/1909067"
---

# **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/](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](http://blog.csdn.net/qeqeqe236/article/details/8919597)

 

这个是从网上找到的，没有具体实践，由于更换adt可能会引起其他问题， 考虑项目开发进度环境暂时不敢动了，有条件的可以尝试下。

 

下图是我工程的文件结构：

   
![](http://dl2.iteye.com/upload/attachment/0087/1135/87ab6b41-027b-3a22-92c7-74d624a1b121.bmp)