0

When the view is removed, errors occurred, I can not find the cause of the errors:

**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

            }
        }
    }

Here are errors:

  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)
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • try after commenting Log.d("mydebug","imageView------------>"+imageView); Log.d("mydebug","viewGroup------------>"+viewGroup); lines – ρяσѕρєя K May 22 '12 at 03:35
  • Is the button pointing to your `buttonOnclick` method in the view that you are removing? – Barak May 22 '12 at 04:01

2 Answers2

0

You have use buttononclick method in your AnimationActivity class. So if you have mentioned which button click in your XML file then good. If you have not mention
in XML which button is click, then you must mention in your activity which button click so that the corresponding method call is made. One more thing is that if it did not find view group so that it shows null pointer exception. You are trying to access or use view group which value is null or not found.

<?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>
Flexo
  • 87,323
  • 22
  • 191
  • 272
itechDroid
  • 1,031
  • 1
  • 11
  • 17
0

you first press button, viewgroup will remove imageview,then viewgroup doesn't include Imageview; if you second press button, imageview is null,so you can't remove imageview from viewgroup.

vane
  • 1