0

在下面的代码中,我不断收到一个空异常错误,并且已经测试到知道它来自代码行“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" >

追溯:

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
4

1 回答 1

0

我想到了!

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

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

startup_logo = (LinearLayout) findViewById (R.id.startup);
于 2012-09-08T01:19:27.707 回答