ViewAnimator导致NullPointerException

时间:2012-07-13 15:48:24

标签: android nullpointerexception viewanimator

嘿,我正在尝试使用ViewAnimator,我可能错过了一些基本的东西。 这就是我所拥有的:

        va = new ViewAnimator(this);
    va.setInAnimation(inFromLeftAnimation());
    va.setOutAnimation(outToRightAnimation());

    RelativeLayout stage = (RelativeLayout)findViewById(R.layout.american_stage);
    va.addView(stage,0);


    setContentView(va);

inFromLeftAnimation和outToRightAnimation只处理动画并完美运行。 当然,文件真的存在...... 我得到这个日志:

   07-13 15:45:14.515: E/AndroidRuntime(564): java.lang.RuntimeException: Unable to start activity ComponentInfo{shibby.webhunt/shibby.webhunt.ArcadeModeActivity}: java.lang.NullPointerException
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.os.Looper.loop(Looper.java:123)
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-13 15:45:14.515: E/AndroidRuntime(564):  at java.lang.reflect.Method.invokeNative(Native Method)
07-13 15:45:14.515: E/AndroidRuntime(564):  at java.lang.reflect.Method.invoke(Method.java:521)
07-13 15:45:14.515: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-13 15:45:14.515: E/AndroidRuntime(564):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-13 15:45:14.515: E/AndroidRuntime(564):  at dalvik.system.NativeStart.main(Native Method)
07-13 15:45:14.515: E/AndroidRuntime(564): Caused by: java.lang.NullPointerException
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.view.ViewGroup.addView(ViewGroup.java:1815)
07-13 15:45:14.515: E/AndroidRuntime(564):  at shibby.webhunt.ArcadeModeActivity.onCreate(ArcadeModeActivity.java:25)
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

我不明白为什么。 是否无法将XML文件用作视图?我是否必须自己构建观点?

1 个答案:

答案 0 :(得分:0)

07-13 15:45:14.515: E/AndroidRuntime(564): Caused by: java.lang.NullPointerException
07-13 15:45:14.515: E/AndroidRuntime(564):  at android.view.ViewGroup.addView(ViewGroup.java:1815)

看起来您正在尝试添加一个null的视图(RelativeLayout stage),我不相信您可以使用findViewById()加载布局:

findViewById(R.layout.american_stage);

如果您尝试在american_stage.xml中打开不可见的布局,请使用LayoutInflater:

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
stage = layoutInflater.inflate(R.layout.american_stage, null);  

如果您尝试加载已经可见的RelativeLayout:

1)为RelativeLayout提供身份american_stage

<RelativeLayout 
    android:id="@+id/american_stage"
    ...

2)并使用:

findViewById(R.id.american_stage);