Android. Why would an image in activity 1 end up in activity 2? (Zxing CaptureActivity is activity 2)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 23:56:13

问题


All was going well developing a new app until I started dressing up the interface.

I have implemented the zxing barcode scanner within my project for user convenience and my custom control.

The only changes I make to the CaptureActivity are in handling the scan results. I also shot through the AndroidManifest.xml and took away the intent filters to avoid my app from being offered for the requests the barcode scanner app handles. (It doesn't feel right having the android OS offer my activity alongside of the hard earned "awesomeness" of Xzing's Barcode Scanner)

I added two ImageViews to my index activity and 1 of them triggers the scanner. The interesting part is that the image that shows up in the scanner is not the 1 that links to the scanner.

In summary, 2 images for navigation, Second Image leads to scanner, first image is blocking my scanner view.

At first I changed the name of the asset (was add_card.png to add_tag.png) and I had a clean view of the scanner, but since then the image has crept back in.

Here is some index activity code

//in on create
ImageView view = (ImageView) findViewById(R.id.addTagButton);
        view.setOnClickListener(addCardListener);

view = (ImageView) findViewById(R.id.scanButton);
        view.setOnClickListener(scanListener);

// end of in on create

private OnClickListener addCardListener = new OnClickListener() {
    public void onClick(View v) {
        startActivity(new Intent(getApplicationContext(), AddCard.class));
    }
};
private OnClickListener scanListener = new OnClickListener() {
    public void onClick(View v) {
        startActivity(new Intent(getApplicationContext(), CaptureActivity.class));
    }
};

Some xml that the index file loads

<ImageView  android:src="@drawable/add_tag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginLeft="12dip"
        android:id="@+id/addTagButton"
        android:layout_gravity="bottom|left"
></ImageView>

And the only changes I made in the CaptureActivity are in the code that is triggered after reading the barcode.

UPDATE

I tried setting the launcher to CaptureActivity and the image is not showing up like in the screenshot. This was advice from Pawan and I think we were both hoping the problem would be replicated so that I would start looking in the 1 place I had already crossed off my list. (That is how I find my keys when they are lost)

UPDATE 2

I tried changing the ImageView in question (add_tag) to a button --- no change If I remove the ImageView in question from xml, not an applicable solution, the issue disappears. It is something about when that png is already loaded into memory. I have more images loaded than described above in my Index activity, but the add_tag image consistently shows up over the camera. Then if I keep the image resource in res/drawable, but never load it into the Index activity, then it doesn't show up over the camera.

What are some reasons an image from the activity1 will end up in activity2? How can I clean up Index activity so that it is as if the xml never loaded the image in onStop?

UPDATE 3 I do not experience the problem anymore and can move on. I am still unsure if it has to do with the camera in general or Zxing. Considering Sean's advice about it having to do with Java.R, I tried removing the drawable that was causing the problem, rename, insert into project. This didn't exactly work.
It resolved when I added a copy of my drawable into the project.
The drawable folder now starts with add_card add_tag ...etc

If I set my ImageView to add_card in the Index activity, the problem occurs, If I set my ImageView to add_tag no problem occurs. --> This leads me to believe that the first (alphabetically) drawable resource will conflict with Scanner I am going to keep a placeholder image that I never load as the top of the drawable folder and keep my fingers crossed.

There's some food for thought. (I am still open to someone explaining to me why this is happening)

PS Feel free to edit down my question if you feel my journey to the solution is a little bloated.


回答1:


The most likely explanation is that your generated R.java file is out of sync with your compiled code.

It's possible that your bytecode refers to constant values that have since changed, and so are now pointing at the wrong images. Just clean and recompile, if so. But I don't think that's it.

I think you've got resource IDs from our code, and yours, and it's not using the one you think. This is symptomatic of copying-and-pasting our project. We strongly discourage starting from a clone; there are far too many of those already. It would be better (and probably less error prone) to write your own scanner app and reuse bits and pieces as needed.




回答2:


The bug is manifesting itself only when xzing's CaptureActivity is "called" from activity that is in landscape mode. If "calling" activity is in portait bug does not occur. Subsequent calls to CaptureActivity are OK from landscape mode. I can't find the exact cause but the effect is that Views in CaptureActivity "autoset" drawable for background and thus obstructing the camera view. Luckily there are two possible workarounds: 1. As Maudicus found himself: just place some image file in res/drawable/ that is first alphabetically, for example aaa.png. 2. Remove background drawables at runtime like this:

((TextView) findViewById(R.id.status_view)).setBackgroundDrawable(null);
((ViewfinderView) findViewById(R.id.viewfinder_view)).setBackgroundDrawable(null);

Place the code in onCreate() just bellow setContentView(R.layout.capture);.

You may need to remove background drawables from more views depending on what is visible in your layout.



来源:https://stackoverflow.com/questions/8236278/android-why-would-an-image-in-activity-1-end-up-in-activity-2-zxing-captureac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!