如何在android中删除视图

如何在android中删除视图,android,viewgroup,Android,Viewgroup,删除视图时,出现错误,我找不到错误的原因: **java:** package com.test; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.animation.AlphaA

删除视图时,出现错误,我找不到错误的原因:

**java:**

  package com.test;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.animation.AlphaAnimation;
    import android.view.animation.Animation;
    import android.view.animation.Animation.AnimationListener;
    import android.widget.ImageView;

    public class AnimationActivity extends Activity {
        ImageView imageView = null;
        ViewGroup viewGroup = null;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            imageView = (ImageView)findViewById(R.id.imageviewID);
            viewGroup = (ViewGroup)findViewById(R.id.layoutId); 
        }
        public void buttonOnclick(View e){
            int id = e.getId();
            if(id==R.id.alpha){
                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0f);
                alphaAnimation.setDuration(5000);
                alphaAnimation.setStartOffset(500);
                alphaAnimation.setFillAfter(true);
                alphaAnimation.setFillBefore(false);
                alphaAnimation.setAnimationListener(new RemoveAnimationListener());
                imageView.startAnimation(alphaAnimation);
                Log.d("mydebug","imageView------------>"+imageView);
                Log.d("mydebug","viewGroup------------>"+viewGroup);
            }
        }
        private class RemoveAnimationListener implements AnimationListener{

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                viewGroup.removeView(imageView);

            }
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }
        }
    }
  05-22 10:01:16.367: W/dalvikvm(1839): threadid=1: thread exiting with uncaught exception (group=0x4001d7c8)ss
    05-22 10:01:16.397: E/AndroidRuntime(1839): FATAL EXCEPTION: main
    05-22 10:01:16.397: E/AndroidRuntime(1839): java.lang.NullPointerException
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1366)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.View.draw(View.java:6743)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.View.draw(View.java:6743)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1857)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewRoot.draw(ViewRoot.java:1407)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.os.Looper.loop(Looper.java:123)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at java.lang.reflect.Method.invoke(Method.java:521)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at dalvik.system.NativeStart.main(Native Method)
以下是错误:

**java:**

  package com.test;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.animation.AlphaAnimation;
    import android.view.animation.Animation;
    import android.view.animation.Animation.AnimationListener;
    import android.widget.ImageView;

    public class AnimationActivity extends Activity {
        ImageView imageView = null;
        ViewGroup viewGroup = null;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            imageView = (ImageView)findViewById(R.id.imageviewID);
            viewGroup = (ViewGroup)findViewById(R.id.layoutId); 
        }
        public void buttonOnclick(View e){
            int id = e.getId();
            if(id==R.id.alpha){
                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0f);
                alphaAnimation.setDuration(5000);
                alphaAnimation.setStartOffset(500);
                alphaAnimation.setFillAfter(true);
                alphaAnimation.setFillBefore(false);
                alphaAnimation.setAnimationListener(new RemoveAnimationListener());
                imageView.startAnimation(alphaAnimation);
                Log.d("mydebug","imageView------------>"+imageView);
                Log.d("mydebug","viewGroup------------>"+viewGroup);
            }
        }
        private class RemoveAnimationListener implements AnimationListener{

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub
                viewGroup.removeView(imageView);

            }
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }
        }
    }
  05-22 10:01:16.367: W/dalvikvm(1839): threadid=1: thread exiting with uncaught exception (group=0x4001d7c8)ss
    05-22 10:01:16.397: E/AndroidRuntime(1839): FATAL EXCEPTION: main
    05-22 10:01:16.397: E/AndroidRuntime(1839): java.lang.NullPointerException
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1366)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.View.draw(View.java:6743)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.View.draw(View.java:6743)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1857)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewRoot.draw(ViewRoot.java:1407)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.os.Looper.loop(Looper.java:123)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at java.lang.reflect.Method.invoke(Method.java:521)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    05-22 10:01:16.397: E/AndroidRuntime(1839):     at dalvik.system.NativeStart.main(Native Method)
您已在AnimationActivity类中使用ButtonNonclick方法。所以如果你有 提到在XML文件中单击哪个按钮就好了。如果你没有提到 在XML中单击哪个按钮,那么您必须在活动中提到哪个按钮 单击以进行相应的方法调用。还有一件事是如果它没有 查找视图组,使其显示空指针异常。您正在尝试访问或 使用值为null或未找到的视图组

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutId"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView android:id="@+id/imageviewID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:contentDescription="@string/app_name"
        android:paddingBottom="50dip"
        />
    <Button android:id="@+id/alpha"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/alpha"
        android:onClick="buttonOnclick"
        android:layout_below="@id/imageviewID"
        />
</RelativeLayout>

首先按下按钮,viewgroup将删除imageview,然后viewgroup不包括imageview;如果再次按下按钮,imageview为空,因此无法从视图组中删除imageview。

请在注释日志后重试。dmydebug,imageview------>+imageview;Log.dmydebug,viewGroup-->+viewGroup;Line是指要删除的视图中指向ButtonClick方法的按钮吗?