1

我正在尝试使用developers.android.com的本教程制作旋转动画

我很确定,一切正常,但似乎“应用程序已停止”。

那是我的主要活动:

public class MainActivity extends Activity {




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    TextView txt = (TextView) findViewById(R.id.diavlo_book);  
    Typeface font = Typeface.createFromAsset(getAssets(), "DiavloBook-Regular.ttf");  
    txt.setTypeface(font);  

    TextView txt2 = (TextView) findViewById(R.id.diavlo_book_name);  
    Typeface font2 = Typeface.createFromAsset(getAssets(), "DiavloBook-Regular.ttf");  
    txt2.setTypeface(font2);

 // Load the ImageView that will host the animation and
    // set its background to our AnimationDrawable XML resource.
    ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
    img.setBackgroundResource(R.drawable.spin_animation);

    // Get the background, which has been compiled to an AnimationDrawable object.
    AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

    // Start the animation (looped playback by default).
    frameAnimation.start();

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      public void run() {
        //Do something after 100ms
          Intent intent = new Intent(MainActivity.this, SecondActivity.class);

        startActivity(intent);
       finish(); 
      }
    }, 5000);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

还有 spin_animation.xml:

<animation_list android:id="@+id/spinning_wheel_image" xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/logo_animation01" android:duration="50" />
<item android:drawable="@drawable/logo_animation02" android:duration="50" />
<item android:drawable="@drawable/logo_animation03" android:duration="50" />
<item android:drawable="@drawable/logo_animation04" android:duration="50" />
<item android:drawable="@drawable/logo_animation05" android:duration="50" />
<item android:drawable="@drawable/logo_animation06" android:duration="50" />
<item android:drawable="@drawable/logo_animation07" android:duration="50" />
<item android:drawable="@drawable/logo_animation08" android:duration="50" />
<item android:drawable="@drawable/logo_animation09" android:duration="50" />
<item android:drawable="@drawable/logo_animation10" android:duration="50" />
<item android:drawable="@drawable/logo_animation11" android:duration="50" />
<item android:drawable="@drawable/logo_animation12" android:duration="50" />
<item android:drawable="@drawable/logo_animation13" android:duration="50" />
<item android:drawable="@drawable/logo_animation14" android:duration="50" />
<item android:drawable="@drawable/logo_animation15" android:duration="50" />
<item android:drawable="@drawable/logo_animation16" android:duration="50" />
<item android:drawable="@drawable/logo_animation17" android:duration="50" />
<item android:drawable="@drawable/logo_animation18" android:duration="50" />
<item android:drawable="@drawable/logo_animation19" android:duration="50" />
<item android:drawable="@drawable/logo_animation20" android:duration="50" />
<item android:drawable="@drawable/logo_animation21" android:duration="50" />
<item android:drawable="@drawable/logo_animation22" android:duration="50" />
<item android:drawable="@drawable/logo_animation23" android:duration="50" />
<item android:drawable="@drawable/logo_animation24" android:duration="50" />
<item android:drawable="@drawable/logo_animation25" android:duration="50" />

日志:

09-21 19:10:42.244: E/Trace(662): error opening trace file: No such file or directory (2)
09-21 19:10:42.764: D/dalvikvm(662): GC_FOR_ALLOC freed 35K, 4% free 8006K/8259K, paused 52ms, total 54ms
09-21 19:10:42.774: I/dalvikvm-heap(662): Grow heap (frag case) to 8.821MB for 1000016-byte allocation
09-21 19:10:42.874: D/dalvikvm(662): GC_CONCURRENT freed <1K, 4% free 8982K/9287K, paused 31ms+17ms, total 93ms
09-21 19:10:42.965: D/dalvikvm(662): GC_FOR_ALLOC freed 0K, 4% free 8982K/9287K, paused 46ms, total 46ms
09-21 19:10:42.994: I/dalvikvm-heap(662): Grow heap (frag case) to 10.966MB for 2250016-byte allocation
09-21 19:10:43.104: D/dalvikvm(662): GC_CONCURRENT freed 0K, 4% free 11179K/11527K, paused 39ms+15ms, total 112ms
09-21 19:10:43.174: D/AndroidRuntime(662): Shutting down VM
09-21 19:10:43.174: W/dalvikvm(662): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-21 19:10:43.194: E/AndroidRuntime(662): FATAL EXCEPTION: main
09-21 19:10:43.194: E/AndroidRuntime(662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.czolgi.ii.ws/com.example.czolgi.ii.ws.MainActivity}: java.lang.NullPointerException
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.os.Looper.loop(Looper.java:137)
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-21 19:10:43.194: E/AndroidRuntime(662):  at java.lang.reflect.Method.invokeNative(Native Method)
09-21 19:10:43.194: E/AndroidRuntime(662):  at java.lang.reflect.Method.invoke(Method.java:511)
09-21 19:10:43.194: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-21 19:10:43.194: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-21 19:10:43.194: E/AndroidRuntime(662):  at dalvik.system.NativeStart.main(Native Method)
09-21 19:10:43.194: E/AndroidRuntime(662): Caused by: java.lang.NullPointerException
09-21 19:10:43.194: E/AndroidRuntime(662):  at com.example.czolgi.ii.ws.MainActivity.onCreate(MainActivity.java:35)
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.app.Activity.performCreate(Activity.java:5008)
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-21 19:10:43.194: E/AndroidRuntime(662):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-21 19:10:43.194: E/AndroidRuntime(662):  ... 11 more

它出什么问题了?我无法解决它:(我的所有图像“logo_animation**”也在drawable和spin_animation.xml。

4

2 回答 2

0

它在 MainActivity.java 的第 35 行收到 NullPointerException。所以我猜下面的事情之一正在发生。

  1. 它没有找到ImageView所以img.setBackgroundResource(R.drawable.spin_animation);失败了

  2. 或者它没有设置背景,所以当你打电话时它崩溃了AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

  3. 或者动画铸造失败导致 null 所以当你打电话时它会崩溃frameAnimation.start();

于 2012-09-21T19:35:30.420 回答
-1

代替

AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

AnimationDrawable frameAnimation = (AnimationDrawable) img.getDrawable();
于 2014-04-18T11:55:00.260 回答