views:

91

answers:

2

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! :0 Can you help me?

Also here is ALL my code... maybe there is an error I didn't see?

JAVA:

 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>    

Please help. :(

+1  A: 

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.

BrennaSoft
What would be an id?
Mia
An id would be something like TextView in your Main.xml. It's an object inside the xml file.However, in order to 'get' the id, you would have to set it, so your TextView in xml would have <TextView android:id="@+id/textview01" android:layout_width="fill_parent" ...and then you could reference that textview in your code like this:TextView tw = (TextView) findViewById(R.id.textview01);
Aurora
A: 

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.

Chris Stewart