Genevieve
Genevieve

Reputation: 975

Android - Error if I don't import R and another error if I do?

If I don't import R, I get "id cannot be resolved or is not a field"

(R.id.mainanim);

And if I import R, the id error is gone, but I get "main and mainanim cannot be resolved or is not a field". They're my .xml files.

Here is my code... maybe there is an error I didn't see?

 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>    

Upvotes: 0

Views: 4045

Answers (2)

Chris Stewart
Chris Stewart

Reputation: 3303

Generally, when you have problems with R, there's something wrong in one of your XML layout files. R is unable to be generated and therefore any reference to it in your code will toss out an error.

Upvotes: 0

Robby Pond
Robby Pond

Reputation: 73484

Mainanim.xml is a layout, not an id. To do the animation in your layout you need an ImageView to set the AnimationDrawable as the background.

Upvotes: 2

Related Questions