I am making an app where when we click on add button it adds 1 to the number and when we click subtract it subtracts the number.
I am doing everything right but dont know why its giving error while processing in Emulator.
Below is my source code :
XML Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/total"
android:id="#+id/tvDisplay"
android:textSize="20dp"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bAdd"
android:layout_marginTop="25dp"
android:layout_marginBottom="2dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sub"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bSub"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
/>
</LinearLayout>
Java Code
package com.example.plus.minus;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PlusMinus extends Activity {
int counter;
Button add , sub;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (Button) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Adds 1 to the counter
counter = counter + 1;
display.setText(" Your total is :" + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Subtract 1 from counter
counter = counter - 1;
display.setText(" Your total is :" + counter);
}
});
}
}
Also please let me know how can I find runtime errors myself.
And also how to debug errors myself.
Thanks In Advance
because you are casting the wrong view.
change
display = (Button) findViewById(R.id.tvDisplay);
to
display = (TextView) findViewById(R.id.tvDisplay);
display = (TextView) findViewById(R.id.tvDisplay);
not Button
It should be TextView instead of Button
ClassCastException
This is because of you're identify your TextView as Button So, just try to identify your TextView as like below
display = (TextView) findViewById(R.id.tvDisplay);
instead of
display = (Button) findViewById(R.id.tvDisplay);
Related
This is one of my first app projects and I am pretty excited! I cannot make the "split" button work (it has to divide the total by the number of people set in "people"). The first percentage buttons work fine, but not the last "split it!".
I am learning a lot and I love it!
xml.file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activiy_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context="com.example.dell.calculator.MainActivity"
android:weightSum="1">
<EditText
android:id="#+id/subtotal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Subtotal"
android:inputType="number|numberDecimal" />
<EditText
android:id="#+id/itbms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="ITBMS"
android:inputType="number|numberDecimal" />
<Button
android:id="#+id/diez"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="10%" />
<Button
android:id="#+id/quince"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="15%" />
<Button
android:id="#+id/veinte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="20%" />
<TextView
android:id="#+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:text="TOTAL"
android:textSize="25dp"
android:inputType="numberDecimal"/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Number of People"
android:layout_weight="0.09" />
<EditText
android:id="#+id/people"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="25dp"
android:inputType="number" />
<Button
android:id="#+id/split"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Split It!" />
<EditText
android:id="#+id/finalresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.55"
android:ems="10"
android:hint="EACH"
android:inputType="numberDecimal" />
</LinearLayout>
Mainactivity.java
package com.example.dell.calculator;
import android.icu.text.NumberFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Locale;
import static com.example.dell.calculator.R.id.add;
public class MainActivity extends AppCompatActivity {
TextView result, finalresult;
EditText subtotal, itbms, people;
Button diez, quince, veinte, split;
float result_num, result_num2;
int num1, num2, num3, num4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
finalresult = (TextView) findViewById(R.id.finalresult);
itbms = (EditText) findViewById(R.id.itbms);
subtotal = (EditText) findViewById(R.id.subtotal);
diez = (Button) findViewById(R.id.diez);
quince = (Button) findViewById(R.id.quince);
veinte = (Button) findViewById(R.id.veinte);
diez.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1 = Integer.parseInt(subtotal.getText().toString());
num2 = Integer.parseInt(itbms.getText().toString());
result_num = (float) ((num1 + num2) * 1.10);
result.setText(String.valueOf(result_num));
}
});
quince.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1 = Integer.parseInt(subtotal.getText().toString());
num2 = Integer.parseInt(itbms.getText().toString());
result_num = (float) ((num1 + num2) * 1.15);
result.setText(String.valueOf(result_num));
}
});
veinte.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1 = Integer.parseInt(subtotal.getText().toString());
num2 = Integer.parseInt(itbms.getText().toString());
result_num = (float) ((num1 + num2) * 1.20);
result.setText(String.valueOf(result_num));
}
});
split.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num4 = (int) result_num;
num3 = Integer.parseInt(people.getText().toString());
result_num2 = (float) num4 / num3;
finalresult.setText(String.valueOf(result_num2));
}
});
}
}
This is totally normal, for beginners.I would recommend you to Declare and initialize View Objects(Button,TextView..etc)
simultaneously.
Button split = (Button) findViewById(R.id.split);
You forgot to obtain the button view reference:
Change this:
diez = (Button) findViewById(R.id.diez);
quince = (Button) findViewById(R.id.quince);
veinte = (Button) findViewById(R.id.veinte);
for this:
diez = (Button) findViewById(R.id.diez);
quince = (Button) findViewById(R.id.quince);
veinte = (Button) findViewById(R.id.veinte);
split = (Button) findViewById(R.id.split);
This kind of errors can be detected by reading the app log. I am sure that a NullPointerException ocurrs caused by the invokation of split.setOnClickListener (split was null).
When I click on the add button without entring anything on the edittext,unfortunately calculator has stopped.
package com.example.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Calci extends Activity {
TextView t1;
EditText e1,e2;
Button add,sub,mul,div;
Context c;;
String b,a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calci);
e1=(EditText) findViewById(R.id.EditText01);
e2=(EditText) findViewById(R.id.EditText02);
add=(Button) findViewById(R.id.add);
sub=(Button) findViewById(R.id.sub);
mul=(Button) findViewById(R.id.mul);
div=(Button) findViewById(R.id.div);
t1=(TextView) findViewById(R.id.textView1);
a=e1.getText().toString();
b=e2.getText().toString();
}
public void doSomething(View v){
if(v.getId()==R.id.add){
if (a==null & b==null){
Toast.makeText(c,"PLEASE ENTER SOMETHING", Toast.LENGTH_LONG).show();
}
else{
int result=Integer.parseInt(a) + Integer.parseInt(b);
t1.setText(Integer.toString(result));
}
}
}
}
When I click on the add button,unfortunately calculator has stopped.
XML FILE
<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=".Calci" >
<EditText
android:id="#+id/EditText01"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="number" />
<EditText
android:id="#+id/EditText02"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/EditText01"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/EditText01"
android:layout_marginTop="20dp"
android:text="ADD"
android:onClick="doSomething"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button4"
android:layout_marginTop="44dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/add"
android:onClick="doSomething"
android:text="SUBTRACT" />
<Button
android:id="#+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/sub"
android:onClick="doSomething"
android:text="MULTIPLY" />
<Button
android:id="#+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/mul"
android:onClick="doSomething"
android:text="DIVIDE" />
</RelativeLayout>
When I click on the add button,unfortunately calculator has stopped.\
what shuold i do now
First of all initialize
t1=(TextView) findViewById(R.id.textView1);
And you can put validation for empty check on button click listener before furthe process like
if(!(t1.getText.toString().equals(""))) to show alerts
I think problem is that you have not declared your text-view, try to declare it like below and then try.
t1=(TextView) findViewById(R.id.textView1);
Edit
if (e1.getText.toString().trim() == "" && e2.getText.toString().trim() == "")
{
// alert
}
else
{
// do your code
}
So your final code would be
public void doSomething(View v){
if(v.getId()==R.id.add){
a=e1.getText().toString();
b=e2.getText().toString();
if (a == "" && b == "")
{
// alert
}
else
{
int result=Integer.parseInt(a) + Integer.parseInt(b);
t1.setText(Integer.toString(result));
}
}
}
At first you have mistake in your function - you should call e1.setText(), not t1, t1 undeclared and not initialized. And next you should use OnClickListener like this:
public class Calci extends Activity {
TextView t1;
EditText e1,e2;
Button add,sub,mul,div;
String b,a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calci);
e1=(EditText) findViewById(R.id.EditText01);
e2=(EditText) findViewById(R.id.EditText02);
add=(Button) findViewById(R.id.add);
sub=(Button) findViewById(R.id.sub);
mul=(Button) findViewById(R.id.mul);
div=(Button) findViewById(R.id.div);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a=e1.getText().toString();
b=e2.getText().toString();
int result=Integer.parseInt(a) + Integer.parseInt(b);
e1.setText(Integer.toString(result));
}
});
}
}
And yes, check the textfield for not empty String.
if((!a.isEmpty())&(!b.isEmpty())) { your code }
And the best way - use try-catch for catching FormatNumberException
try {
int result=Integer.parseInt(a) + Integer.parseInt(b);
e1.setText(Integer.toString(result));
}
catch (FormatNumberException e) {
Log.d("Exception", e.getMessage());
}
So you can prevent all crashes.
I just started learning Computer Science and Android development. I've been going through some helloworld demos to try and learn.
So going wild, I tried to rewrite a program that has one button for two buttons using the onClickListener. While I don't have any compiling errors, my program is force closing on me:
01-05 11:20:33.968: E/AndroidRuntime(3257): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multbuttontest/com.example.multbuttontest.MultiButtonActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class button
My XML file looks like so (Sorry I suck at formatting):
<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=".MultiButtonActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
and code:
package com.example.multbuttontest;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class MultiButtonActivity extends Activity implements View.OnClickListener{
Button button1, button2;
int touchCount1, touchCount2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multi_button);
button1 = (Button) findViewById(R.id.button1);
button1.setText( "Touch Button 1!");
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setText( "Touch Button 2!");
button2.setOnClickListener(this);
}
public void onClick(View v){
switch(v.getId()){
case R.id.button1:
touchCount1++;
button1.setText("Touched b1 " + touchCount1 + " times(s)");
break;
case R.id.button2:
touchCount2++;
button2.setText("Touched b2 " + touchCount2 + " times(s)");
break;
}
}
}
This is strictly just for my learning purposes, and the code does suck. Any help would be appreciated.
It's not button, it's Button. The XML tag points to the java class in the framework. And Java is case sensitive.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here is some code that might help you out.
MainActivity:
public class MainActivity extends Activity
{
private TextView txt1;
private Button btnCount,btnClear;
private int num1 = 1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (TextView) findViewById(R.id.txt1);
btnCount = (Button) findViewById(R.id.button1);
btnClear = (Button) findViewById(R.id.button2);
}
public void sendMessage(View view)
{
txt1.setText("You have clicked"+ (num1) + "times");
num1++;
}
public void clear(View view)
{
num1 = 1;
txt1.setText("You have clicked"+ (num1) + "times");
}
}
activity_main.xml:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_clear"
android:onClick="clear" />
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/pushed" />
Output:
This is a simple shell app that will display number of files in data/app folder when button is clicked the textviews are aligned as they should be, but when the button is clicked the textview that displays the number of files is moved down one line. Why?
Here is the code and the main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="32dp"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:text="#string/user_apps" />
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView2"
android:text="0" />
</RelativeLayout>
code:
package rs.test.rootapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class RootAppActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String command[]={"su", "-c","ls /data/app | wc -l"};
Shell shell = new Shell();
String text = shell.sendShellCommand(command);
setNewTextInTextView(text);
}
});
}
public void setNewTextInTextView(String text){
//TextView tv= new TextView (this);
//tv.setText(text);
//setContentView(tv);
TextView tv =(TextView) findViewById(R.id.tv);
tv.setText(text);
}
}
Screenshots:
set the text in textview after done the trimming operation on it,may be this will work, try it
tv.setText(text.trim());
With looking very quickly it seems like the return from the command is coming with the text.. Try and replace the new line in text before setting the text in the TextView.
i displayed a expression on the screen "2+3=?" and what i want to do is when i press 5 the question mark gets replaced by 5 and when i press # displays "correct" in green
package org.example.question;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class QuestionActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
//variable for different questions
//variable and type declaration for buttons and text
TextView display;
TextView displayGuess;
TextView displayQuestion;
TextView Correct;
TextView guess;
TextView question;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//display text on screen
displayGuess = (TextView) findViewById(R.id.Guess);
displayQuestion = (TextView) findViewById(R.id.Question);
//Creating and setting onClickerLIstener on each button
Button one = (Button) findViewById(R.id.keypad_1);
one.setOnClickListener(this);
Button two = (Button) findViewById(R.id.keypad_2);
two.setOnClickListener(this);
Button three = (Button) findViewById(R.id.keypad_3);
three.setOnClickListener(this);
Button four = (Button) findViewById(R.id.keypad_4);
four.setOnClickListener(this);
Button five = (Button) findViewById(R.id.keypad_5);
five.setOnClickListener(this);
Button six = (Button) findViewById(R.id.keypad_6);
six.setOnClickListener(this);
Button seven = (Button) findViewById(R.id.keypad_7);
seven.setOnClickListener(this);
Button eight = (Button) findViewById(R.id.keypad_8);
eight.setOnClickListener(this);
Button nine = (Button) findViewById(R.id.keypad_9);
nine.setOnClickListener(this);
Button del = (Button) findViewById(R.id.keypad_delete);
del.setOnClickListener(this);
Button zero = (Button) findViewById(R.id.keypad_0);
zero.setOnClickListener(this);
Button hash = (Button) findViewById(R.id.keypad_hash);
hash.setOnClickListener(this);
Button minus = (Button) findViewById(R.id.keypad_minus);
minus.setOnClickListener(this);
}
//method to tell what each button does
public void onClick(View argh0) {
switch(argh0.getId()) {
case R.id.keypad_hash:
if("2+3=5".equals(display.getText().toString()))
{
display.setText("CORRECT".setColor."#00ff00");
}
break;
case R.id.keypad_5:
String str5 = display.getText().toString();
display.setText(str5.replace("?","5"));
break;
case R.id.keypad_1:
display.setText("1");
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/Guess"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="Guess: "
android:textSize="25dp" />
<TextView
android:id="#+id/Question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="2+3=?"
android:textSize="25dp" />
<TextView
android:id="#+id/CORRECT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text=""
android:textSize="25dp" />
<TextView
android:id="#+id/INCORRECT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text=""
android:textSize="25dp" />
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keypad"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow>
<Button android:id="#+id/keypad_1"
android:text="1" >
</Button>
<Button android:id="#+id/keypad_2"
android:text="2" >
</Button>
<Button android:id="#+id/keypad_3"
android:text="3" >
</Button>
</TableRow>
<TableRow>
<Button android:id="#+id/keypad_4"
android:text="4" >
</Button>
<Button android:id="#+id/keypad_5"
android:text="5" >
</Button>
so far i can display the question but when i press "5" the application crashes
really will appreciate the help
I don't know whether you posted the whole code or not, but from what I see you never initialize the variable "display" (TextView display;). Then when you use it you will get a NullPointerException.
I think you missed a line like display = (TextView) findViewById(R.id.display..);