I am using Xamarin I am wanting to start a new activity that is called AutoLinkActivity.
Here is my code:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Text.Util;
namespace TestTextViewAutoLink
{
[Activity (Label = "TestTextViewAutoLink", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
Intent intent= new Intent(this.ApplicationContext, AutoLinkActivity);
intent.SetFlags(ActivityFlags.NewTask);
StartActivity(intent);
}
}
}
The build error that I am getting is:
'TestTextViewAutoLink.AutoLinkActivity' is a 'type' but is used like a 'variable'
May I please have some help to get this working?
Thanks in advance
Use this:
Intent intent= new Intent(this.ApplicationContext, typeof(AutoLinkActivity));
the second parameter has to be the type of the activity class, not the class itself.
You have to use:
typeof(NameofyourActivity)
Try this if you don't want use intent and start it directly:
protected override void OnCreate (Bundle bundle)
{
StartActivity(typeof(AutoLinkActivity));
}
Related
I made a simple program, which include TextView and Button; and every time the Button is clicked, the counter on TextView grows by 1.
I also wanted that every time I click the Button, toast message will be seen.
I opened a new item in Visual Studio to do it :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V7.App;
namespace perek1_helek1_dug1_listening
{
class Game_code : Button, Android.Views.View.IOnClickListener
{
private int point = 0;
private TextView screen;
public Game_code( TextView screen, Context context):base(context)
{
this.screen = screen;
}
public static void ShowToastMethod(Context context)
{
Toast.MakeText(context, "mymessage ", ToastLength.Long).Show();
}
public void OnClick(View v)
{
if (v.Id == Resource.Id.btnxml)
{
point++;
**ShowToastMethod( context);**
}
screen.Text = "" + point;
}
}
}
That is How MainActivity.cs looks like:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
namespace perek1_helek1_dug1_listening
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
TextView tv;
Button btn;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
tv = (TextView)FindViewById(Resource.Id.textboxml);
btn = (Button)FindViewById(Resource.Id.btnxml);
Game_code game = new Game_code(tv, this);
btn.SetOnClickListener(game);
}
}
}
However, the line :
ShowToastMethod( context);
makes the error. What should I do?
I would suggest you use the events that C# has provided:
int point=0;
.
.
.
//after you find button view by ID
btn.Click+= delegate {
tv.Text=point++;
ShowToastMethod();
};
public void ShowToastMethod()
{
Toast.MakeText(this, "mymessage ", ToastLength.Long).Show();
}
I googled around and found that all I had to do is change the line :
ShowToastMethod( context);
to:
ShowToastMethod(this.Context );
Thank you anyway.
using Android.App;
using Android.Widget;
using Android.OS;
using System;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using System.Configuration;
namespace googlemaps
{
[Activity(Label = "googlemaps", MainLauncher = true, Icon = "#drawable/icon")]
MainActivity
public class MainActivity: Activity, IOnMapReadyCallback
{
private GoogleMap GMap;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.XMLFile1);
SetUpMap();
}
private void SetUpMap()
{
if (GMap == null)
{
MapFragment mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map).GetMapAsync(this);
}
}
public void OnMapReady(GoogleMap googleMap)
{
MarkerOptions MarkerOptions = new MarkerOptions();
MarkerOptions.SetPosition(new LatLng(16.03, 108));
MarkerOptions.SetTitle("My Postion");
googleMap.AddMarker(MarkerOptions);
googleMap.UiSettings.ZoomControlsEnabled = true;
googleMap.UiSettings.CompassEnabled = true;
googleMap.MoveCamera(CameraUpdateFactory.ZoomIn());
}
}
}
and
'Fragment' does not contain a definition for 'GetMapAsync' and no extension method 'GetMapAsync' accepting a first argument of type 'Fragment' could be found (are you missing a using directive or an assembly reference?) googlemaps
Thank you in advance
Your method "OnMapReady" needs to override the base class's method.
public override void OnMapReady(GoogleMap gmap)
{
//dostuff
}
I'm not 100% because I've never done this specifically, but I'm fairly certain it's complaining about this callback method.
I used this to find this out. This is the native Java for android development, but it mostly translates pretty well over into Xamarin from experience.
My Android service closing when I close the main activity.
I tried add to the AndroidManifest.xml: <service android:name="App7.SimpleService" android:stopWithTask="false">
I also tried the following code for starting service:
Intent intent2 = new Intent(ApplicationContext, typeof(SimpleService));
StartService(intent2);
and
StartService(new Intent("App7.SimpleService"));
I tried also the return START_STICKY; and return StartCommandResult.Sticky;, but no effects.
My code:
SimpleService.cs:
using System;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Util;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using Android.Widget;
using Android;
using App7;
using static System.Net.Mime.MediaTypeNames;
using System.Threading.Tasks;
using System.Text;
namespace SimpleService
{
[Service]
[IntentFilter(new String[] { "App7.SimpleService" })]
public class SimpleServiceBinder : Service
{
public static StartCommandResult START_STICKY { get; private set; }
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
/// My service code
return StartCommandResult.Sticky;
}
public override IBinder OnBind(Intent intent)
{
// binder = new SimpleService(this);
return null;
}
public override void OnDestroy()
{
base.OnDestroy();
}
}
}
MainActivity.cs:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using App7;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using Android.Webkit;
using System.Threading.Tasks;
namespace SimpleService
{
[Activity(Label = "aplikacja1", MainLauncher = true)]
[Service]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//////////////////////////////////////
this.StartService(new Intent(this, typeof(SimpleServiceBinder)));
/// I tried also////
// Intent intent2 = new Intent(ApplicationContext, typeof(SimpleService));
// StartService(intent2);
// StartService(new Intent("App7.SimpleService"));
}
}
}
Please, help me
My Xamarin.Forms app used to work, but for some reason it just stopped working. This is the error I get:
Argument #1 cannot convert 'BadgerBand.Droid.App' expression to type 'Xamarin.Forms.Application' (CS1503)
I am also getting a CS1502 error, which is:
The best overloaded method match for `Xamarin.Forms.Platform.Android.FormsApplicationActivity.LoadApplication(Xamarin.Forms.Application)' has some invalid arguments (CS1502) (BadgerBand.Droid)
Does anyone know how I can fix this?
using System;
using BadgerBand;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace BadgerBand.Droid
{
[Activity (Label = "BadgerBand.Droid", Icon = "#drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
//BadgerBand.Init(typeof(BadgerBand).Assembly);
global::Xamarin.Forms.Forms.Init (this, bundle);
//LoadApplication (new Xamarin.Forms.Application ());
LoadApplication (new App ());
}
}
}
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Media;
namespace MoviePlayer
{
[Activity(Label = "MoviePlayer", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity
{
int count = 1;
MediaPlayer mp;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mp = new MediaPlayer();
mp.Prepare();
mp.SetDataSource()
mp.Start();
}
}
}
I don't know what should be in the mp.SetDataSource()
I looked on examples but didn't understand.
You need to pass a path to the file you want to play as a string to the SetDataSource() method to play that file.
Mono Documentation
Android Documentation