imageview fadeineffect无法正常工作

时间:2011-12-16 03:35:57

标签: android animation nullpointerexception fadein

我一直在Android上练习一些动画,但我无法让我的代码工作。应用程序在启动时失败。这是我的主要课程+课堂上的淡出,

你可以帮我找出问题吗?

 package send.Shift;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class ShiftsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        ImageView Arrow = (ImageView) findViewById(R.drawable.arrow);
        Animation fadeInAnimation = AnimationUtils.loadAnimation(this,
                R.anim.fade_in);
        // Now Set your animation
        Arrow.startAnimation(fadeInAnimation);
    }



} 

XML:

<?xml version="1.0" encoding="utf-8"?>

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="500" />

logcat的:

   12-16 03:40:23.863: D/AndroidRuntime(707): Shutting down VM

   12-16 03:40:23.863: W/dalvikvm(707): threadid=1: thread exiting with uncaught exception (group=0x40015560)
   12-16 03:40:23.873: E/AndroidRuntime(707): FATAL EXCEPTION: main
   12-16 03:40:23.873: E/AndroidRuntime(707): java.lang.RuntimeException: Unable to start activity ComponentInfo{send.Shift/send.Shift.ShiftsActivity}: java.lang.NullPointerException
   12-16 03:40:23.873: E/AndroidRuntime(707):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at android.os.Handler.dispatchMessage(Handler.java:99)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at android.os.Looper.loop(Looper.java:123)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at android.app.ActivityThread.main(ActivityThread.java:3683)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at java.lang.reflect.Method.invokeNative(Native Method)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at java.lang.reflect.Method.invoke(Method.java:507)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   12-16 03:40:23.873: E/AndroidRuntime(707):   at dalvik.system.NativeStart.main(Native Method)
   12-16 03:40:23.873: E/AndroidRuntime(707): Caused by: java.lang.NullPointerException
   12-16 03:40:23.873: E/AndroidRuntime(707):   at send.Shift.ShiftsActivity.onCreate(ShiftsActivity.java:21)
  12-16 03:40:23.873: E/AndroidRuntime(707):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  12-16 03:40:23.873: E/AndroidRuntime(707):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
  12-16 03:40:23.873: E/AndroidRuntime(707):    ... 11 more

2 个答案:

答案 0 :(得分:1)

R.drawable.arrow不能是imageview的id。这是一个可绘制的,就像你在drawable文件夹中保存(png或jpg)的一些图片一样。你需要的是像R.id.my_image_view

答案 1 :(得分:0)

试试这个,

 ImageView imageView= (ImageView)findViewById(R.id.imageView);
 Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_in_anim);
  //Now Set your animation
 imageView.startAnimation(fadeInAnimation );