从 Android 中的 LinearLayout 中删除视图时出现异常

发布于 2024-11-18 08:46:52 字数 6159 浏览 2 评论 0原文

我尝试在将自定义视图从 LinearLayout 容器中删除之前对其进行动画处理。只是我不断收到 NullPointerException,我无法追踪到任何特定的位置。

当我添加两个视图并删除第一个视图时,会发生异常。

我的代码是:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.form_alg);
    fadeInAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.witness_fade_in);

    list = (LinearLayout) this.findViewById(R.id.alg_list_getuigen);

    addWitness = (ImageButton) this
            .findViewById(R.id.alg_getuige_toevoegen);
    addWitness.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final View w = View.inflate(getApplicationContext(),
                    R.layout.form_getuige, null);

            ImageButton button = (ImageButton) w
                    .findViewById(R.id.getuige_verwijderen);
            button.setOnClickListener(new android.view.View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.d(TAG, "deleteView");
                    Animation fadeOutAnimation = AnimationUtils
                            .loadAnimation(getApplicationContext(),
                                    R.anim.witness_fade_out);
                    fadeOutAnimation
                            .setAnimationListener(new AnimationListener() {
                                @Override
                                public void onAnimationEnd(
                                        Animation animation) {
                                    list.removeView(w);
                                    list.invalidate();
                                }

                                @Override
                                public void onAnimationRepeat(
                                        Animation animation) {
                                }

                                @Override
                                public void onAnimationStart(
                                        Animation animation) {
                                }
                            });
                    w.setVisibility(View.VISIBLE);
                    w.startAnimation(fadeOutAnimation);
                    w.setVisibility(View.INVISIBLE);
                }
            });
            Log.d(TAG, "addView");

            w.setVisibility(View.INVISIBLE);
            list.addView(w);
            w.startAnimation(fadeInAnimation);
            w.setVisibility(View.VISIBLE);
            list.invalidate();
        }

    });
}

这是堆栈跟踪:

07-03 03:17:30.335: WARN/dalvikvm(343): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): FATAL EXCEPTION: main
07-03 03:17:30.365: ERROR/AndroidRuntime(343): java.lang.NullPointerException
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1372)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.View.draw(View.java:6986)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.widget.ScrollView.draw(ScrollView.java:1409)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.View.draw(View.java:6883)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.View.draw(View.java:6883)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewRoot.draw(ViewRoot.java:1522)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.os.Looper.loop(Looper.java:123)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at java.lang.reflect.Method.invokeNative(Native Method)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at java.lang.reflect.Method.invoke(Method.java:507)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at dalvik.system.NativeStart.main(Native Method)

Im trying to animate a custom view before removing it from a LinearLayout container. Only I keep getting a NullPointerException which I can't trace to anywhere specific.

The exception happens when I have added two views and remove the first one.

My code is:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.form_alg);
    fadeInAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.witness_fade_in);

    list = (LinearLayout) this.findViewById(R.id.alg_list_getuigen);

    addWitness = (ImageButton) this
            .findViewById(R.id.alg_getuige_toevoegen);
    addWitness.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final View w = View.inflate(getApplicationContext(),
                    R.layout.form_getuige, null);

            ImageButton button = (ImageButton) w
                    .findViewById(R.id.getuige_verwijderen);
            button.setOnClickListener(new android.view.View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.d(TAG, "deleteView");
                    Animation fadeOutAnimation = AnimationUtils
                            .loadAnimation(getApplicationContext(),
                                    R.anim.witness_fade_out);
                    fadeOutAnimation
                            .setAnimationListener(new AnimationListener() {
                                @Override
                                public void onAnimationEnd(
                                        Animation animation) {
                                    list.removeView(w);
                                    list.invalidate();
                                }

                                @Override
                                public void onAnimationRepeat(
                                        Animation animation) {
                                }

                                @Override
                                public void onAnimationStart(
                                        Animation animation) {
                                }
                            });
                    w.setVisibility(View.VISIBLE);
                    w.startAnimation(fadeOutAnimation);
                    w.setVisibility(View.INVISIBLE);
                }
            });
            Log.d(TAG, "addView");

            w.setVisibility(View.INVISIBLE);
            list.addView(w);
            w.startAnimation(fadeInAnimation);
            w.setVisibility(View.VISIBLE);
            list.invalidate();
        }

    });
}

Here is the stacktrace:

07-03 03:17:30.335: WARN/dalvikvm(343): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-03 03:17:30.365: ERROR/AndroidRuntime(343): FATAL EXCEPTION: main
07-03 03:17:30.365: ERROR/AndroidRuntime(343): java.lang.NullPointerException
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1372)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.View.draw(View.java:6986)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.widget.ScrollView.draw(ScrollView.java:1409)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.View.draw(View.java:6883)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.View.draw(View.java:6883)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewRoot.draw(ViewRoot.java:1522)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.os.Looper.loop(Looper.java:123)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at java.lang.reflect.Method.invokeNative(Native Method)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at java.lang.reflect.Method.invoke(Method.java:507)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-03 03:17:30.365: ERROR/AndroidRuntime(343):     at dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛清羽墨安 2024-11-25 08:46:52

我也有同样的问题。您必须使用处理程序来执行修改 UI 的代码(在本例中为removeView())。我将添加一个 Handler 字段并在 onCreate() 方法中构造它。然后,在 onAnimationEnd() 中,执行以下操作:

fHandler.post(new Runnable() {
    @Override
    public void run() {
        list.removeView(w);
        list.invalidate();
    }
}

I had the same problem. You have to use a Handler to execute the code that modifies the UI (removeView() in this case). I would add a Handler field and construct it in your onCreate() method. Then, in onAnimationEnd(), just do this:

fHandler.post(new Runnable() {
    @Override
    public void run() {
        list.removeView(w);
        list.invalidate();
    }
}
北凤男飞 2024-11-25 08:46:52

要删除布局的所有子级,然后添加新的子级,请找到以下代码

if (linearLayout.getChildCount() > 0)
            {
                linearLayout.removeAllViewsInLayout();
                linearLayout.invalidate();
            }

To remove all child of layout and then add new childs, find below code

if (linearLayout.getChildCount() > 0)
            {
                linearLayout.removeAllViewsInLayout();
                linearLayout.invalidate();
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文