My simple app closes on button click [duplicate] - android

This question already has an answer here:
"Unfortunately My App has stopped" [closed]
(1 answer)
Closed 4 years ago.
The app is expected to accept two numbers, and on button click, print the sum in the textView.
The layout appears as expected as this as desired :
Layout
But on button click, the app 'stops like this : Stopping message.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/editText1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="46dp"
android:layout_marginTop="88dp"
android:ems="10"
android:inputType="number"
android:layout_alignParentLeft="true"
android:layout_marginLeft="46dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/editText1"
android:layout_marginEnd="57dp"
android:ems="10"
android:inputType="number"
android:layout_alignParentRight="true"
android:layout_marginRight="57dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="192dp"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="187dp"
android:text="Button" />
</RelativeLayout>
and the java file is.
package com.example.home.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextClock;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText et1, et2;
TextView tv1;
Button bt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
tv1 = (TextView) findViewById(R.id.textView1);
bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int num1, num2, res;
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());
res = num1 + num2;
tv1.setText(res);
}
});
}
}
When I run the app, it shows the layout as desired on the mobile and also on the emulator. It also accepts the two numbers properly. But when the button is clicked, the app stops as shown. I am using Android 3.1.3.
Kindly advise where I am going wrong ?

change this line : tv1.setText(res);
into this tv1.setText(String.valueOf(res));

The cause is tv1.setText(res). There are two methods with one parameter, which are setText(Int) and setText(CharSequence). As in your code, you accidentally called the first method as you declared res as an Integer, which is interpreted as a resource id. You should call the second one by converting res to String.
Just change the line tv1.setText(res); to tv1.setText(Integer.toString(res));
package com.example.home.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextClock;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText et1, et2;
TextView tv1;
Button bt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
tv1 = (TextView) findViewById(R.id.textView1);
bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int num1, num2, res;
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());
res = num1 + num2;
tv1.setText(Integer.toString(res));
}
});
}
}
You can read up the TextView documentation to learn more about its public methods.

You are setting an integer value in textfield, which is not allowed. So you are getting an exception. In order to solve convert integer to string.
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int num1, num2, res;
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());
res = num1 + num2;
tv1.setText("" + res);
}
});
Also you should apply validation. As if user clicks the button without entering any number, the app will stop. So you should add -
if(! et1.getText().toString().equals("") && ! et2.getText().toString().equals("") ) {
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());
res = num1 + num2;
tv1.setText("" + res);
}
else {
Toast.makeText(this, "Enter the numbers", Toast.LENGTH_LONG).show();
}

The res variable should be String type.

Related

android studio 2.3 i have problems in textview, it is not displaying the result

using two buttons one for add and one for subtraction and a text view to show the numbers but when I run this program textview is not showing the numbers like after pressing add button value shoud be 1 when I use the buttons and code of java looks fine
I have tried to change their names also but its not working
here is the java code
package com.example.junaid;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button mns, pls;
TextView tv;
int counter;
EditText ed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mns = (Button) findViewById(R.id.minuss);
pls = (Button) findViewById(R.id.pluss);
tv = (TextView) findViewById(R.id.textView2);
counter = 0;
mns.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
tv.setText("Your Total is" + counter);
}
});
pls.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
tv.setText("Your Total is" + counter);
}
});
}
}
and XML code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/pluss"
android:layout_width="259dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:text="Add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<Button
android:id="#+id/minuss"
android:layout_width="257dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:text="Subtract"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/pluss"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/textView2"
android:layout_width="257dp"
android:layout_height="84dp"
android:layout_marginTop="8dp"
android:text="Your Total is"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Try this way,
you didn't set the initial value in textview like 0.
1. set initial value in text view as 0 (zero).
2. on every +/- click, first get data from textview in integer format.
3. perform +/- operation on it. Like a+1 or a-1
4. store output to textview.
Done.
Try this:
mns.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String oldStr = tv.getText().tostring();
Int oldVal = Integer.parseInt(oldStr);
Int newVal = oldVal - 1;
String newStr = String.valueOf(newVal);
tv.setText("Your Total is" + newStr);
}
});
pls.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String oldStr = tv.getText().tostring();
Int oldVal = Integer.parseInt(oldStr);
Int newVal = oldVal + 1;
String newStr = String.valueOf(newVal);
tv.setText("Your Total is" + newStr);
}
});

How can store Data text view data?

In this file i have two Edit text and one textview.I want to sum two number.then result are show in text view.Then i want to store Textview data. please help.i am trying but i am fail.
Activity_main.xml//
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Edit"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/Edit1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add"
android:id="#+id/add"
android:onClick="add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:id="#+id/sub"
android:onClick="sub"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:paddingBottom="30dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pr"
android:text="Previoues Data"
/>
</LinearLayout>
</LinearLayout>
the java file is
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText Edit,Edit1;
Button add, sub, pr;
TextView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Edit = (EditText) findViewById(R.id.Edit);
Edit1 = (EditText) findViewById(R.id.Edit1);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
pr = (Button) findViewById(R.id.pr);
view = (TextView) findViewById(R.id.view);
Edit.setText("0");
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int value1 = Integer.parseInt(Edit.getText().toString());
int value2 = Integer.parseInt(Edit1.getText().toString());
int result = value1 + value2;
view.setText(Integer.toString(result));
// Edit.setText(Integer.toString(result));
SharedPreferences sharedPreferences=getSharedPreferences("Mydata",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("value",result);
editor.commit();
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int value1 = Integer.parseInt(Edit.getText().toString());
int value2 = Integer.parseInt(Edit1.getText().toString());
int result = value1 - value2;
view.setText(Integer.toString(result));
Edit.setText(Integer.toString(result));
}
});
pr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("Mydata",Context.MODE_PRIVATE);
// int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
String v=sharedPreferences.getInt("value","");
view.setText(v);
}
});
}
}
update your xml file remove onClick from your button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add"
android:id="#+id/add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:id="#+id/sub"
/>
Problem is with your getting value from sharedPreference
String v=sharedPreferences.getInt("value","");
why are you passing string as default value and your
try this String
v = Integer.toString(sharedPreferences.getInt("value",0));
in the layout you´ve got android:onClick="add" there should be a public void add(View v) to handle the event. Do the sum inside that handler.
Please, follow the documentation
http://developer.android.com/reference/android/widget/Button.html and see the :onCLick handling.

cannot be resolved or is not a field with layout textview and button

I'm getting four errors all with this message: cannot be resolved or is not a field
The errors are in two different .java files
StartScreen.java.text
tv = (TextView) findViewById(R.id.startscreen); (startscreen underlined in red)
Button startButton = (Button) findViewById(R.id.play_game); (play_game underlined in red)
I creaded a textview called startscreen and a button called startButton inside game.xml
PlayGame.java
tv2 = (TextView) findViewById(R.id.game_text); (game_text underlined in red)
Button startButton = (Button) findViewById(R.id.end_game); (end_game underlined in red)
I creaded a textview called game_text and a button called end_game inside game.xml
StartScreen.java
package com.example.startscreenapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class StartScreen extends Activity {
private static final int PLAY_GAME = 1010;
private TextView tv;
private int meaningOfLife = 42;
private String userName = "Douglas Adams";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.startscreen.text);
//Display initial values
tv.setText(userName + ":" + meaningOfLife);
//Set up button listener
Button startButton = (Button) findViewById(R.id.play_game);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startGame();
}
});
}
#Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == PLAY_GAME && resultCode == RESULT_OK) {
meaningOfLife = data.getExtras().getInt("returnInt");
userName = data.getExtras().getString("returnStr");
//Show it has changed
tv.setText(userName + ":" + meaningOfLife);
}
super.onActivityResult(requestCode, resultCode, data);
}
private void startGame() {
Intent launchGame = new Intent(this, PlayGame.class);
//passing information to launched activity
launchGame.putExtra("meaningOfLife", meaningOfLife);
launchGame.putExtra("userName", userName);
startActivityForResult(launchGame, PLAY_GAME);
}
}
PlayGame.java
package com.example.startscreenapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PlayGame extends Activity {
private TextView tv2;
int answer;
String author;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
tv2 = (TextView) findViewById(R.id.game_text);
//reading information passed to this activity
//Get the intent that started this activity
Intent i = getIntent();
//returns -1 if not initialized by calling activity
answer = i.getIntExtra("meaningOfLife", -1);
//returns [] if not initialized by calling activity
author = i.getStringExtra("userName");
tv2.setText(author + ":" + answer);
//Change values for an example of return
answer = answer - 41;
author = author + " Jr.";
//Set up button listener
Button startButton = (Button) findViewById(R.id.end_game);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Return information to calling activity
Intent i = getIntent();
i.putExtra("returnInt", answer);
i.putExtra("returnStr", author);
setResult(RESULT_OK, i);
finish();
}
});
}}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/startscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="115dp"
android:layout_marginTop="164dp"
android:text="TextView" />
<Button
android:id="#+id/play_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/startscreen"
android:layout_below="#+id/startscreen"
android:layout_marginTop="64dp"
android:text="Button" />
</RelativeLayout>
game.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/game_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/end_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Your textview id on your xml is startscreen
Instead of v = (TextView) findViewById(R.id.startscreen.text); just remove the .text part
in startscreen.java you have tv = (TextView) findViewById(R.id.startscreen.text); and in your activity_main.xml you have android:id="#+id/startscreen".
what you should do :
1-is removing the .text from tv = (TextView) findViewById(R.id.startscreen.text);
2- clean and build you project again. (if this didn't work , close and open eclipse again.
i suggest to use ctrl + space in completing you statement, that would help in reducing error and give suggestion.

Eclipse Android Checkbox

I would like to create checkbox with text that would behave something like what i have below in pseudo code:
Set in text in EditText field
if Button clicked:
create new Checkbox with entered text.
How do i go about doing that in Android?
So far ive got the button :) ... but dont know how to get new Checkbox with text in it ,if its clicked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Button create = (Button) findViewById(R.id.button1);
create.setOnClickListener(this);
EditText textInCheckBox = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
// TODO Automatisch generierter Methodenstub
switch (v.getId()) {
case R.id.button1:
//after creating new checkbox with text in it
//Editbox should restet text field to null;
break;
}
}
}
Here is some code that will demonstrate how to add checkboxes dynamically with the title you entered into the EditText.
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
final Button btnCreate = (Button) findViewById(R.id.btnCreate);
final RadioGroup radContainer = (RadioGroup) findViewById(R.id.radContainer);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = txtTitle.getText().toString().trim();
if (!name.isEmpty()) {
CheckBox checkBox = new CheckBox(MainActivity.this);
checkBox.setText(name);
checkBox.setLayoutParams(params);
radContainer.addView(checkBox);
} else {
Toast.makeText(MainActivity.this, "Enter a title for the checkbox", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp" >
<EditText
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone" />
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Checkbox" />
<RadioGroup
android:id="#+id/radContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Simple Math Android App Not Working

I am new to Android developing and I am trying to make a simple app as practice. All it consists of is you type one number into the first box, a number into the second box, press a button and the result prints into a textview. The problem is, when I press the send button Nothing is happening. Eclipse is not showing any errors, nor is LogCat. So I am officially stumped as to why when I run this I'm not getting anything out. Hopefully you guys can help.
Here is the XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="172dp"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="48dp"
android:text="First Number:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="40dp"
android:text="Second Number:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_alignTop="#+id/textView1"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView2"
android:layout_toRightOf="#+id/linearLayout1"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/editText2"
android:layout_below="#+id/editText2"
android:text="Calc" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_centerHorizontal="true"
android:text="TextView" />
</RelativeLayout>
And here is the Java:
package com.example.mathbox;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button sum;
EditText num1, num2;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void main(String[] args){
num1 = (EditText) findViewById(R.id.editText1);
num2 = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.textView3);
num1.getText().toString();
num2.getText().toString();
int x =Integer.parseInt("num1");
int y =Integer.parseInt("num2");
int nas = x + y;
tv.setText(nas);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {}
});}}
You are parsing the wrong string..
num1.getText().toString();
num2.getText().toString();
int x =Integer.parseInt("num1");
int y =Integer.parseInt("num2");
What you are doing now is parsing the strings "num1" and "num2" into a string and I don't think that is what you want. So it should be:
int x = Integer.parseInt(num1.getText().toString());
int y = Integer.parseInt(num2.getText().toString());
And also move the code into the onCreate() method just like #AndroidEnthusiastic suggested.
public void main(String[] args)
For Android it is a simple method,not an entry point that by the way supposed to be :
public static void main(String[] args)
I can't see that you are calling your main method in your code.
num1.getText().toString();
num2.getText().toString();
You just get the values without assigning it to any properties.
This code is totally wrong.
I suggest you to start from the beginning with official Android Training : Building Your First App.
Hye Diamond you are doing something wrong. Its ok your beginner. see you are creating main mothod. In java main method public static void main get executed first but here once activity created oncreate method will execute. So you can do it in following ways:
//inside oncreate method do like this
num1 = (EditText) findViewById(R.id.editText1);
num2 = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.textView3);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
int val1 = num1.getText().toString();
int val2 = num2.getText().toString();
int x =Integer.parseInt(val1);
int y =Integer.parseInt(val2);
int nas = x + y;
tv.setText(nas);
}
});
this might be help you. It's not tested. May be you have to do some changes.
Here is the complete solution
package com.example.mathbox;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button sum;
EditText num1, num2;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.editText1);
num2 = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.textView3);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
num1.getText().toString();
num2.getText().toString();
int x =Integer.parseInt("num1");
int y =Integer.parseInt("num2");
int nas = x + y;
tv.setText(nas);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Please take a look at this link
Find this code.. hope it helps
public class MainActivity extends Activity {
Button sum;
EditText num1, num2;
TextView tv;
String x;
String y;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.editText1);
num2 = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.textView3);
sum = (Button) findViewById(R.id.button1);
sum.setOnClickListener(new clicker());
}
class clicker implements Button.OnClickListener {
public void onClick(View v) {
Integer nas;
x = num1.getText().toString();
y = num2.getText().toString();
nas = Integer.parseInt(x) + Integer.parseInt(y);
tv.setText(nas.toString());
}
}
}
Please try this code,
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class add extends Activity {
Button button1;
EditText txtbox1,txtbox2;
TextView tv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtbox1= (EditText) findViewById(R.id.txtbox1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.lbl1);
txtbox2= (EditText) findViewById(R.id.txtbox2);
button1.setOnClickListener(new clicker());
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)+Integer.parseInt(b);
tv.setText(vis.toString());
}
}
}
Try this,
int x =Integer.valueof(""+num1.getText().toString());
int y =Integer.valueof(""+num2.getText().toString());
int nas = x + y;
tv.setText(""+String.valueof(nas));
Please refrain from using the main function. It's not really required in an activity class and also not a general practice. #AndroidEnthusiastic has given the correct code.
Added:
Since the aforementioned answer of mine was downvoted, I'll go ahead and just tell you what you've done wrong in your code(there's a lot of code in the other answers given here).
num1 = (EditText) findViewById(R.id.editText1);
num2 = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.textView3);
num1.getText().toString();//Store this in a string say a
num2.getText().toString();//Store this in a string say b
/*you're actually the passing the literal "num1" or "num2". Something which you don't want.
Instead, pass the strings a and b instead of num1 and num2 respectively*/
int x =Integer.parseInt("num1");
int y =Integer.parseInt("num2");
int nas = x + y;
tv.setText(nas);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*You need to put the code here. i.e. You need to take the values from the
editTexts and store them in strings and finally integers when the button is clicked.*/
}
});

Categories

Resources