( i use visual studio 2015 Xamarin )
this is the message that i see after few seconds that my android app starts:
" Android.Util.AndroidRuntimeException: Only the original thread that created a view hierarchy can touch its views. "
-My app should should write continuously current date and time in a TextView.
this is the code i have written.. where is the error? how can i fix it?
namespace App4
{
[Activity(Label = "app4", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
private delegate void delegato();
private delegato del1;
public MainActivity()
{
del1 = getdatehour;
}
private void Do()
{
del1();
}
private void getdatehour()
{
TextView datehour = (TextView)FindViewById(Resource.Id.textView1);
DateTime date;
while (true)
{
date = DateTime.Now;
String s = "DATA E ORA: " + date;
datehour.Text = s;
}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
Thread thdo = new Thread(Do);
thdo.Start();
}
}
}
Related
There was a problem parsing the package while installing .apk file.
Given below is configuration and code.
VS2015
MainActivity.cs
namespace WebAp
{
[Activity(Label = "TC", MainLauncher = true, Theme ="#android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
private WebView web_view;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
web_view = FindViewById<WebView>(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.LoadUrl("http://www.tc.com");
web_view.SetWebViewClient(new HelloWebViewClient());
}
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return true;
}
}
public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Android.Views.Keycode.Back && web_view.CanGoBack())
{
web_view.GoBack();
return true;
}
return base.OnKeyDown(keyCode, e);
}
}
}
Configurataion :
enter image description here
Thats it my code. I was also try different API for Compile, Min and target version.
Please help me thanks.
I wanna point out that i'm a total newbie in Xamarin.
My goal is to create simple app that after you push the button, it will jump to another layout. My problem is, that after i created a new layout(called layout1) and new activity, I can't reference to this layout in my MainActivity.
freshly created Activity:
namespace Layouts
{
[Activity(Label = "Activity1")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
}
}
}
And my MainActivity:
namespace Layouts
{
[Activity(Label = "Layouts", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView (Resource.Layout.Main);
Button button1 = FindViewById<Button>(Resource.Id.button);
button1.Click += delegate
{
StartActivity(typeof(layout1)); //can't reference layout1 there
};
}
}
}
Any help will be great, thanks :)
StartActivity(typeof(Activity1));
Im using xamarin for android and i have looked into this issue but nothing seems to work.
Im trying to get a webview to work with a button but it keeps saying whenever i click on the button.
nativeOnDraw failed; clearing to background color.
I will include image of my layout and my code here.
MainActivity
namespace Application
{
[Activity(Label = "Application", MainLauncher = true, Icon = "#drawable/icon", Theme = "#android:style/Theme.NoTitleBar")]
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);
WebPage webPage = new WebPage();
Button btn_MySchedule = FindViewById<Button>(Resource.Id.btnSchedule);
Button btn_MyCareer = FindViewById<Button>(Resource.Id.btnCareer);
Button btn_MySocial = FindViewById<Button>(Resource.Id.btnSocial);
Button btn_Feedback = FindViewById<Button>(Resource.Id.btnFeedback);
Button btn_MyDetails = FindViewById<Button>(Resource.Id.btnDetails);
btn_MySchedule.Click += delegate
{
webPage.isSchedule = true;
//wPage.SetUrl("https://www.google.com");
StartActivity(typeof(WebPage));
};
}
}
}
WebPage
namespace Application
{
[Activity(Label = "WebPage", Theme = "#android:style/Theme.NoTitleBar")]
public class WebPage : Activity
{
public bool isSchedule;
private WebView _webView;
private string _url;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.WebPage);
_webView = FindViewById<WebView>(Resource.Id.webView);
new DisplayWebPage(_webView, GetUrl());
}
public string GetUrl()
{
return _url;
}
public void SetUrl(string sUrl)
{
_url = sUrl;
}
private void ButtonCLicked()
{
if (isSchedule == true)
{
SetUrl("https://www.google.com");
Toast.MakeText(this, "isSchedule is true", ToastLength.Short).Show();
}
}
}
}
I have found the issue. It was due to me calling an empty string via the GetUrl() methods.
The Solution I used is:
[Export("MyScheduleClicked")]
public void MyScheduleClicked(View v)
{
var activity2 = new Intent(this, typeof(WebPage));
activity2.PutExtra("MyURL", "https://www.google.com");
StartActivity(activity2);
}
I'm starting my first Xamarin.Forms app in Visual Studio 2015 on Windows.
My StartUp project is firstApp.Droid.
In MainActivity.cs in firstApp.Droid I have this code:
namespace firstApp.Droid
{
[Activity (Label = "firstApp", 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);
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new firstApp.App ());
}
}
}
In App.cs I have this code:
public App ()
{
// The root page of your application
MainPage = new NavigationPage(new SensorPage());
}
And in SensorPage.cs I have this auto-generated code:
public class SensorPage : ContentPage
{
public SensorPage ()
{
Content = new StackLayout {
Children = {
new Label { Text = "Hello SensorPage" } //line that causes the error
}
};
}
}
When I try to compile the app, I get the error:
CS0104 'Label' is an ambiguous reference between 'Xamarin.Forms.Label' and 'System.Reflection.Emit.Label'
What should I do to resolve this?
either remove using System.Reflection.Emit; from the top of your SensorPage.cs, or alternately use an explicit namespace for Label:
Content = new StackLayout {
Children = {
new Xamarin.Forms.Label { Text = "Hello SensorPage" }
}
};
How can I implement a simple "splash screen" on program startup? I am copying a SQLite DB and it can be a bit of a long process that is not UI "friendly" .
I would prefer not to use "java code".
TIA
I recently solved this problem in the following way.
In the main activity I passed a parameter via the intent to set the number of milliseconds for which the splash screen would remain visible.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Intent i=new Intent();
i.SetClass(this, typeof (Splash));
i.PutExtra("Milliseconds", 3000);
StartActivity(i);
}
Then, in the second activity which I named "Splash" I retrieved the value and set a second thread to end the activity when the time had elapsed.
[Activity(Label = "Daraize Tech")]
public class Splash : Activity
{
private int _milliseconds;
private DateTime _dt;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_milliseconds = Intent.GetIntExtra("Milliseconds", 1000);
SetContentView(Resource.Layout.Splash);
_dt=DateTime.Now.AddMilliseconds(_milliseconds);
}
public override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
new Thread(new ThreadStart(() =>
{
while (DateTime.Now < _dt)
Thread.Sleep(10);
RunOnUiThread( Finish );
}
)).Start();
}
}
Also see http://docs.xamarin.com/android/tutorials/Creating_a_Splash_Screen
Really great tutorial.
It only takes about 10 lines of code :)
In a Styles.xml:
<resources>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">#drawable/splash</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
In your activity:
[Activity (MainLauncher = true, Theme = "#style/Theme.Splash", NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your async task here...
StartActivity (typeof (Activity1));
}
}
This worked for me:
Starting an new thread from the splash activity. You can wait a few seconds or load some data or something else.
[Activity(MainLauncher = true, NoHistory = true)]
public class Splashscreen : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.splashscreen);
new Thread (new ThreadStart (() =>
{
//Load something here ...
Thread.Sleep(1500);
Intent main = new Intent (this, typeof(MainActivity));
this.StartActivity (main);
this.Finish ();
})).Start ();
}
}
This solution gets you the following:
Immediate display of the splash screen
Removal of splash screen the exact time the "main" activity is launched (main activity replaces the splash activity)
In OnCreate, SetContentView is called to get the splash screen up, and then the worker thread is kicked off, which runs the slow processing data initialization stuff.
This way, the spalsh screen is displayed without delay. The last statement in the worker thread kicks off the "main" app/activity, which will have its DB & data all ready for access. Calling StartActivity() from OnCreate (ie, after initializeDataWorker.Start()), will cause MainActivity to run before/while the DB is being created and/or data is being fetched, which is usually not desireable).
This solution lacks a way to remove the splash screen from the backstack. When I get around to implementing this functionality I'll update it.
namespace Mono.Droid
{
[Activity(
Label = "Splash Activity",
MainLauncher = true,
Theme = "#android:style/Theme.Black.NoTitleBar",
Icon = "#drawable/icon",
NoHistory = false)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.SplashLayout);
Thread initializeDataWorker = new Thread(new ThreadStart(InitializeData));
initializeDataWorker.Start();
}
private void InitializeData()
{
// create a DB
// get some data from web-service
// ...
StartActivity(typeof(MainActivity));
}
}
}