C
C#17mo ago
uselessxp

❔ Xamarin.Forms Android App problem using Application.Context.StartActivity(intent)

Hi, in short, I created a super small Xamarin.Forms Android App. All I need is just to press a button, that will launch another application. Then I immedaitely added the button in the myPage.xaml file in the shared Xamarin.Forms project. About the myPage.xaml.cs file this is the code I binded to the button:
private void OnMyButtonClicked(object sender, EventArgs e)
{
DependencyService.Get<IAppLauncher>().LaunchApp("com.android.chrome", "test");
}
private void OnMyButtonClicked(object sender, EventArgs e)
{
DependencyService.Get<IAppLauncher>().LaunchApp("com.android.chrome", "test");
}
I defined the interface into App.xaml.cs (still shared project) using
public interface IAppLauncher
{
void LaunchApp(string packageName, string data);
}
public interface IAppLauncher
{
void LaunchApp(string packageName, string data);
}
Then I implmented it in the Android project by adding some code into MainActivity.cs
namespace AppExample1.Droid
{
public class AppLauncherImplementation : IAppLauncher
{
public void LaunchApp(string packageName, string data)
{
var uri = Android.Net.Uri.Parse($"appexample2://data/{data}");
var intent = new Intent(Intent.ActionView, uri);
intent.SetPackage(packageName);

var context = Android.App.Application.Context;
context.StartActivity(intent);
}
}
}
namespace AppExample1.Droid
{
public class AppLauncherImplementation : IAppLauncher
{
public void LaunchApp(string packageName, string data)
{
var uri = Android.Net.Uri.Parse($"appexample2://data/{data}");
var intent = new Intent(Intent.ActionView, uri);
intent.SetPackage(packageName);

var context = Android.App.Application.Context;
context.StartActivity(intent);
}
}
}
The problem is that every time I click the button, for some reason that I don't know, I always get error System.NullReferenceException: 'Object reference not set to an instance of an object.'
3 Replies
Accord
Accord17mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Grin
Grin16mo ago
I think this has something to do with
// Set the main layout for the activity
SetContentView(Resource.Layout.activity_main);
// Set the main layout for the activity
SetContentView(Resource.Layout.activity_main);
I could be wrong, but I've found that including layouts within my activity_main xml file this error dissappears.
<include
android:id="@+id/id_SubStationMenu"
layout="@layout/locationmenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<include
android:id="@+id/id_SubStationMenu"
layout="@layout/locationmenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server