2013-08-17 2 views
1

У меня есть объект AnimationDrawable, когда я вызываю метод wavingAndroid, игра вылетает. Любая помощь? Вот мой MainActivity.javaМое приложение падает при вызове моей анимации?

private void wavingAndroid(){ 
ImageView animateAndroid = (ImageView) findViewById(R.id.dancingAndroidLogo); 
animateAndroid.setImageResource(R.drawable.movingandroid); 
AnimationDrawable androidAnimation = (AnimationDrawable) animateAndroid.getDrawable(); 
androidAnimation.start(); 
    } 

и я пытаюсь вызвать его здесь:

public void onClick(View v){ 
    wavingAndroid(); 
    } 

Вот мой XML-файл:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:oneshot="true" > 

<item 
    android:drawable="@drawable/dancinglogo01" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo02" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo03" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo04" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo05" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo06" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo07" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo08" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo09" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo10" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo11" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo12" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo13" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo14" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo15" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo16" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo17" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo18" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo19" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo20" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo21" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo22" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo23" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo24" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo25" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo27" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo28" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo29" 
    android:duration="41"/> 
<item 
    android:drawable="@drawable/dancinglogo30" 
    android:duration="41"/> 

<!-- Reset to original --> 
<item 
    android:drawable="@drawable/dancinglogo01" 
    android:duration="10"/> 

    </animation-list> 

Вот мой LogCat:

08-17 09:59:22.487: E/AndroidRuntime(22392): FATAL EXCEPTION: main 
    08-17 09:59:22.487: E/AndroidRuntime(22392): java.lang.NullPointerException 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at   com.thatnerdstuff.build_a_rig.MainActivity.wavingAndroid(MainActivity.java:149) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at com.thatnerdstuff.build_a_rig.MainActivity.access$0(MainActivity.java:145) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at com.thatnerdstuff.build_a_rig.MainActivity$1.onClick(MainActivity.java:46) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at android.view.View.performClick(View.java:4203) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at android.view.View$PerformClick.run(View.java:17189) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at android.os.Handler.handleCallback(Handler.java:615) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at android.os.Handler.dispatchMessage(Handler.java:92) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at android.os.Looper.loop(Looper.java:137) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at android.app.ActivityThread.main(ActivityThread.java:4950) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at java.lang.reflect.Method.invokeNative(Native Method) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at java.lang.reflect.Method.invoke(Method.java:511) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
    08-17 09:59:22.487: E/AndroidRuntime(22392): at dalvik.system.NativeStart.main(Native Method) 

Любая помощь по вопрос был бы фантастическим, поскольку я смотрел на многие другие форумы, и чтобы попытаться найти исправление, и я не знаю, откуда он. Я пробовал очистить проект и ничего. Заранее спасибо!

+1

Что находится в строке 149? com.thatnerdstuff.build_a_rig.MainActivity.wavingAndroid (MainActivity.java:149) –

+0

androidAnimation.start(); @matheszabi –

+0

androidAnimation = (AnimationDrawable) animateAndroid.getDrawable(); возвращает null thats проблема, которая должна ее решить –

ответ

1

Try загрузка изображений в AnimationDrawable в коде, как это:

AnimationDrawable frameAnimation= new AnimationDrawable(); 
frameAnimation.addFrame(getResources().getDrawable(R.drawable.dancinglogo01), 41); 
frameAnimation.addFrame(getResources().getDrawable(R.drawable.dancinglogo02), 41); 

Добавить все кадры, как это в вашем методе wavingAndroid(), а затем загрузить его в ImageView как этот

animateAndroid = (ImageView) findViewById(R.id.dancingAndroidLogo); 
animaateAndroid.setBackgroundDrawable(frameAnimation); 

Теперь в вашем onClick() метод запуска анимации с использованием:

frameAnimation.start(); 
Смежные вопросы