1

developers.android.com のこのチュートリアルを使用して、スピン アニメーションを作成しようとしています。

すべて問題ないと確信していますが、「アプリケーションが停止しました」と表示されます。

それが私の MainActivity です:

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 に答える