ViewFlipper : Receiver not registered

2020-02-06 02:34发布

问题:

In my app sometimes I receive this error :

 java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1@4806a4a8   
at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:667)
at android.app.ApplicationContext.unregisterReceiver(ApplicationContext.java:840)
at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:321)
at android.widget.ViewFlipper.onDetachedFromWindow(ViewFlipper.java:104)
at android.view.View.dispatchDetachedFromWindow(View.java:5891)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1076)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1074)
at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1570)
at android.view.ViewRoot.doDie(ViewRoot.java:2565)
at android.view.ViewRoot.die(ViewRoot.java:2535)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:218)
at android.view.Window$LocalWindowManager.removeViewImmediate(Window.java:436)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3498)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3599)
at android.app.ActivityThread.access$2300(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1867)
 at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)

What's this? What should I do? Need some help please...

Here is some code :

on onCreate() method I have this :

vf = (ViewFlipper) findViewById(R.id.details);
        vf.setFlipInterval(3000);
        vf.startFlipping();
        populate();

where populate method is this :

private void populate() {
        for (int i = 0; i < jArray.length(); i++) {
            System.out.println("lungime" + jArray.length());
            LinearLayout l = new LinearLayout(this);
            l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            l.setBackgroundColor(0x000000);
            l.setOrientation(LinearLayout.VERTICAL);
            vf.addView(l);

            File f = new File(Environment.getExternalStorageDirectory()
                    + "/Downloads/");

            File[] files = f.listFiles();

            Bitmap bitmap = BitmapFactory.decodeFile(files[i].getPath());
            img = new ImageView(this);
            img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));

            img.setImageBitmap(bitmap);

            System.out.println("target " + target[i]);
            img.setOnTouchListener(this);
            img.setId(i);

            l.addView(img);
            img = null;

        }

回答1:

According to Daniel Lew's solution, create this class:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ViewFlipper;

public class GabrielleViewFlipper extends ViewFlipper {
    public GabrielleViewFlipper(Context context) {
        super(context);
    }
    public GabrielleViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onDetachedFromWindow() {
        try {
            super.onDetachedFromWindow();
        }
        catch (IllegalArgumentException e) {
            stopFlipping();
        }
    }
}

and in your layout don't use the normal ViewFlipper, use GabrielleViewFlipper:

<your.package.GabrielleViewFlipper
    .
    .
    .
</your.package.GabrielleViewFlipper>


回答2:

Try adding this class to your project:

public class ContentViewFlipper extends ViewFlipper {
   public ContentViewFlipper( Context context ) {
      super( context );
   }

   public ContentViewFlipper( Context context, AttributeSet attrs ) {
      super( context, attrs );
   }

   @Override
   protected void onDetachedFromWindow() {
      try {
         super.onDetachedFromWindow();
      }
      catch( Exception e ) {}
   }
}

And then use it instead of the regular ViewFlipper in your XML:

<com.yourPackage.ContentViewFlipper
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Edit

As this problem seems to be specific to Android 2.1 (API 7), you can make sure you only target this change by copying the layout file(s) with the ViewFlipper to res/layout-v7 and then changing the ViewFlipper to the ContentViewFlipper in those layouts. This way only devices running Android 2.1 will use the fix.