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.
Related
I have created my first simple currency converter app which takes INR as input and which gives amount in USD as Toast. The app works fine and returns the correct result when an amount is given as input and the "Convert" button is pressed, but as soon as no input is given and then the "Convert" button is pressed, the app crashes.
Here is the code for the .xml file:
<?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"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:background="#color/colorPrimary"
android:fontFamily="monospace"
android:padding="20sp"
android:text="INR to USD converter"
android:textColor="#android:color/background_light"
android:textSize="18sp" />
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:background="#color/colorPrimary"
android:onClick="convert"
android:text="Convert" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
app:srcCompat="#drawable/currency" />
<EditText
android:id="#+id/amount"
style="#style/Widget.AppCompat.Light.AutoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="34dp"
android:ems="10"
android:hint="Amount(INR)"
android:inputType="numberDecimal" />
</RelativeLayout>
And code for the main activity:
package com.example.asus.currencyconverter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void convert(View view){
EditText amount = (EditText) findViewById(R.id.amount);
Double amountDouble= Double.parseDouble(amount.getText().toString());
Double dollar= amountDouble * 0.01575;
Toast.makeText(MainActivity.this, "$" + dollar.toString(), Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
}
}
What am I doing wrong?
Replace with this code:
public class MainActivity extends AppCompatActivity {
public EditText amount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
amount = (EditText) findViewById(R.id.amount);
}
public void convert(View view) {
String text = amount.getText().toString();
if (!text.isEmpty()) {
Double amountDouble = Double.parseDouble(text);
Double dollar = amountDouble * 0.01575;
Toast.makeText(MainActivity.this, "$"+dollar.toString(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Please enter a valid value", Toast.LENGTH_LONG).show();
}
}
}
It will show a toast to enter a value if it is empty.
That's because when you have empty EditText and call getText() it returns empty string which you put to Double.parseDouble(), which raises NumberFormatException. You want to check first is your String is empty.
public void convert(View view) {
EditText amount = (EditText) findViewById(R.id.amount);
String textAmount = amount.getText().toString();
if (!TextUtils.isEmpty(textAmount)) {
Double amountDouble = Double.parseDouble(textAmount);
Double dollar= amountDouble * 0.01575;
Toast.makeText(MainActivity.this, "$" + dollar.toString(), Toast.LENGTH_LONG).show();
}
}
I've asked a question similar to this. The point of this code is to check if a checklist is checked, if it is than it adds 1 to the score.
I've been told that the problem is that I haven't set an event. Please explain what I need to do to make it work, I'm new to android development and have been stuck on this for a few days. Please give a code example with your answer.
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* Created by Ashraf on 3/2/2016.
*/
public class check_Button extends Pop_sallian{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
// Connects The variable to an xml id
TextView output = (TextView) findViewById(R.id.output);
final int[] score = {0};
//sets the variable to 0
OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.concern:
score[0]++;
break;
case R.id.faith:
score[0]++;
break;
case R.id.respect:
score[0]++;
break;
case R.id.education:
score[0]++;
break;
case R.id.community:
score[0]++;
break;
}
}
};
// adds the variables together to form a score
if(score[0] == 0){
output.setText("Come on! Get involved, your la sallian community needs you.");
} else if(score[0] == 1){
output.setText("Good start, keep going!");
} else if(score[0] == 2){
output.setText("Room to improve but doing good!");
} else if(score[0] == 3){
output.setText("Very good, others look up to you!");
} else if(score[0] == 4){
output.setText("Wow, you really are an inspiration");
} else if(score[0] == 5){
output.setText("Excellent! You're a leader in your la sallian community. Nice work!");
} else{
output.setText("Unknown");
}
// changes the output text based on score value
}
}
^^checklist code^^
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.who);
Button today = (Button) findViewById(R.id.today);
Button sallian = (Button) findViewById(R.id.sallian);
Button how = (Button) findViewById(R.id.toBe);
Button moreInfo = (Button) findViewById(R.id.info);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop.class));
}
});
today.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_today.class));
}
});
sallian.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_sallian.class));
}
});
how.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_how.class));
}
});
moreInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_info.class));
}
});
}
}
^^Code where the button is and, when pressed calls on the checklist code, The Ok button is to close the pop up when pressed^^
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
android:elevation="8dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you a Sallian?"
android:id="#+id/textView7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you meet the following prerequisites, if you do you may be a Sallian"
android:id="#+id/textView8"
android:layout_below="#+id/textView7"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you concerened for the poor and Social Justice?"
android:id="#+id/concern"
android:textSize="18sp"
android:layout_below="#+id/textView8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have faith in the presence of God?"
android:id="#+id/faith"
android:textSize="15sp"
android:layout_below="#+id/concern"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have Respect for all people?"
android:id="#+id/respect"
android:layout_below="#+id/faith"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:textSize="15sp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you value education?"
android:id="#+id/education"
android:textSize="15sp"
android:layout_below="#+id/respect"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you inclusive in your community?"
android:id="#+id/community"
android:layout_below="#+id/education"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15sp"
android:checked="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okButton_sallian"
android:layout_below="#+id/community"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:layout_marginTop="90dp"
android:layout_marginBottom="20dp"
android:background="#FAFAFA"
android:textColor="#00E676"
android:elevation="2dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="#+id/check"
android:textColor="#00E676"
android:elevation="2dp"
android:background="#FAFAFA"
android:layout_alignTop="#+id/okButton_sallian"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/output"
android:textColor="#1eff00"
android:textSize="23sp"
android:layout_below="#+id/community"
android:layout_centerHorizontal="true"
android:layout_above="#+id/check" />
</RelativeLayout>
</ScrollView>
^^ xml code the code takes for^^
You are defining a checked listener, but doing nothing with it. You need to set it to your checkbox or checkboxes.
mCheckBox = (CheckBox) findViewById(R.id.concern);
mCheckBox.setOnCheckedChangedListener(checkedListener);
Just like you do on your buttons.
I am trying to take the value of an integer newVal (string) from one method, to use in the toast message in another method.
Although I have declared this int newVal at the top of the class, it is only showing a 0 in the toast nessage. (And even that maybe the int value).
Everything else ie checkbox results are showing up OK, but the number picker result, newVal isn't. Yet it looks correct.
Can anyone see where I'm going wrong with the toast message ie adding/finding the value of newVal??
Many thanks in advance!
Below is the MainActivity and activity_main.xml..
package com.example.frytest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.NumberPicker;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.NumberPicker.OnValueChangeListener;
public class MainActivity extends Activity {
int newVal;
private TextView tv;
Button button;
Button btnDisplay;
private CheckBox chk1, chk2, chk3, chk4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
addListenerOnChkIos();
}
public void setupUI()
{
tv = (TextView) findViewById(R.id.tvId);
NumberPicker np = (NumberPicker) findViewById(R.id.npId);
np.setOnValueChangedListener(new OnValueChangeListener()
{
public void onValueChange(NumberPicker picker, int oldVal,
int newVal)
{
tv.setText(String.valueOf(newVal));
}
});
np.setMaxValue(12);
np.setMinValue(0);
}
public void addListenerOnChkIos() {
chk1 = (CheckBox) findViewById(R.id.chk1);
chk2 = (CheckBox) findViewById(R.id.chk2);
chk3 = (CheckBox) findViewById(R.id.chk3);
chk4 = (CheckBox) findViewById(R.id.chk4);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
//Run when button is clicked
#Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("Haddock-Large : ").append(chk1.isChecked());
result.append("\nHaddock-Medium : ").append(chk2.isChecked());
result.append("\nHaddock-Small :").append(chk3.isChecked());
result.append("\nKids Corner : ").append(chk4.isChecked());
Toast.makeText(MainActivity.this, result.toString() + " " + String.valueOf(newVal),
Toast.LENGTH_LONG).show();
}
});
}
}
activity_main.xml
<LinearLayout 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="com.example.frytest.MainActivity"
android:background="#e83a43"
android:orientation="vertical" >
<CheckBox
android:id="#+id/chk1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Haddock(large) £4-40"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold" />
<NumberPicker android:id="#+id/npId"
android:layout_marginTop="5dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:textStyle="bold"
android:background="#ffffff" />
<TextView
android:id="#+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="0"
android:textSize="20sp" />
<CheckBox
android:id="#+id/chk2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Haddock(Medium) £3-00"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/chk3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Haddock(Small) £2.30"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold" />
<CheckBox
android:id="#+id/chk4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chips £1-40"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
You are never calling a method that writes a value to newVal so it would be initialized with 0 by default.
it because you never change the value of newValue, when onValueChange is invoked
public void onValueChange(NumberPicker picker, int oldVal,
int newVal)
{
MainActivity.this.newVal = newVal;
tv.setText(String.valueOf(newVal));
}
or you could simply read it back from the TextView (tv), before showing the Toast
I have 3 buttons in my application,
buttons id's = button1, button2, button3.
button1 and button3 need to do the same action, exit the game - working.
button2 need to add 1 to a counter and change a text to the counter (using for a score)
so i made an int counter=0, tried to make the script and the app crashes when I press on button2, here's the logcat and the script:
package com.example.bluetap;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FirstActivity extends Activity implements OnClickListener {
private TextView changingTextView;
private Button button1;
private Button button2;
private Button button3;
private int counter=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
changingTextView = (TextView) findViewById(R.id.changingTextView);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
}
public void onClick(View v) {
int id = v.getId();
if (id == R.id.button1) {
finish();
System.exit(0);
}
else if (id == R.id.button2) {
counter++;
changingTextView.setText(counter);
}
else if (id == R.id.button3) {
finish();
System.exit(0);
}
}
}
here's the 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"
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="com.example.bluetap.FirstActivity$PlaceholderFragment" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:minWidth="200dp"
android:text="Press The White Button"
android:textColor="#color/blue" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:background="#android:color/white"
android:minWidth="200dp"
android:text="Press The White Button"
android:textColor="#color/blue" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/button2"
android:minWidth="200dp"
android:text="Press The White Button"
android:textColor="#color/blue" />
<TextView
android:id="#+id/changingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button3"
android:layout_below="#+id/button3"
android:layout_marginLeft="80dp"
android:layout_marginTop="95dp"
android:text="0"
android:textSize="60sp" />
here's the logcat:
TextView.setText(int) expects a resource identifier. You're trying to show the actual int value in your TextView. Android looks for a resource with that value, which it cannot find. Thus crashing your application.
Use changingTextView.setText(String.valueOf(counter)); to convert it to a String first.
you have problem on:
else if (id == R.id.button2) {
counter++;
changingTextView.setText(counter);
}
you can't set integer value to TextView, so you need cast that to String.
you can use:
changingTextView.setText(""+counter);
or
changingTextView.setText(Integer.toString(counter));
or
changingTextView.setText(String.valueOf(counter));
Header 1
This is the code that i have used. i want to add a name to a database. when i enter the name into the EditText, it accepts and when i click the submit button, it shows the message "App_name has stopped unfortunately"
activity_main.xml
<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_marginTop="70dp"
android:text="USERNAME"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="28dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="50dp"
android:text="SUBMIT" />
</RelativeLayout>`
Header 1
Main_Activity.java
package com.example.dbconcept;
import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
Button b;
String user;
EditText usr;
SQLiteDatabase db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
try{
db=openOrCreateDatabase("Mydb",MODE_PRIVATE,null);
db.execSQL("create table table1(user varchar(50))");
}
catch(Exception e)
{
}
b.setOnClickListener(this);
}
public void onClick(View arg0) {
user=usr.getText().toString();
db.execSQL("insert into table1 values('"+user+"')");
}
}
you have not intialized usr
just add
usr = (EditText) findViewById(R.id.editText1)
to your onCreate
You forget to intialized the usr EditText.
usr = (EditText) findViewById(R.id.editText1)
Add this in onCreate(...) after setContentView(R.layout.activity_main);