1

i'm trying to test the DependencyService of my app by displaying an alert dialog from an android project when i press a button i created in the share code project.

When i try to set the Context property in the 'AlertDialog.Builder' by entering 'this' in the parenthesis i get this error: "Cannot convert from 'CallDiverter2.Droid.CallDiverter_Android' to 'Android.Content.Context'.

Also the namespace is decorated with: '[assembly: Dependency(typeof(CallDiverter_Android))]' if its matter.

this is the function in the android project i want to call using the DependecyService:

public class CallDiverter_Android : ICallDiverter
{
    public void DivertCall(string callForwardString)
    {
        //Divert call code

        //String callForwardString = "**21*1234567890#";
        //Intent callIntent = new Intent(Intent.ActionDial); // ACTION_CALL
        //Android.Net.Uri uri = Android.Net.Uri.Parse(callForwardString);
        //callIntent.SetData(uri);
        //Android.App.Application.Context.StartActivity(callIntent);


        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        AlertDialog alert = dialog.Create();
        alert.SetTitle("Title");
        alert.SetMessage("Simple Alert");
        alert.SetButton("OK", (s, ev) =>
        {
            alert.SetMessage("This is an alert message");
        });

        alert.Show();
    }

    public void StopCallDiverting()
    {
        //Stop the call diverting action
    }
}

How should i fix this so i can successfully test the dependencyService?

MetaDude
  • 139
  • 5
  • 14
  • `new AlertDialog.Builder()` constructor requires a `Android.content.context` as a first parameter, while you passing `this`, thich is not a context. – Vladyslav Matviienko Jun 27 '18 at 08:09

4 Answers4

5

If you have a single activity application you could probably just get away with passing in

Android.App.Application.Context

Eg

AlertDialog.Builder dialog = new AlertDialog.Builder(Android.App.Application.Context);

However to get the current Activity Context in a Multiple Activity Application, then you will need to keep track of it

See this answer for how to keep track of the current context

https://stackoverflow.com/a/47363378/1612975

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thank you, your answer helped me to display thet alertDialog when i press the button. Although it says now that the 'Xamarin.Forms.Forms.Context' is obsolete – MetaDude Jun 27 '18 at 09:13
  • @MetaDude Sorry `Android.App.Application.Context` try this – TheGeneral Jun 27 '18 at 09:16
  • @TheGenetral Dont be its cool. I tried it but it threw an uncaught exception: 'unable to add window - token null is not for an application' error message, and kinda freeze my visual studio. your previous solution on the other hand did work. – MetaDude Jun 27 '18 at 09:31
  • @MetaDude is this a single activity app? – TheGeneral Jun 27 '18 at 09:37
  • In single activity you mean single window app? If so, then yes For now there is only one window at the moment – MetaDude Jun 27 '18 at 09:38
  • @MetaDude Android.App.Application.Context should work, and it shouldnt stall visual studio – TheGeneral Jun 27 '18 at 09:43
  • Alright i'll try it again. In the meanwhile i get an error in my ViewModel, it enter a breakpoint at the Command in the constructor. Something about 'Error while resolving expression: Object reference not set to an instance of an object. Hope it will work after i'll found out how to solve this new problam. Thank you any way! – MetaDude Jun 27 '18 at 09:58
  • @MetaDude for a more reliable way, follow that linq in the answer. also dont be worried about reopening the question if your problems arent solved or asking a new one – TheGeneral Jun 27 '18 at 09:59
  • Thank you! I'll sure be doing that if i need to :) – MetaDude Jun 27 '18 at 10:01
  • A little question i just thought about, should've i put this code in the MainActivity class in the Droid project in Xamarin or could i create new class in the Droid project and work on this there? – MetaDude Jun 27 '18 at 16:03
0

There is a trick in xamarin to do so. and most people also consider it very handy approach that is

create a static property of your MainActivity Class inside it (Singleton approach)

internal static MainActivity Instance { get; private set; }

Assign this Instance object value at the end of the construct of MainActivity like this

Instance = this;

Now use it like this to get context where ever you need

 AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.Instance);

This works well because most of the xamarin forms app have only one activity i.e. MainActivity

Harsh Chaurasia
  • 109
  • 1
  • 6
  • I tried your solution but when i started the app and clicked the button it threw a bunch of error messages and did not displayed the alertDialog popup. – MetaDude Jun 27 '18 at 09:25
  • will you please share the error you are getting. and how you are invoking DivertCall method – Harsh Chaurasia Jun 27 '18 at 10:05
0

this does not work, because your class does not inherit from the Activity/Context class. Therefore this cannot be converted to Android.Content.Context

Just use Android.App.Application.Context to get the current Android Context.

Csharpest
  • 1,258
  • 14
  • 32
  • Hey, i tried this like TheGeneral proposed earlier but it freeze the visual studio and was unable to display the Aler window. It says in the LivePlayer Debug log: 'Unable to add window - token null is not for an application' – MetaDude Jun 27 '18 at 09:34
  • Try calling it from the UI Thread like this `Device.BeginInvokeOnMainThread(() => DependencyService.Get....)` – Csharpest Jun 27 '18 at 09:46
  • I'll try it. In the meanwhile i get an error in my ViewModel, it enter a breakpoint at the Command in the constructor. Something about 'Error while resolving expression: Object reference not set to an instance of an object. Hope it will work after i'll found out how to solve this new problam. Thank you any way! – MetaDude Jun 27 '18 at 09:58
  • Good luck, I think you will have to fix the command problem on your own, otherwise open a new question. – Csharpest Jun 27 '18 at 10:50
0

I just had this error and fixed it. This question is pretty old now but I put my insight for any person meeting the issue.

It turns out it did not like me to put my classes to work with SQLite into the AppName.Android.Persistance namespace instead of AppName.Droid.Persistance.

I just put Droid back and the error has gone.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
UchiTesting
  • 111
  • 8