I have a UserHandler Singleton Class and I declared a variable like this
class UserHandler
{
...
public DatePickerDialog dialog = null;
...
}
Then in my MainActivity.cs file
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Text;
using Android.Views;
using Android.Widget;
using Java.Interop;
using Java.Lang;
using System;
using System.Collections.Generic;
using System.Globalization;
using AlertDialog = Android.Support.V7.App.AlertDialog;
using Exception = System.Exception;
class MainActivity
{
private readonly UserHandler U_Handler = UserHandler.GetInstance;
...
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
...
U_Handler.dialog = new DatePickerDialog(this);
...
}
When I run it on my device (Oppo, Android 8.1.0), it works normally but when I tried it on other device (Samsung, Android 6.0.1), it crashes and upon debugging, it shows this
**Java.Lang.NoSuchMethodError:** 'no non-static method "Landroid/app/DatePickerDialog;.<init>(Landroid/content/Context;)V"'
How to solve this?
Should I use 3rd party Libraries?
Because the constructor DatePickerDialog(Context context) was added in API level 24,when you run it in Samsung, Android 6.0.1,it will throw the Java.Lang.NoSuchMethodError exception.
You can make a judgment call and use the old API when the SDK version is less than 24, and use the new API when it is greater than 24.
The api you could look at DatePickerDialog
Related
I would like to open Unity Android application from the setting panel within the custom canvas view.
Can I get a code snippet to bring it within canvas view rather than opening over the whole window?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;
using System.IO;
using System;
public class GetSetting : MonoBehaviour
{
void Start()
{
#if UNITY_ANDROID
string bundleId = "com.android.settings";
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
//if the app is installed, no errors. Else, doesn't get past next line
AndroidJavaObject launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
launchIntent.Call<AndroidJavaObject>("addCategory", "android.intent.category.MAIN");
launchIntent.Call<AndroidJavaObject>("addCategory", "android.intent.category.HOME");
launchIntent.Call<AndroidJavaObject>("setFlags", 0x10000000);
ca.Call("startActivity", launchIntent);
up.Dispose();
ca.Dispose();
packageManager.Dispose();
launchIntent.Dispose();
#else
Debug.Log("No Toast setup for this platform.");
#endif
}
}
I am currently busy with a Xamarin mobile application (only android now) and I am trying to show PDF files. I found a free plugin here: https://github.com/xamarin/docs-archive/tree/master/Recipes/xamarin-forms/Controls/display-pdf but I run into an issue.
When I go to the view I see the PDF controls, but not the PDF itself. (shows a gray screen). Here is a similar issue: display local PDF file in webview control - Displays Blank Pdf File but nothing is working.
I put the PDF file in the correct place with the correct options, see here:
And here you can see the view in the emulator:
I also tried the run the given demo project, but I get a lot of errors :(
Some help would be really appreciated!
CustomView:
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace BizzTimeTest.ViewModels
{
public class CustomPDFWebView : WebView
{
public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri",
returnType: typeof(string),
declaringType: typeof(CustomPDFWebView),
defaultValue: default(string));
public string Uri
{
get { return (string)GetValue(UriProperty); }
set { SetValue(UriProperty, value); }
}
}
}
Custom renderer:
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using BizzTimeTest.Droid;
using BizzTimeTest.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomPDFWebView), typeof(CustomWebViewRenderer))]
namespace BizzTimeTest.Droid
{
[Obsolete]
public class CustomWebViewRenderer : WebViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var customWebView = Element as CustomPDFWebView;
Control.Settings.AllowUniversalAccessFromFileURLs = true;
Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///android_asset/Content/{0}", "test_pdf_file.pdf")));
}
}
}
}
xaml
<local:CustomPDFWebView x:Name="custom_pdf_viewer" IsVisible="false"
Uri="test_pdf_file.pdf" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
I am using Xamarin android
I have Spinner , When I use the setAdapter function I get this error
Error 3 'Android.Widget.Spinner' does not contain a definition for 'setAdepter' and no extension method 'setAdepter' accepting a first argument of type 'Android.Widget.Spinner' could be found (are you missing a using directive or an assembly reference?)
This is my code
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;
namespace FSF
{
[Activity(Label = "Request")]
public class RequestHolidayActivity : Activity
{
Spinner spinner_holidayType;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.RequestHoliday);
spinner_holidayType = FindViewById<Spinner>(Resource.Id.RequestHoliday_spinner_holidayT);
var items = new List<string>() { "first", "second", "third", "forth" };
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, items);
spinner_holidayType.setAdepter(adapter);
}
}
}
I tried to solve it , but I didn't find a solution.
There is not SetAdapter method. Instead use the Adapter property and change your code to: spinner_holydayType.Adapter = adapter;
I created a WCF service in Visual Studio 2012 that I intend to consume in Xamarin and have it save user registration information to a table in SQL server. The service works fine when debugging in Visual Studio, the data entries actually save to my table in SQL. I want to consume the service in Xamarin and here is my code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ItusService;
using Android.App;
using Android.Locations;
using Android.OS;
using Android.Util;
using Android.Widget;
//some code here...
public static readonly EndpointAddress EndPoint = new EndpointAddress("http://192.168.1.155:12100/Service1.svc");
private Service1Client service;
protected override void OnCreate (Bundle bundle)
{
//some code here...
InitializeService1Client();
}
private void InitializeService1Client()
{
BasicHttpBinding binding = new BasicHttpBinding ();
service = new Service1Client(binding, EndPoint);
}
async void AddressButton_OnClick(object sender, EventArgs eventArgs)
{
//some code here...
UserLocation useraddress = new UserLocation ();
useraddress.ClientID = _client.Text = "ddsdfs";
useraddress.FullName = _name.Text = "dsffdf";
useraddress.LocationCoordinates = _locationText.Text;
useraddress.LocationAddress = _addressText.Text;
useraddress.DistressTime = _time.Text = "djdsk";
string result = service.ToString ();
_resultTxt.Text = result;
}
I don't know if there is something I am missing out because nothing is being saved to my SQL table from my Android device. I debug using both an emulator and my device, nothing has been saved using both. I have opened the port for listening, I have created a reference file using silverlight slsvc tool according to the Xamarin Walkthrough http://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/ I have tried everything I possibly can. Please help.
The general pattern for calling a WCF method is something like this (this is general WCF and really has nothing to do with Xamarin or Android)
// create the client and bind it
client = new ServiceClient(binding, endpoint);
// specify the callback
client.GetLocationCompleted += OnGetLocationCompleted;
// make the request to your method, passing any required parameters
client.GetLocationAsync(useraddress);
because the WCF call is async, you need to provide a callback method (the naming of the EventArgs class will depend on how your service is defined)
public void OnGetLocationCompleted(object sender, GetLocationCompletedEventArgs args) {
// if something bad happend, args.Error will contain an exception
// otherwise args.Result will contain any return value from you method
}
Using NuGet I added Unity to my Xamarin.Forms Application.
In my App class constructor I want to newup a UnityContainer instance.
using Xamarin.Forms;
public class App : Application
{
public App ()
{
unityContainer = new UnityContainer();
// The root page of your application
this.MainPage = new NavigationPage(new HomePage());
}
private static IUnityContainer unityContainer;
}
Unfortunately a System.Reflection.TargetInvocationException. Is thrown on the line unityContainer = new UnityContainer(); as I start to run my app in the android emulator.
What could be the reason to this?