-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only the original thread that created a view hierarchy can touch its view #14052
Comments
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process. |
It looks like a worker thread is trying to load image content into an Android.Widget.ImageView which, of course, fails because it's not being loaded in the main (UI) thread. |
I originally had the content being loaded in the main thread but ran into another problem and was advised not to do that. So I now move it into an async call but end up with this issue. Any advice on how to work around this for now? |
As far as I can tell, your code isn't doing anything wrong. From what I can tell, you async fetch the |
Let me paste the call stack for continued reference (maybe someone else on the team can help point me in the right direction):
|
Transition back to the calling thread context (which should theoretically be the main thread). Fixes issue #14052
Oh, fixed!? Good job getting that done so quickly. Any idea on rough timeline for when it will be released? |
Transition back to the calling thread context (which should theoretically be the main thread). Fixes issue #14052
Transition back to the calling thread context (which should theoretically be the main thread). Fixes issue #14052
Transition back to the calling thread context (which should theoretically be the main thread). Fixes issue #14052
I'm having this same issue and exception with image views. How would you upgrade Maui if a fix gets released? Have you been able to do so yet? |
No I am still having the issue. I can't release until this is fixed. I hoped it would have been released by now but not sure how to tell when or if it is getting released. |
Actually shortly after I wrote that message I noticed there was a 7.0.81 Service Release today. I installed that and I can't seem to get the exception to reoccur. https://github.com/dotnet/maui/releases/tag/7.0.81 |
Oh, thanks for the update, anthough I can't see #14052 listed on that page... |
I looked for that bug fix number too but even though I didn't see it I thought I would try it anyway. Let me know if you try it as I may be missing a scenario. I still cannot reproduce the crash which was quite easy to create before. |
I just had a play and also seems to be ok for me. However I have found it to be intermittent in the past so will need to give it a few days before declaring that it is fixed! |
This issue came up for me from the MAUI version 7.0.59 while in 7.0.58 it worked.
In my case I have a collectionview showing pictures in contentview containing Image as control with source
It happens when I remove an item from the CollectionView and try to add a new one. The CollectionView itemtemplate:
|
Hey guys, my patch still hasn't been merged so it's not yet in any released version. |
The comment was about that it is a regression bug - it only appeared since 7.0.59 and did not appear in 7.0.58. |
I know it seems that way to you based on your test case, but it has existed for a while now. My patch solves this by making sure that the image data gets set on the Android Image control in the main thread consistently. The current code does not always context switch back to the main thread when setting that image data. Note that I said "doesn't always", meaning sometimes the current code does the right thing but it is through "luck". You got lucky with 7.0.58 and are unlucky with 7.0.59. |
Happened now to me randomly when zooming an image by hand using the pinch & zoom control from official documentation (https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/gestures/pinch)
|
Yeah supposedly 'fixed' nearly 2 months ago but still happening. |
My patch to fix this still hasn't been merged. Hopefully soon. |
Thanks for the update. If it takes this long then it doesn't bode well for the multitude of critical bugs which haven't even been looked at yet! |
I ran into this issue as well. Understanding what was happening took a while, but it seemed to occur when updating an ImageSource in a control I created. When setting the image source a second time, I would encounter the "Only the original thread that created a view hierarchy can touch its views." error message and an app crash. I was able to work around this issue by removing the control (in my case an ImageButton) from its parent, creating a new one with the updated Source, and then adding that back to the parent control. |
For me that happened super randomly in 1 case of 1000, did that happen for you in 100 % cases? Also the issue in my case was that I was not using another thread. |
Are you sure you are looking at the same problem? This is something that happens pretty much 100% of the time for me, on every page I have an image button or an image. To me it looks like a problem with scrolling because it seems to happen while the page with the image scrolls. |
I got this issue also when I fired MVVM Message and setting the Imagesource inside the subscribe event void SubcribeForNewPetCreation()
{
WeakReferenceMessenger.Default.Register<CreatePetMessageModel>(this, (_, message) =>
{
MainThread.BeginInvokeOnMainThread(async () =>
{
var pet = message;
var speciesGroupName = pet.speciesName;
var species = Items.FirstOrDefault(species => species.SpeciesName.Equals(speciesGroupName));
if (species is null)
return;
var petProfileForUpdateUI = mapper.Map<PetProfileCardModel>(pet);
petProfileForUpdateUI.PetAvatar = await ConvertFileResultToImageSource(pet.petAvatar);
species.Add(petProfileForUpdateUI);
var petProfileForServer = mapper.Map<CreateAnimalDTO>(pet);
petProfileForServer.AvatarFile = await ConvertFileResultToStreamPart(pet.petAvatar);
await animalService.CreateAsync(petProfileForServer);
});
});
} async Task<StreamPart> ConvertFileResultToStreamPart(FileResult file)
{
using (var fileBytes = await file.OpenReadAsync())
{
return new StreamPart(fileBytes, file.ContentType);
}
}
async Task<ImageSource> ConvertFileResultToImageSource(FileResult file)
{
MemoryStream memoryStream = new MemoryStream();
await file.OpenReadAsync().ContinueWith((t) =>
{
var stream = t.Result;
stream.CopyTo(memoryStream);
memoryStream.Position = 0;
});
return ImageSource.FromStream(() => new MemoryStream(memoryStream.ToArray()));
} |
Transition back to the calling thread context (which should theoretically be the main thread). Fixes issue #14052
Still happening when binding an Image's Source to something. A simple refresh of a user's profile (which in turn updates their profile picture byte[]) crashes the app. |
Transition back to the calling thread context (which should theoretically be the main thread). Fixes issue #14052
same problem for me, happening when I update the imagesource of an avatarview. |
@samhouts @jstedfast - I don't get it. This was marked as fixed in March. It's still happening. That's 3 months of waiting so I am not convinced that the problem is actually fixed. |
@williambuchanan2 as mentioned here #15397, the planned release is for .NET 8 Preview 5 but I cannot find any .NET 7 release. |
I'm sure nobody will mind that you can't put images into Maui apps. Stupid of me to think this would ever have been possible. |
Description
I seem to have an unhandled exception in Android when showing images on a page. The error is:
Android.Util.AndroidRuntimeException: 'Only the original thread that created a view hierarchy can touch its views.'
Steps to Reproduce
Open the project in the repository below.
Run the project and click the "Image Error" button.
This will open a page which downloads some images from a web API.
Scroll up and down the page a few times. Eventually the error will occur.
Link to public reproduction project repository
https://github.com/williambuchanan2/MauiNavigation
Version with bug
7.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 11 and 13
Did you find any workaround?
No. Unable to proceed with project so this is a blocking problem.
Relevant log output
No response
The text was updated successfully, but these errors were encountered: