setBackgroundResource抛出空异常的原因是什么?

时间:2012-09-08 00:52:22

标签: android animation background null drawable

在下面的代码中,我一直收到一个null异常错误,并且已经测试了一下,以了解它来自代码行'startup_logo.setBackgroundResource(R.anim.startup)',更具体地来说是'setBackgroundResource'。我不确定为什么会发生这种情况,有人可以帮忙吗?感谢。

    protected static AnimationDrawable temp, startup_move, menu_start, featuredpanel_start, collection_start, recommend_start, top_start, random_start;

    protected static void startup() {

        for (int i = 0; true;) {
            Log.d(i + "", "Content");
            if (i == 0) {
                startup_logo.setBackgroundResource(R.anim.startup);
                temp = (AnimationDrawable) startup_logo.getBackground();

                fadeout.start();
            }
            else if (i == 1) {
                featuredpanel.setBackgroundResource(R.anim.featured_move);
                temp = (AnimationDrawable) featuredpanel.getBackground();

                menu.setAnimation(fadein);
                logo.setAnimation(fadein);

                fadein.start();
            }
            else if (!temp.isRunning()) {
                temp.start();
                i++;
            }
        }
    }
}

另一部分代码定义了startup_logo,如下所示:

startup_logo = (LinearLayout) findViewById (R.anim.startup);

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/startup"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@anim/startup" >

TRACEBACK:

09-08 01:02:31.239: E/AndroidRuntime(1105): FATAL EXCEPTION: main
09-08 01:02:31.239: E/AndroidRuntime(1105): java.lang.RuntimeException: Unable to start activity ComponentInfo{My.Taste.App/My.Taste.App.MyTasteActivity}: java.lang.NullPointerException
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.os.Looper.loop(Looper.java:137)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at java.lang.reflect.Method.invoke(Method.java:511)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at dalvik.system.NativeStart.main(Native Method)
09-08 01:02:31.239: E/AndroidRuntime(1105): Caused by: java.lang.NullPointerException
09-08 01:02:31.239: E/AndroidRuntime(1105):     at My.Taste.App.TBGStartAnimations.startup(TBGStartAnimations.java:18)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at My.Taste.App.MyTasteActivity.onCreate(MyTasteActivity.java:68)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.app.Activity.performCreate(Activity.java:4465)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-08 01:02:31.239: E/AndroidRuntime(1105):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-08 01:02:31.239: E/AndroidRuntime(1105):     ... 11 more

1 个答案:

答案 0 :(得分:0)

我明白了!

startup_logo = (LinearLayout) findViewById (R.anim.startup);

应该由R.id而不是R.anim搜索

startup_logo = (LinearLayout) findViewById (R.id.startup);