10

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;

        }
Gabrielle
  • 4,933
  • 13
  • 62
  • 122
  • now I found this http://daniel-codes.blogspot.com/2010/05/viewflipper-receiver-not-registered.html . I am using a device cu version 2.1, so here looks to be the problem :( – Gabrielle Nov 08 '11 at 13:11
  • I get this error if I don't keep the device on my hand on landscape mode. The app is only for landscape mode and if the device is kept on portrait mode I get a force close. Where is the problem? – Gabrielle Nov 21 '11 at 08:11

2 Answers2

36

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
5

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.

kaspermoerch
  • 16,127
  • 4
  • 44
  • 67
  • 2
    I think is not only for Android 2.1...it seems to appear on Android 2.2 too. It is working like this. – Gabrielle Nov 21 '11 at 12:29