2012-09-21 24 views
0

我試圖用這個tutorial from 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。

+1

什麼是MainActivity.java:35 –

+0

可能是img.setBackgroundResource(R.drawable.spin_animation); –

回答

0

它在MainActivity.java第35行得到一個NullPointerException。所以我猜測一個下面的事情正在發生。

  1. 它未能找到ImageView所以img.setBackgroundResource(R.drawable.spin_animation);失敗

  2. 或者不設置背景,因此它崩潰,當你調用AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

  3. 或者動畫鑄造失敗導致空,因此它崩潰了,當你調用frameAnimation.start();

-1

更換

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

隨着

AnimationDrawable frameAnimation = (AnimationDrawable) img.getDrawable(); 
相關問題