IndexOutOfRangeExeption Xamarin Live Player - android

I'm studying Xamarin for two weeks now and I'm following the Xamarin.University course (Consuming REST-based Web Services (XAM150)). In an exercise we have to simply check the connection of the phone. My emulator (Android 7.0) in Visual Studio 2017 does it very well. But when I try on my actual phone (Android 7.1) using Xamarin Live Player, the app will not start and the following Error appears on my phone:
"Visualization Error. Index was outside the bounds of the array.
(IndexOutOfRangeExeption)"
I've searched Google and the forum of Xamarin.University but no luck, it seems that no one else has this problem. When I debug, it stops at when it checks the connectivity. I use the Xam.Plugin.Connectivity for asking the connection and yes, it is referenced in all the parts of the project.
I've come from using this code from the site:
using Plugin.Connectivity;
public App() {
// The root page of your application
MainPage = CrossConnectivity.Current.IsConnected
? (Page) new NetworkViewPage()
: new NoNetworkPage();
}
To this to debug:
using Plugin.Connectivity;
public App() {
InitializeComponent();
try {
var isConnected = CrossConnectivity.Current.IsConnected;
MainPage = isConnected
? (Page)new NetworkViewPage()
: (Page)new NoNetworkPage();
} catch (Exception e) {
throw e;
}
//MainPage = new NetworkViewPage();
}
All this code works for my emulator, but not for my phone. I can display the different pages on my phone but not when I check for the connectivity so the error is really from the plugin and not from my pages.
Has someone any idea what I can try to make this work?

Frauke Nonnenmacher pointed out that it could be a problem with Xamarin Live Player and apparantly that is the issue. If I implement it on my phone with USB debugging, it works just fine. It didn't occur to me that Live Player could be the issue since Xamarin.University wants you to use it and I didn't find anybody on the forum there that had the same problem.
My guess is that checking the connection gives a problem with Xamarin Live Player because it works over the network, you can't use the Player if you don't have a wifi connection. So maybe something goes wrong there.
It is still a mystery why it doesn't work, but a workaround is just USB debugging.

Related

Android only: C# .net-MAUI, not Xamarin(!), sending UDP?

Situation:
VS 2022, 17.0.4
Maui App,
net6.0-android
AndroidManifest.xml contains also:
android.permission.INTERNET
android.permission.CHANGE_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
Connected mobile Phone:
Samsung SM-G960F (Android 10.0 - API 29)
OS: Windows 11, latest patch.
All firewalls are down (for testing purpose only!)
While debugging the develop computer is only connected to a Wifi network; computers ethernet card is disabled.
Mobile phone is connected to this dev computer via USB cable (to be able to debug) and to the same Wifi network as the computer.
App starts and works fine, app can be debugged. No issue at all - except:
After the application is fully initialized and ready to accept user interactions -> Click on button -> Desired method is called -> Code is worked out -> The code should make a simple UDP call but it does not (or the packet does not reach the UDP listener due to missing configuration?).
The UDP receiver works fine and is capable to receive UDP packets.
My mobile phone and the UDP receiver app are using the same port.
I read/found already that in the previous cross-platform framework, means “Xamarin (Android SDK 12)”, some permissions must be set (I did, see above) and that the multicastlock must be set over the WifiManager …
I tried this in my MAUI app. But could not find anything guiding me nor figured it out by myself.
My MAUI sending code:
var dataToBeSend = "What ever ...";
var data = Encoding.UTF8.GetBytes(dataToBeSend);
var UdpClient = new UdpClient();
// UdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
UdpClient.ExclusiveAddressUse = false;
UdpClient.EnableBroadcast = true;
// UdpClient.Client.Bind(new IPEndPoint(IPAddress.Parse("255.255.255.255"), BroadcastPort));
// UdpClient.Client.Bind(new IPEndPoint(IPAddress.Any, BroadcastPort));
UdpClient.Send(data, data.Length, "255.255.255.255", BroadcastPort);
As said: very easy and straight forward.
Notice that I also tried binding UDP code …
So please can someone be so kind to guide me or give me a hint?
Thank you very much in advance!
ANSWER:
After two days I found a solution - and would like to share it because may be it helps someone else.
The code to make the UDP call msut be placed in a THREAD (not task!)
codesnippet:
var communication = new Communication();
var udpThread = new Thread(new ThreadStart(communication.FireUDPCall));
udpThread.Start();
The firewalls can stay turned on / active!

Process.getExclusiveCores() throws exception on certain devices

My Android app is currently in open beta and I am receiving crash reports from my beloved testers. Audio processing is the app's primary focus therefore the render thread is cpu intensive and time sensitive. In an attempt to achieve the best performance possible, I am reserving exclusive cores for the process by calling:
int exclusiveCores[] = {};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
exclusiveCores = android.os.Process.getExclusiveCores();
}
then sending the int array on through the JNI to be handled by the engine.
This has worked fine until recently when I received a crash report stating a RuntimeException was thrown on the getExclusiveCores() call. The device was a Samsung J7 (SM-J727T) running Android 7.0. This is the only device so far that has given me this report.
Has anyone else experienced this issue? Is this API not available for this specific device? Is there another limitation I should be checking for before calling getExclusiveCores?
The documentation states:
To support an exclusive core on a device:
Enable cpusets and configure a cpuset that contains only the top foreground application.
Ensure one core (this is the exclusive core) is reserved for threads from this cpuset.
but I am unable to find any other documentation on how to enable cpusets and configure a cpuset.
Can anyone provide an example of this implementation or point me in the right direction?
Also, I personally do not have this device and Firebase Testlab does not have this device running android 7.0 so testing any solution will have its own complications.
Thanks
I also ran into this on a Samsung Galaxy S6 running Android 7.0.
Unfortunately it looks like getExclusiveCores isn't implemented on some devices (despite being a mandatory API, specified in the Android CDD).
The best advice is to wrap calls to this method in a try/catch block and catch the RuntimeException:
try {
exclusiveCores = android.os.Process.getExclusiveCores();
} catch (RuntimeException e){
Log.w(TAG, "getExclusiveCores() is not supported on this device.");
}

Geocoder.getFromLocation grpc failed on Android real device

I need to get the city and state from latitude and longitude. Geocoder does this function to get the city and state. But I am getting an error java.io.IOException: grpc failed in below line only on Android real device. It is working fine in emulators.
new Geocoder(context).getFromLocation(latLng.latitude, latLng.longitude, 1).get(0);
Also I have been calling this function using Asynctask which is suggested in another post Geocoder grpc failed in stack overflow but nothing works for me.
And I have restart the device as well but still it is failing.
Help needed here. Thanks in advance.
Note: I tested in Google Pixel XL, Samsung S6 edge, Samsung S4.
It looks like this is ongoing issue that was reported in the Google issue tracker both for real devices and emulators. You can refer to the following bugs:
https://issuetracker.google.com/issues/64418751
https://issuetracker.google.com/issues/64247769
Unfortunately, Google haven't solved these issues yet.
As a workaround you can consider using the Geocoding API web service. Please note that there is a Java client library for web services that you can find on Github:
https://github.com/googlemaps/google-maps-services-java
Using Java client library for web services you can implement reverse geocoding lookup that shouldn't give you the error that you experience with native Android geocoder.
The Javadoc for client library is located at
https://googlemaps.github.io/google-maps-services-java/v0.2.5/javadoc/
I hope this helps!
Check your internet connection .
what is going on is at some point between the search and the results rendering , the connection is either to slow or not working at all . so the default error is no GRCP found. your code needs to handle this type of errors and prevent the crash of the map
Happened for me both with a real and an emulated device. Solved my restarting the app after cleaning the project
For me, it was the device time. I was doing some tests, and I had to change the device time. The device time must be set correctly, otherwise you get this error.
It is a reported bug in Android, check #xomena answer for the link.
Alternately, you can use the web services. Add this to gradle dependencies:
api 'com.google.maps:google-maps-services:0.10.2'
Sample code to get addresses:
val geoApiCtx = GeoApiContext.Builder()
.apiKey(Util.readGoogleApiKey(ctx))
.build()
val addresses = GeocodingApi.reverseGeocode(geoApiCtx, loc).await()
When I run with emulator I got the same problem.
The problem has solved after i run with real device.
Making the call to fetch an address via geocoder is recommended to do via an IntentService....but if you're in rapid development mode and don't want to bother setting up a service just yet, you can use a try/catch block
example in Kotlin:
try{
val addressList = geocoder.getFromLocationName(locationName, 3)
//...
}catch(e: IOException) {
when{
e.message == "grpc failed" -> {/* display a Toast or Snackbar instead*/ }
else -> throw e
}
}
What I did was, I changed my accuracy to GPS only, in this case, the application was not calculating my location. I again changed the accuracy to high accuracy and the application started working, no crash at all. you can change your GPS accuracy from location setting on your android mobile.
Using an emulator. I had to update the Google Play Services on the device and then it worked!

Angularjs throws "Unexpected strict mode reserved word" which causes application to crash

I am currently developing an application for mobile phones using angularJS and cordova 3.2.
Some of my players report, that the application is not starting on their devices. This devices are listed below.
optimus p880 (Android Version 4.0.3)
Lifetab von Medion (Adroid Version 4.0.3)
Sony Ericsson Xperia mini pro ( Android 4.0.4)
So it seems somehow connected to that Android Version 4.0.3/4.
Not starting means, that the cordova application is starting, but that angularjs crashes before it is initiated, because all the elements where an ng-cloak class is attached (which in my case is set for the complete root window) remain hidden. So the user does not see anything.
The problem is, that I don't have any of this devices to test it, but one player did send me the logs of his device, saying
Uncaught SyntaxError: Unexpected strict mode reserved word
With the line number and the file name I could determine, that the problem is caused by the code below (its from the angular.js v1.2.16 file in line 3878). The problem seems to apply in the statement throw err;
(#3878)
function createInternalInjector(cache, factory) {
function getService(serviceName) {
if (cache.hasOwnProperty(serviceName)) {
if (cache[serviceName] === INSTANTIATING) {
throw $injectorMinErr('cdep', 'Circular dependency found: {0}', path.join(' <- '));
}
return cache[serviceName];
} else {
try {
path.unshift(serviceName);
cache[serviceName] = INSTANTIATING;
return cache[serviceName] = factory(serviceName);
} catch (err) {
if (cache[serviceName] === INSTANTIATING) {
delete cache[serviceName];
}
throw err; /*** THE ERROR APPLIES HERE! **/
} finally {
path.shift();
}
}
}
I cannot really say, what happens here. The applications works fine on other devices and other android versions.
Has anyone of you an idea how I could fix that issue for the players?
For us this is being thrown only on Android 4.0.4 Galaxy S2 Stock Browser (and app web view) and can be resolved by removing 'use strict;' from our app.js.
I tracked down the offending code by console.logging the value fn.toString().substring(0,150) in the angular.js annotate function, and inspecting the logs in Weinre after hitting about:debug in the Stock Browser, which enables the console on the Stock Browser. Might help you track the problematic line in your own code. It might be that not everyone gets the error at the annotate() step. You should see the line in the console log.
Galaxy S3 with the exact same OS and browser is fine... go figure.
Still looking for a way to have the 'use strict'; there and not get the error. Wrapping the whole thing in an IIFE didn't do the trick and still getting the error in that case.

AIR for Android checking internet connection issue ( actionscript 3 )

I'm developing a simple app for my school which will show you the newest updates from the website, but checking the internet connection seems a little problem in AIR.
I'll be more specific:
I'm using Adobe Flash CS6 to develop the app (Using AIR for android)
The app contains a menu, and the "internet frame", of which the code doesn't really work.
I've tried a couple of things already: URLMonitor(like is available = true), URLLoader(and cathing the IOError if it occurs when there's no internet connection) and something like HTMLLoader (long time ago :P, no success)
The URLLoader works "fine", but I also need to know if the webpage just isn't available (404) when there IS an active internet connection, but in either those cases it will just throw the same "stream error" , so I can distinguish them. That's why it doesn't suit my needs.
The URLMonitor also works "fine", but here comes to problem:
When testing the app on the emulator, it can without a problem detect that there is no internet connection. BUT, when exporting to .apk and running on my android device, it won't succeed in detecting the internet connectivity.
Here's my code:
var monitor:URLMonitor = new URLMonitor(new URLRequest("http://www.google.nl"));
monitor.addEventListener(StatusEvent.STATUS, netConnectivity);
monitor.start();
function netConnectivity(e:StatusEvent):void
{
if(e.target.available)
{
//checking the content
output_txt.text += "\ninternet available";
loader = new URLLoader(new URLRequest(webURL));
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorLoader);
loader.addEventListener(Event.COMPLETE, onCompleteLoader);
}
else
{
//
//
// NO INTERNET CONNECTION AVAILABLE!
//
//
output_txt.text += "\nno internet available";
popupnoconnection_mc.alpha = 100;
popupnoconnection_mc.play();
popupnoconnection_mc.addEventListener(MouseEvent.MOUSE_DOWN, BackToMenu);
}
monitor.stop();
}
As you can see, it adds an EventListener to the URLMonitor, starts the checking, and the function checks the availability of the internet connection. WORKS ON THE EMULATOR, NOT PROPERLY ON MY DEVICE.
What's even more interesting:
What DOES work on my device, is when the internet IS connected. It will just show the text in my output field, but it won't when there is no internet connection!
Also, when lauching the app with an internet connection set up, then entering this frame, then going back to the main menu, then turning the internet off, and then entering this frame again, this code suddently works great!
So it has got to do something with the creation of the URLMonitor, and checking the connection at the same frame I guess, but I need a little hand here!
I think this can't be too difficult!
Thank you so much in advance! (sorry for typos)
You can use NetworkInfo to detect whether there is an internet connection - it works on Android.

Categories

Resources