我正在尝试使用此tutorial from 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。
答案 0 :(得分:0)
它在MainActivity.java的第35行得到NullPointerException。所以我猜测正在发生以下事情之一。
找不到ImageView
,导致img.setBackgroundResource(R.drawable.spin_animation);
失败
或者它无法设置背景,因此当您拨打AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
或者动画播放失败导致null,因此当您调用frameAnimation.start();
答案 1 :(得分:-1)
替换
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
使用
AnimationDrawable frameAnimation = (AnimationDrawable) img.getDrawable();