I need to load a string into a textview that is being displayed on a new page after a button click event. A java.lang.runtimeexception keeps getting thrown when i click the button. "java.lang.reflect.InvocationTargetException"
Here is that section of my code:
SetContentView (Resource.Layout.Main);
Button button1 = FindViewById<Button>(Resource.Id.story1);
button1.Click += delegate {
SetContentView (Resource.Layout.next1);
TextView textvtest = (TextView) FindViewById(Resource.Id.textView1);
textvtest.Text = "test";
};
From the first activity, in buttonClick :
button1.Click += delegate {
var activity2 = new Intent (this, typeof(Activity2));
activity2.PutExtra ("MyData", "test");
StartActivity (activity2);
};
In the second activity, in onCreate() :
protected override void OnCreate (Bundle bundle)
{
SetContentView (Resource.Layout.next1);
TextView textvtest = (TextView) FindViewById(Resource.Id.textView1);
string text = Intent.GetStringExtra ("MyData") ?? "Data not available";
textvtest.Text = text;
}
I don't know if this matters, but other examples of setting textviews with C# include
(object sender, EventArgs e)
after
delegate
like
delegate(object sender, EventArgs e)
your code was a little bit wrong, try this
SetContentView (Resource.Layout.Main);
Button button1 = FindViewById<Button>(Resource.Id.story1);
button1.Click += delegate {
SetContentView (Resource.Layout.next1);
TextView textvtest = FindViewById<TextView>(Resource.Id.textView1);
textvtest.Text = "test";
};
Related
ex: first time: i put 18001090 and i receive : 18001090
second time: i put 056113 but i receive : 18001090
I put extra
Intent notificationIntent = new Intent(context, typeof(MainActivity));
notificationIntent.SetAction("android.intent.action.MAIN");
notificationIntent.PutExtra("number", incomingNumber);
On MainActivity
var number = this.Intent.GetStringExtra("number");
I use Intent to put extra and get extra, but I don't get the result as you, here is my code.
I add EditText and Button on layout5 and TextView on layout6.
Mainactivity:
private EditText edittext;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout5);
btn1 = FindViewById<Button>(Resource.Id.button1);
edittext = FindViewById<EditText>(Resource.Id.edittext1);
btn1.Click += Btn1_Click1;
}
private void Btn1_Click1(object sender, EventArgs e)
{
Intent i = new Intent(this, typeof(Activity1));
//Add PutExtra method data to intent.
i.PutExtra("Name", edittext.Text.ToString());
//StartActivity
StartActivity(i);
}
Avtivity1:
private TextView text;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout6);
// Create your application here
string name = Intent.GetStringExtra("Name");
text = FindViewById<TextView>(Resource.Id.textview1);
text.Text = name;
Log.Debug("123" ,name);
}
Just looking to see if I can find out how i get the item selected in the spinner and store it in a string, i have seen the other posts about this and people say to put this line into code:
(Beneath the last line of the code i posted below)
String Genders = Gender.getSelectedItem().toString();
I try but it gives me a red line underneath (getselecteditem()) - saying spinner does not contain a definition for getselecteditem()
Here is my code:
Spinner Gender;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Form);
var Genders = new String[]
{
"Male", "Female"
};
BaseMale = 2000;
Gender = FindViewById<Spinner>(Resource.Id.spinner1);
Gender.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, Genders);
Would really appreciate any of your help! :)
you might need to implement the selection handle (ItemSelected) like in this example from https://developer.xamarin.com/guides/android/user_interface/spinner/:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "Main" layout resource
SetContentView (Resource.Layout.Main);
Spinner spinner = FindViewById<Spinner> (Resource.Id.spinner);
spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs> (spinner_ItemSelected);
var adapter = ArrayAdapter.CreateFromResource (
this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
}
and here is the handle, the index to selected item appears as e.Position here.
private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
string toast = string.Format ("The planet is {0}", spinner.GetItemAtPosition (e.Position));
Toast.MakeText (this, toast, ToastLength.Long).Show ();
}
MainActivity.cs
namespace testApp
{
[Activity (Label = "DHS HotKeys", MainLauncher = true, Icon = "#drawable/icon")]
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);
// Get our button from the layout resource,
// and attach an event to it
Button button1 = FindViewById<Button> (Resource.Id.button1);
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
var uri = Android.Net.Uri.Parse ("https://www.fridayparentportal.com/delran/");
var intent = new Intent (Intent.ActionView, uri);
StartActivity (intent);
};
button1.Click += (object sender, EventArgs e) => {
var uri2 = Android.Net.Uri.Parse ("http://dhs.delranschools.org/students/lunch_menu/");
var intent2 = new Intent (Intent.ActionView, uri2);
StartActivity (intent2);
};
}
}
}
Inside the resource designer:
public partial class Id
{
// aapt resource value: 0x7f050001
public const int button1 = 2131034113;
// aapt resource value: 0x7f050000
public const int myButton = 2131034112;
The 2nd button is set exactly like the first, but it does nothing when clicked, while the first one opens the webpage. The ID for 2nd button is #+id/button1
Thank you
write each button closer to their eventhandler, like this :
Button btn1 = FindViewById<Button>(Resource.Id.button1);
btn.Click += (object se,EventArgs e)
{
}
I am building an app where user can select which Language he wants to set when the app starts. A pop up will show "Select your Language" with two buttons one for Arabic and the other one for English.
The problem is that when I change the locale and execute this line
SetContentView (Resource.Layout.Main);
the app broke. there is no error or any exception but all controls stop capturing the events. I tried to declare an Intent using this and this.Class and when I call StartActivity it is like restarting the whole app and the pop comes again to select the language. I am new to android development as I spent my last two years working on SAP Abap so I might ask a stupid question :D
here is my code
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Khums
{
[Activity (Label = "Khums", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
int r = Resource.Layout.Main;
SetContentView (Resource.Layout.Main);
var languageIso = "ar-SA";
AlertDialog.Builder alert = new AlertDialog.Builder (this);
//alert.SetTitle ("Selected Language");
AlertDialog alertDiaog = alert.Create ();
alertDiaog.SetTitle ("Select Language:");
alertDiaog.SetButton ("العربية", (s, EventArgs) => {
languageIso = "ar-SA";
var locale = new Java.Util.Locale(languageIso);
Java.Util.Locale.Default = locale;
var config = new Android.Content.Res.Configuration{Locale = locale };
BaseContext.Resources.UpdateConfiguration(config, BaseContext.Resources.DisplayMetrics);
//base.SetContentView(r);
//Intent intent = new Intent(this, this.Class);
//StartActivity(intent);
SetContentView (Resource.Layout.Main);
});
alertDiaog.SetButton2 ("English", (s, EventArgs) => {
languageIso = "en-US";
var locale = new Java.Util.Locale(languageIso);
Java.Util.Locale.Default = locale;
var config = new Android.Content.Res.Configuration{Locale = locale };
BaseContext.Resources.UpdateConfiguration(config, BaseContext.Resources.DisplayMetrics);
SetContentView (Resource.Layout.Main);
});
alertDiaog.Show();
Button button = FindViewById<Button> (Resource.Id.myButton);
RadioButton rb_FirstTime = FindViewById<RadioButton> (Resource.Id.radioButton1);
RadioButton rb_Regular = FindViewById<RadioButton> (Resource.Id.radioButton2);
EditText ti_lyearBalance = FindViewById<EditText> (Resource.Id.ti_lastBalance);
EditText ti_Balance = FindViewById<EditText> (Resource.Id.ti_Balance);
EditText ti_Clothes = FindViewById<EditText> (Resource.Id.ti_Clothes);
EditText ti_Food = FindViewById<EditText> (Resource.Id.ti_Food);
EditText ti_Perfumes = FindViewById<EditText> (Resource.Id.ti_Perfumes);
EditText ti_Subscriptions = FindViewById<EditText> (Resource.Id.ti_Subscriptions);
EditText ti_Others = FindViewById<EditText> (Resource.Id.ti_Others);
TextView lbl_lyearBalance = FindViewById<TextView> (Resource.Id.lbl_lastBalance);
rb_FirstTime.Click += RadioButtonHandler;
rb_Regular.Click += RadioButtonHandler;
button.Click += MyButtoHandler;
}
private void RadioButtonHandler(object sender, EventArgs e)
{
}
private void MyButtoHandler(object sender, EventArgs e)
{
}
private double calculateKhumus (double[] amounts, Boolean isRegular)
{
}
private void LangSwitchHndler(Object sender, EventArgs e)
{
}
}
}
can you please show me what I'm doing wrong here. I tried to use togglebutton instead of alert and also standard button but ends up with the same issue. Thank you.
It is solved after trying for two days. All I did is to create a menu and change the target framework to Android 3.1 Honeycomb. Then I just start the activity and everything is working fine.
So the issue was only target framework!
I'm currently working on a website using c# and asp.net. For this purpose, I need to create dynamic controls but I enconter some issues. I already read official documentation and searched for lots of tutorial but unfortunately, no one allowed me to fix this problem.
Here is a very simplified example of what I'm trying to do;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
CreateControls();
else
UpdatePage();
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = "Button1";
button1.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = "Button2";
button2.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = "I went through UpdatePage and changed";
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button1.Text = "I went through UpdatePage and changed";
}
protected void _ClickEvent(object sender, EventArgs e)
{
}
The aim here would just be to change the buttons' text when clicking on one of them. "Page_Load" method is correctly called as well as the "UpdatePage" one, but in the latter, Button1 and Button2 controls have disappeared (they are not in the panel controls anymore) and an NullPointer exception is obviously raised.
Would anybody have an explanation ? I know I probably missed something about page life cycle but could not find any clear solution anywhere.
Thanks a lot !
You are creating the controls the first time the page is loaded, but the Page_Load event is too late to add controls to the page and have WebForms know about that.
On the initial page load, somewhere between the OnInit and Page_Load, WebForms makes a note of what controls are currently on the page and sets them up in the view state and all of that stuff, so that they next time you post back it knows those controls should be there. If you don't add your controls until Page_Load, WebForms isn't really paying attention any more to what you're adding to the page, so they next time you post back it doesn't know to put those controls on the page.
Move your CreateControls call into the OnInit method. This will tell WebForms at the appropriate time to create the controls (about the same time that any controls from the .aspx markup get added, although slightly later). Then WebForms will be aware of those controls and will apply any view state necessary (if it's a postback), and then finally on Page_Load you can muck with the control data with your UpdatePage call.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
}
Think of OnInit as "put all of the controls on the page and wire up event handlers".
Think of Page_Load as "put data into the controls that are already there".
The controls that you are creating dynamically will be lost on postback. Try this:
protected void Page_Load(object sender, EventArgs e)
{
CreateControls();
UpdatePage();
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = "Button1";
button1.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = "Button2";
button2.Click += new System.EventHandler(_ClickEvent);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = "I went through UpdatePage and changed";
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button1.Text = "I went through UpdatePage and changed";
}
protected void _ClickEvent(object sender, EventArgs e)
{
}
Will try it:
protected String TextButton1
{
get { return (String) ViewState["TextButton1"]; }
set { ViewState["TextButton1"] = value; }
}
protected String TextButton2
{
get { return (String)ViewState["TextButton2"]; }
set { ViewState["TextButton2"] = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
UpdatePage();
}
}
protected void CreateControls()
{
Button button1 = new Button();
button1.ID = "_Button1";
button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1;
button1.Click += new System.EventHandler(_ClickEvent1);
_Panel.Controls.Add(button1);
Button button2 = new Button();
button2.ID = "_Button2";
button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2;
button2.Click += new System.EventHandler(_ClickEvent2);
_Panel.Controls.Add(button2);
}
protected void UpdatePage()
{
Button button1 = ((Button)_Panel.FindControl("_Button1"));
button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1;
Button button2 = ((Button)_Panel.FindControl("_Button2"));
button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2;
}
protected void _ClickEvent1(object sender, EventArgs e)
{
TextButton1 = "test";
Button b = (Button) sender ;
b.Text = TextButton1;
}
protected void _ClickEvent2(object sender, EventArgs e)
{
TextButton2 = "test";
Button b = (Button)sender;
b.Text = TextButton2;
}