The question is quite simple. But I want to know where exactly do we make our references to the gui elements? As in which is the best place to define:
final
Well, it depends on your needs. Very often I keep my references to widgets in activity (as a class fields) - and set them in onCreate
method. I think that is a good idea
Probably the reason for your nulls is that you are trying to call findViewById()
before you set contentView()
in your onCreate()
method - please check that.
in Kotlin 1.3
val readTextFromUser = (findViewById(R.id.inputedText) as EditText).text.toString()
This will read the current text that the user has typed on the UI screen
If you are doing it before the setContentView()
method call, then the values will be null.
This will result in null:
super.onCreate(savedInstanceState);
Button btn = (Button)findViewById(R.id.btnAddContacts);
String text = (String) btn.getText();
setContentView(R.layout.main_contacts);
while this will work fine:
super.onCreate(savedInstanceState);
setContentView(R.layout.main_contacts);
Button btn = (Button)findViewById(R.id.btnAddContacts);
String text = (String) btn.getText();
The quickest solution to your problem I believe is that you simply are missing parentheses on your getText
. Simply add ()
to edit.getText().toString()
and that should solve it
Put this in your MainActivity:
{
public EditText bizname, storeno, rcpt, item, price, tax, total;
public Button click, click2;
int contentView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.main_activity );
bizname = (EditText) findViewById( R.id.editBizName );
item = (EditText) findViewById( R.id.editItem );
price = (EditText) findViewById( R.id.editPrice );
tax = (EditText) findViewById( R.id.editTax );
total = (EditText) findViewById( R.id.editTotal );
click = (Button) findViewById( R.id.button );
}
}
Put this under a button or something
public void clickBusiness(View view) {
checkPermsOfStorage( this );
bizname = (EditText) findViewById( R.id.editBizName );
item = (EditText) findViewById( R.id.editItem );
price = (EditText) findViewById( R.id.editPrice );
tax = (EditText) findViewById( R.id.editTax );
total = (EditText) findViewById( R.id.editTotal );
String x = ("\nItem/Price: " + item.getText() + price.getText() + "\nTax/Total" + tax.getText() + total.getText());
Toast.makeText( this, x, Toast.LENGTH_SHORT ).show();
try {
this.WriteBusiness(bizname,storeno,rcpt,item,price,tax,total);
String vv = tax.getText().toString();
System.console().printf( "%s", vv );
//new XMLDivisionWriter(getString(R.string.SDDoc) + "/tax_div_business.xml");
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
}
There! The debate is settled!
Place the following after the setContentView() method.
final EditText edit = (EditText) findViewById(R.id.Your_Edit_ID);
String emailString = (String) edit.getText().toString();
Log.d("email",emailString);