java - Android - 如果我不导入 R 会出现错误,如果导入则会出现另一个错误?

标签 java android eclipse sdk

如果我不导入 R,我会收到“id 无法解析或不是字段”

(R.id.mainanim);

如果我导入 R,id 错误就会消失,但我得到“main 和 mainanim 无法解析或不是字段”。它们是我的 .xml 文件。

这是我的代码...也许有一个我没有看到的错误?

 package com.example.carrottest2;

 import android.app.Activity;
 import android.graphics.drawable.AnimationDrawable;
 import android.os.Bundle;
 import android.widget.ImageView;
 import android.R;

public class Carrottest2 extends Activity {
/** Called when the activity is first created. */

AnimationDrawable mainanimation; 

public void onCreate(Bundle icicle) {  
     super.onCreate(icicle);  
     setContentView(R.layout.main); 

     ImageView mainimage = (ImageView) findViewById(R.id.mainanim); 
     mainimage.setBackgroundResource(R.anim.mainanim); 
     mainanimation = (AnimationDrawable) mainimage.getBackground(); 

Main.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>  
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
</LinearLayout>

Mainanim.xml:

<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false"> 
<item android:drawable="@drawable/carrotsmile" android:duration="2000" /> 
<item android:drawable="@drawable/carrotblink" android:duration="2000" /> 
</animation-list>    

最佳答案

Mainanim.xml 是布局,而不是 id。要在布局中执行动画,您需要一个 ImageView 将 AnimationDrawable 设置为背景。

关于java - Android - 如果我不导入 R 会出现错误,如果导入则会出现另一个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3309382/

相关文章:

android - Gradle 同步失败 : Cause: https://downloads. gradle.org/distributions/gradle-3.1.2-all.zip

java - 错误 : 'cannot find symbol class GetTokenResult' after adding latest firebase-messaging dependency

Android相当于iCloud Key/Value Storage

java - 无法使用 html 发送超链接 key

java - IDE 的 Maven 支持比带有 m2e 的 Eclipse 更好吗?

java - 使用 Apache Ant 构建 PhoneGap 应用程序

java - 添加背景图片时 : Error inflating class android. widget.ImageButton

抛出 javax.imageio.IIOException : Can't read input file! ,但我不知道为什么会这样?

Java Swing : Do something while the key is being pressed

java - 将数据从一个表插入到另一个表时出错