My Xamarin cross-platform app works well in debug mode (Android), but when I change to release, it throw exceptions. I've already searched to correct for this situation. Most of posts reveal that android.permission.INTERNET
needs to be added in AndroidManifest.xml file. But this doesn't work for my problem.
Additional, regardless Linker is Sdk Assembly only or None in release mode, it still don't work. (My app build on Android Pie 9.0)
And exceptions:
Unhandled Exception:
04-21 22:37:08.972 E/mono ( 5319): System.NullReferenceException: Object reference not set to an instance of an object.
04-21 22:37:08.972 E/mono ( 5319): at .Views.LoginPage+<SigninProcedure>d__2.MoveNext () [0x001d6] in LoginPage.xaml.cs:56
04-21 22:37:08.972 E/mono ( 5319): --- End of stack trace from previous location where exception was thrown ---
04-21 22:37:08.972 E/mono ( 5319): at (wrapper dynamic-method) System.Object.31(intptr,intptr)
04-21 22:37:08.972 E/mono ( 5319): at (wrapper native-to-managed) System.Object.31(intptr,intptr)
04-21 22:37:08.973 E/mono-rt ( 5319): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
04-21 22:37:08.973 E/mono-rt ( 5319): at Views.LoginPage+<SigninProcedure>d__2.MoveNext () [0x001d6] in LoginPage.xaml.cs:56
<uses-permission android:name="android.permission.INTERNET" />
Below is my code when it throws exception:
public async Task<List<User>> FindExistUserAsync(string weburl, string username)
{
string url = weburl + username;
try
{
var response = await client.GetAsync(url);// throw exception after run this line.
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var JsonResult = response.Content.ReadAsStringAsync().Result;
try
{
//Debug.WriteLine(JsonResult);
var contentResp = JsonConvert.DeserializeObject<List<User>>(JsonResult);
return contentResp;
}
catch (Exception ex) { Debug.WriteLine(ex.Message); return null; }
}
}
catch (Exception ex) { Debug.WriteLine(ex.Message); return null; }
return null;
}