I'm a beginner, I did a simple task but I do not see the result in TextView. How can I solve my problem?
Code:
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText1, editText2;
TextView textView;
public void Add(View v)
{
float x = Float.parseFloat(editText1.getText().toString());
float y = Float.parseFloat(editText2.getText().toString());
float sum = x + y;
textView.setText(String.valueOf(sum));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText1 = findViewById(R.id.editText);
editText2 = findViewById(R.id.editText2);
textView = findViewById(R.id.textView);
}
}
The second option I tried was:
textView.setText("SUM = " + String.valueOf(sum));
But then I receive a message:
Do not concatenate text displayed with setText. Use resource string with placeholders.
PS. Of course i added method android:onClick = "Add"
You can try sum.toString() instead of String.valueOf(sum)
This will do your work
textView.setText(Float.toString(sum))
you better make button onclick in java, then display the string value to the textview
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleButtonClick();
}
private void handleButtonClick() {
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addAndSetResult();
}
});
}
private void addAndSetResult() {
// inside here do the add logic and set result to textview
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
float x = Float.parseFloat(editText1.getText().toString());
float y = Float.parseFloat(editText2.getText().toString());
float sum = x + y;
String sumStr = String.valueOf(sum);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(sumStr);
}
Related
XML LAYOUT SCREENSHOT
Trying to calculate the final amount of selected items in an another activity. Need to create order summary in another activity.
Tried introducing multiplication function but also need to hard code the quantity values to auto calculate the final amount.
public class MainActivity extends AppCompatActivity {
EditText pantqty;
EditText pantrate;
TextView result;
Button SUBMIT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.et_pantqty);
pantrate = (EditText) findViewById(R.id.et_pantrate);
SUBMIT = (Button) findViewById(R.id.et_submit);
result = (TextView) findViewById(R.id.et_result);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int q = Integer.valueOf(pantqty.getText().toString());
float r = Integer.valueOf(pantrate.getText().toString());
float amount = q * r;
result.setText(Float.toString(amount));
}
});
}
}
Need to create a method in OnCLickListener to calculate the amount from the items selected.can if else case be used on OnclickListerButton?
Thanks for the help.
You can do something like this:
public class MainActivity extends AppCompatActivity {
EditText pantqty;
EditText pantrate;
EditText shirtQty;
EditText shirtRate;
TextView result;
Button SUBMIT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.et_pantqty);
pantrate = (EditText) findViewById(R.id.et_pantrate);
shirtQty = (EditText) findViewById(R.id.shirtqtyId);
shirtRate = (EditText) findViewById(R.id.shirtRateId);
SUBMIT = (Button) findViewById(R.id.et_submit);
result = (TextView) findViewById(R.id.et_result);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculateSum();
}
});
}
private void calculateSum(){
int q=0;
String pantQty=pantqty.getText().toString().trim();
if(!pantQty.isEmpty() && pantQty!="") {
q=Integer.valueOf(pantQty);
}
float r = Integer.valueOf(pantrate.getText().toString());
int a = Integer.valueOf(shirtQty.getText().toString());
float b = Integer.valueOf(shirtRate.getText().toString());
float amount = (q * r)+(a*b);
result.setText(Float.toString(amount));
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("total",amount);
startActivity(intent);
}
}
On the secondActivity you get get the total as:
float total=getIntent().getFloatExtra("total",0.0);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.lo_pantqty);
shirtqty = (EditText) findViewById(R.id.lo_shirtqty);
shareesqty = (EditText) findViewById(R.id.lo_shareesqty);
suitqty = (EditText) findViewById(R.id.lo_suitqty);
result = (TextView) findViewById(R.id.et_result);
SUBMIT = (Button) findViewById(R.id.io_enter);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculationSum();
}
});
}
private void calculationSum() {
int q = 0;
String pantQTY = pantqty.getText().toString().trim();
if (!pantQTY.isEmpty()&&pantQTY!=""){
q = Integer.valueOf(pantQTY);
}
int r = 0;
String shirtQTY = shirtqty.getText().toString().trim();
if (!shirtQTY.isEmpty()&&shirtQTY!=""){
r = Integer.valueOf(shirtQTY);
}
int a = 0;
String suitQTY = suitqty.getText().toString().trim();
if (!suitQTY.isEmpty()&&suitQTY!=""){
a = Integer.valueOf(shirtQTY);
}
int b = 0;
String shareesQTY = shareesqty.getText().toString().trim();
if (!shareesQTY.isEmpty()&&shareesQTY!=""){
b = Integer.valueOf(shirtQTY);
}
int amount = (q*30) + (r*35) + (a*220) + (b*60);
result.setText(Integer.toString(amount));
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Like in subject. I got null pointer, how it is possible ?
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
You just forgot to do findViewById to your Controls in OnCreate()
button2 =(Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.btnID);
textView2 =(textView) findViewById(R.id.textView2);
Sample code
#Override
protected void onCreate( Bundle savedInstanceState ){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.mybtnID);
button2 = (Button) findViewById(R.id.button2);
textView2 = (textView) findViewById(R.id.textView2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick( View view ){
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick( View view ){
size = list.size();
textView2.setText(size);
}
});
}
You are missing
findViewById for all your widget
i.e.
Button button, button2;
TextView textView, textView2;
button = findViewById(R.id.id_of_button_1); // same for button2,textview,textview2
You can initialize with findById(R.id.myBtn) like everyone is saying. Or programatically like button = new Button(this);
Try this
You forgot to add findViewById
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button2=(Button)findViewById(R.id.button2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
Try this are missing findViewById for all your widget
button = (Button) findViewById(R.id.btnID);
button2 =(Button) findViewById(R.id.button2);
textView = =(textView) findViewById(R.id.textView);
textView2 =(textView) findViewById(R.id.textView2);
Sample Code
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btnID);
button2 =(Button) findViewById(R.id.button2);
textView = =(textView) findViewById(R.id.textView);
textView2 =(textView) findViewById(R.id.textView2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
I am trying to use to distance formula, When I try to run the program, I can put in values for x1,y1,x2, but no result shows up when I try and find the distance. Any suggestions?
public class MainActivity extends AppCompatActivity {
EditText editText, editText2, editText3, editText4;
Button button;
TextView tv_result;
double a,b,c,d, e, x1,x2,y1,y2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
button = (Button) findViewById(R.id.button);
//tv_result = (TextView) findViewById(R.id.tv_result);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!editText.getText().toString().equals("") && !editText2.getText().toString().equals("")
&& !editText3.getText().toString().equals("") && !editText4.getText().toString().equals("")){
x1 = Double.parseDouble(editText.getText().toString());
x2 = Double.parseDouble(editText.getText().toString());
y1 = Double.parseDouble(editText.getText().toString());
y2 = Double.parseDouble(editText.getText().toString());
e = Math.sqrt(Math.pow(x2-x1,2)+ Math.pow(y2-y1,2));
// if (e == 0) {
// tv_result.setText("The two points are the same");
if (e > 0){
tv_result.setText("e");
}
}
}
});
}
}
Basically forked your code. Made some changes though.
Changes
Uncomment tv_result = (TextView) findViewById(R.id.tv_result); as #ibtehaz pointed out as it would lead to an NPE at tv_result.setText("e");.
At this line tv_result.setText("e"); you are setting the character "e" to the TextView rather than your result. You should be doing tv_result.setText(e);.
See complete code below,
public class MainActivity extends AppCompatActivity {
EditText editText, editText2, editText3, editText4;
Button button;
TextView tv_result;
double a,b,c,d, e, x1,x2,y1,y2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
button = (Button) findViewById(R.id.button);
tv_result = (TextView) findViewById(R.id.tv_result);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!editText.getText().toString().equals("")
&& !editText2.getText().toString().equals("")
&& !editText3.getText().toString().equals("")
&& !editText4.getText().toString().equals("")){
x1 = Double.parseDouble(editText.getText().toString());
x2 = Double.parseDouble(editText.getText().toString());
y1 = Double.parseDouble(editText.getText().toString());
y2 = Double.parseDouble(editText.getText().toString());
e = Math.sqrt(Math.pow(x2-x1,2)+ Math.pow(y2-y1,2));
if (e == 0) {
tv_result.setText("The two points are the same");
} else if (e > 0){
tv_result.setText(String.valueOf(e));
}
}
}
});
}
}
Change/ UN COMMENT THIS LINE
tv_result = (TextView) findViewById(R.id.tv_result);
You would have get an ERROR when you try to bind data on a NULL Object!
Cheers!
I'm new to android and Java. I wanna pass a variable (ac) from OnClickListener to another. I've tried this way, but i receive this error: cannot resolve symbol 'ac'. Can you help me please?
Button Calculate = (Button) theLayout.findViewById(R.id.button);
Button buttonb = (Button) theLayout.findViewById(R.id.buttonb);
final TextView tvac = (TextView) theLayout.findViewById(R.id.tvac);
final TextView tvh = (TextView) theLayout.findViewById(R.id.tvh);
final EditText eta = (EditText) theLayout.findViewById(R.id.eta);
final EditText etn = (EditText) theLayout.findViewById(R.id.etn);
final EditText etb = (EditText) theLayout.findViewById(R.id.etb);
Calculate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Double a = new Double(eta.getText().toString());
Double n = new Double(etn.getText().toString());
Double ac = a*n;
tvac.setText(getResources().getString(R.string.tvresultados2) + " " + ac);
}
});
buttonb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
double b = new Double(etb.getText().toString());
double h = ac/b; //error: cannot resolve symbol 'ac'
tvh.setVisibility(View.VISIBLE);
tvh.setText("h = " + h);
}
});
The most simple way is to declare global variable. Declare your ac outside of onCreate scope instead of inside of it.
public Double ac; // global variable
#Override
public void onCreate(Bundle savedInstanceState){
Button Calculate = (Button) theLayout.findViewById(R.id.button);
Button buttonb = (Button) theLayout.findViewById(R.id.buttonb);
final TextView tvac = (TextView) theLayout.findViewById(R.id.tvac);
final TextView tvh = (TextView) theLayout.findViewById(R.id.tvh);
final EditText eta = (EditText) theLayout.findViewById(R.id.eta);
final EditText etn = (EditText) theLayout.findViewById(R.id.etn);
final EditText etb = (EditText) theLayout.findViewById(R.id.etb);
Calculate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Double a = new Double(eta.getText().toString());
Double n = new Double(etn.getText().toString());
ac = a*n;
tvac.setText(getResources().getString(R.string.tvresultados2) + " " + ac);
}
});
buttonb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
double b = new Double(etb.getText().toString());
double h = ac; //assign global variable into h
tvh.setVisibility(View.VISIBLE);
tvh.setText("h = " + h);
}
});
}
I am a beginner to this field of android and am currently in pursuit of developing an application to track the number of periods that i have bunked with the use of counters and the subject names in edittext and the values of counters keeps on incrementing as the button bunked is pressed everytime and its change will be effected in a textfield respectively
my app has only one activity and when the user has finished entering the data in edittext and as the counters are incremented so do the values in textview and when the user presses the back button the app exits and now when the user enters the app once again now all the previously entered data are gone...
no data in edittext or textview either...
public class MainActivity extends Activity {
public static final String sub1="sub";
public static final String count1="Count";
String sub[]=new String[10];
String count[]=new String[10];
int counter[]=new int[10];
Button button[]=new Button[10];
EditText et[]=new EditText[10];
TextView tv[]=new TextView[12];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button[0] = (Button) findViewById(R.id.button1);
button[1] = (Button) findViewById(R.id.button2);
button[2] = (Button) findViewById(R.id.button3);
button[3] = (Button) findViewById(R.id.button4);
button[4] = (Button) findViewById(R.id.button5);
button[5] = (Button) findViewById(R.id.button6);
button[6] = (Button) findViewById(R.id.button7);
button[7] = (Button) findViewById(R.id.button8);
button[8] = (Button) findViewById(R.id.button9);
tv[0] = (TextView) findViewById(R.id.textView2);
tv[1] = (TextView) findViewById(R.id.textView3);
tv[2] = (TextView) findViewById(R.id.textView4);
tv[3] = (TextView) findViewById(R.id.textView5);
tv[4] = (TextView) findViewById(R.id.textView6);
tv[5] = (TextView) findViewById(R.id.textView7);
tv[6] = (TextView) findViewById(R.id.textView8);
tv[7] = (TextView) findViewById(R.id.textView9);
tv[8] = (TextView) findViewById(R.id.textView10);
tv[9] = (TextView) findViewById(R.id.textView1);
et[0] = (EditText) findViewById(R.id.editText1);
et[1] = (EditText) findViewById(R.id.editText2);
et[2] = (EditText) findViewById(R.id.editText3);
et[3] = (EditText) findViewById(R.id.editText4);
et[4] = (EditText) findViewById(R.id.editText5);
et[5] = (EditText) findViewById(R.id.editText6);
et[6] = (EditText) findViewById(R.id.editText7);
et[7] = (EditText) findViewById(R.id.editText8);
et[8] = (EditText) findViewById(R.id.editText9);
for(int l=0;l<9;l++) {
sub[l]=et[l].getText().toString();
et[l].setText(sub[l]);
}
for (int j = 0; j < 9; j++) {
button[j].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
for(int k=0;k<9;k++) {
if(button[k]==arg0.findViewById(R.id.button1)) {
counter[k]++;
tv[k].setText(""+counter[k]);
}
if(button[k]==arg0.findViewById(R.id.button2)) {
counter[k]++;
tv[k].setText(""+counter[k]);
}
if(button[k]==arg0.findViewById(R.id.button3)) {
counter[k]++;
tv[k].setText(""+counter[k]);
}
if(button[k]==arg0.findViewById(R.id.button4)) {
counter[k]++;
tv[k].setText(""+counter[k]);
}
if(button[k]==arg0.findViewById(R.id.button5)) {
counter[k]++;
tv[k].setText(""+counter[k]);
}
if(button[k]==arg0.findViewById(R.id.button6)) {
counter[k]++;
tv[k].setText(""+counter[k]);
}
if(button[k]==arg0.findViewById(R.id.button7)) {
counter[k]++;
tv[k].setText(""+counter[k]);
}
if(button[k]==arg0.findViewById(R.id.button8)) {
counter[k]++;
tv[k].setText(""+counter[k]);
}
if(button[k]==arg0.findViewById(R.id.button9)) {
counter[k]++;
tv[k].setText(""+counter[k]);
}
}
}
});
}
}
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(sub1,sub[0]);
savedInstanceState.putInt(count1, counter[0]);
super.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
sub[0]=savedInstanceState.getString(sub1);
counter[0]=savedInstanceState.getInt(count1);
}
}
Its happening because you are saving all data in Arrays which get cleaned up when the app exits. There are several ways tht you could do this. You could either use SharedPreferences to save your data or write your values to a file.
For e.g. you could use SharedPreferences as follows:
public static final String MY_PREF = "MyPreference";
SharedPreferences.Editor editorPref;
public static String keyName = "MyKey";
SharedPreferences myPref;
You could then create the shared preference and add values to it.Hope this helps