开发者

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" encodin开发者_如何学JAVAg="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 is a layout, not an id. To do the animation in your layout you need an ImageView to set the AnimationDrawable as the background.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜