I want to change the background photo by changing the main activity java code here is what I write. When I run the application on my galaxy s3 and when I click the button, my phone get stuck and exit the app
This is the xml code:
<RelativeLayout android:id="#+id/helpLayout"
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"
android:background="#drawable/bgr"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView2"
android:textStyle="bold"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/How" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_centerVertical="true"
android:onClick="onRadioButtonClicked"
android:text="Click Me :)" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="26dp" >
<RadioButton
android:id="#+id/radio0"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Happy" />
<RadioButton
android:id="#+id/radio1"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sad" />
<RadioButton
android:id="#+id/radio2"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In Love" />
<RadioButton
android:id="#+id/radio3"
android:textColor="#808080"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Missing" />
</RadioGroup>
</RelativeLayout>
and this is the java code:
package com.example.anny;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
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 onRadioButtonClicked(View view) {
LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main);
ll.setBackgroundResource(R.drawable.p1);
}
}
In your onRadioButtonclicked your code is:
LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main);//parameter is layout instead of Id.
ll.setBackgroundResource(R.drawable.p1);
First of all you don't have a LinearLayout in your XML file. The container is a RelativeLayout. Secondly, you have provided a layout reference instead of an ID. In Android, we provide ID as R.id.IdName. The ID of your RelativeLayout in your XML code is helpLayout. So, the code should be :
RelativeLayout ll = (RelativeLayout) findViewById(R.id.helpLayout);
change your code of onRadioButtonClicked with this one,then your problem will definetly solved:
RelativeLayout ll = (RelativeLayout) findViewById(R.id.helpLayout);
ll.setBackgroundResource(R.drawable.p1);
Related
I am creating a diet app for myself and I do not know how to transfer result from one calculation which was done in a different activity and use it in another one.
To make things more complicated, I want my spinner in MoreLooseWeightDetails.class to have a list of different diet types which have different fat, proteins and carb ratio. Every time the user choose different type it should automatically change the ratio.
This is where the calculation is being done and has to be trnasfered to MoreLooseWeightDetails.class
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class LooseWeight extends AppCompatActivity {
TextView TotalCal;
EditText numb2;
Spinner ActLvl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loose_weight);
numb2 = (EditText) findViewById(R.id.value1);
ActLvl = (Spinner) findViewById(R.id.value2);
TotalCal = (TextView)findViewById(R.id.resultDisplay);
final Spinner ActLvl = (Spinner)findViewById(R.id.value2);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(LooseWeight.this, android.R.layout.simple_expandable_list_item_1,getResources().getStringArray(R.array.lvl));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ActLvl.setAdapter(myAdapter);
Button calcBtn = (Button)findViewById(R.id.result);
calcBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View view) {
float numb3 = Float.parseFloat(numb2.getText().toString());
float a = numb3 * 24;
float b = a * Float.parseFloat(ActLvl.getSelectedItem().toString());
float c = b - 500;
TotalCal.setText(Float.toString(c));
}
});
Button extra = (Button)findViewById(R.id.advance);
extra.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(),MoreLooseWeightDetails.class);
startActivity(intent);
}
});
}
}
Here is the MoreLooseWeightDetails.class
package com.fitup.fit_up;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MoreLooseWeightDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_more_loose_weight_details);
}
}
Here is the MoreLooseWeightDetails.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_more_loose_weight_details"
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.fitup.fit_up.MoreLooseWeightDetails">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/Title3"
tools:text="More"
android:textSize="36sp" />
<TextView
android:text="Select Type of Diet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Title3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp"
android:id="#+id/Info3"
android:textSize="30sp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/Info3"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:id="#+id/spinner" />
<TextView
android:text="You Should Eat :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/Info4"
android:textSize="30sp"
android:layout_below="#+id/spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Protein (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit1"
android:textSize="24sp"
android:layout_below="#+id/Info4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="78dp" />
<TextView
android:text="Fat (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unit3"
android:textSize="24sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/result1"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignLeft="#+id/result2"
android:layout_alignStart="#+id/result2" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/unit2"
android:id="#+id/result2"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/result3"
android:layout_alignStart="#+id/result3" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/unit2"
android:id="#+id/result3"
android:textSize="30sp"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/unit2"
android:layout_toEndOf="#+id/unit2" />
<TextView
android:text="Carbohydrates (grams):"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/unit2"
android:textSize="24sp"
android:layout_below="#+id/unit1"
android:layout_alignRight="#+id/unit1"
android:layout_alignEnd="#+id/unit1" />
</RelativeLayout>
I`ve been trying to build my app in android studio workbench, however I have stumbled across an error message reading "cannot cast from view to checkbox". I have tried different things to resolve the issue to no avail.
This is mainactivity code:
package com.example.sonu.animal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {
private Checkbox check1,check2,check3;
private Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addListnerOnButton() {
check1 = (Checkbox)findViewById(R.id.checkBox_dog);
check2 =(Checkbox)findViewById(R.id.checkBox2);
check3= (Checkbox)findViewById(R.id.checkBox3);
b1 = (Button)findViewById(R.id.button);
}
}
This is xml file:
<?xml version="1.0" encoding="utf-8"?>
<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.sonu.animal.MainActivity">
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="dog"
android:id="#+id/checkBox_dog"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:checked="false" />
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cat"
android:id="#+id/checkBox2"
android:layout_below="#+id/checkBox_dog"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="56dp"
android:hint="cat"
android:checked="false" />
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="cow"
android:id="#+id/checkBox3"
android:layout_below="#+id/checkBox2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp"
android:checked="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="select"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
I'm very new to this. My buttons weren't responding to clicks. I added android:onClick="onClick" to the xml file. Now i get the error below.
java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class testapp.two.MainActivity for onClick handler on view class android.widget.Button with id 'buttonMinus'
Please help, java code along with the manifest file and main xml are included below.
Thanks.
package testapp.two;
import testapp.two.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button okButton, minusButton, plusButton;
TextView textScore, scoreCard;
int score = 95;
private static final String TAG = "GolfScore";
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate started");
okButton = (Button)findViewById(R.id.buttonOK);
minusButton = (Button)findViewById(R.id.buttonMinus);
plusButton = (Button)findViewById(R.id.buttonPlus);
textScore = (TextView)findViewById(R.id.textScore);
scoreCard = (TextView)findViewById(R.id.scoreCard);
//set button listeners
okButton.setOnClickListener(this);
minusButton.setOnClickListener(this);
plusButton.setOnClickListener(this);
textScore.setText(String.valueOf(score));
Log.d(TAG, "onCreate finished");
}//onCreate
#Override
public void onClick(View v) {
Log.d(TAG, "in onCreate");
switch (v.getId()){
case R.id.buttonMinus:
score--;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonPlus:
score++;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonOK:
break;
}//end of switch
}//end of my onclick
}//end of MainActivity
<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:onClick="#+id/buttonMinus, #+id/buttonPlus, #+id/textScore"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:text="Golf Score App"
android:textAlignment="center" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:layout_marginTop="14dp"
android:baselineAligned="false"
android:gravity="fill"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonMinus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:clickable="true"
android:onClick="onClick"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="-" />
<Button
android:id="#+id/buttonPlus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:clickable="true"
android:onClick="onClick"
android:layout_weight="1"
android:text="+" />
<TextView
android:id="#+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="92"
android:textSize="20sp" />
<Button
android:id="#+id/buttonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:layout_marginRight="33dp"
android:clickable="true"
android:onClick="onClick"
android:layout_weight="1"
android:text="Ok"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="#+id/scoreCard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="22dp"
android:text="Score card info goes here" />
</RelativeLayout>
As 0xDEADC0DE says, you don't need the OnClickListener if you use the onClick attribute in your XML and vice versa.
The reason why the onClick(View v) method can't be found is the Overrideannotation. It depends to the OnClickListener this way. There is a "free" method without annotation needed that the method can be found if the onClick tag is used.
Now you have two options: get rid of the OnClicklistener and the association to the buttons or the onClick in your XML.
Edit:
Remove the onClick attributes from the RelativeLayout and the Buttons in your XML. In addition, remove clickable from your Buttons. If you set clickable = true, the View will get an empty OnClickListener automatically. This means your OnClickListener in your Activity will not be called.
Just remove android:onClick="onClick" from your XML.
Note that it is preferrable to make a single OnClickListener for each button rather than one big OnClickListener that has to check the id to decide what to do.
I am new to app making, and am making a simple calculator.
Whenever I click a button, I get the message "Unfortunately, Calculator has stopped." Any help would be greatly appreciated.
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"
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=".Calculator" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="70dp"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginLeft="70dp"
android:layout_marginTop="26dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Add"
android:onClick="DoAdd" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="90dp"
android:text="Subtract"
android:onClick="DoMinus"/>
<Button
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/button2"
android:onClick="DoTimes"
android:text="Multiply" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="94dp"
android:text="TextView" />
Java File:
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculator, menu);
return true;
}
public void doAdd(View v)
{
EditText firstNumber = (EditText)findViewById(R.id.editText1);
EditText secondNumber = (EditText)findViewById(R.id.editText2);
double x = Double.parseDouble(firstNumber.getText().toString());
double y = Double.parseDouble(secondNumber.getText().toString());
double total = x + y;
TextView answerSpace = (TextView)findViewById(R.id.textView1);
answerSpace.setText(Double.toString(total));
}
}
Thankyou.
Change your xml onClick attribute to exactly match the method name in your java code:
<Button
android:id="#+id/button1"
...
android:onClick="doAdd" />
Notice the lower-case d at the beginning.
If those two don't match, the app crashes while Android is trying to invoke the onClick method you specified.
FD_ already pointed out the mistake from your code.
But for better coding approach you can follow this. If you wish.
In your xml file, define single method name for onClick on each button ie for
addition,substration,division,multiplication.
What i mean to say...?
// For Addition....
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Add"
android:onClick="calculate" />
//Define android:onClick="calculate" for remaining 3 buttons
ie say for subtraction
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Subract"
android:onClick="calculate" />
do similarly for division and multiplication
Now, in Java File
1)Define Variables globally
2)Find their ids in onCreateMethod(), ie when the activity created.
How to do this?
public class Calculator extends Activity
{
EditText firstNumber,secondNumber;
onCreate()
{
firstNumber = (EditText)findViewById(R.id.editText1);
secondNumber = (EditText)findViewById(R.id.editText2);
}
public void calculate(View v)
{
switch(v.getId())
{
case R.id.button1:
// Do coding here addition....
break;
case R.id.button2:
// Do coding here substraction....
break;
case R.id.button3:
// Do coding here multiplication....
break;
case R.id.button4:
// Do coding here division....
break;
}
}
}
I am trying to run this app on android emulator but it shows "Unfortunately the app has stopped working":
package com.example.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
static int counter;
static Button badd, bsub;
static TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
badd=(Button)findViewById(R.id.button1);
bsub=(Button)findViewById(R.id.button2);
text=(TextView) findViewById(R.id.textView1);
badd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
text.setText("your total is :"+counter);
}
});
bsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
text.setText("your total is :"+counter);
}
});
}
#Override
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;
}
}
xml code:
<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:gravity="start"
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" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="your total is 0"
android:textSize="65sp"
/>
<Button
android:id="#+id/button1"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_gravity="center"
android:text="add"
/>
<Button
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/textView1"
android:layout_marginLeft="117dp"
android:text="subtract" />
</RelativeLayout>
Log cat shows the following:
caused by java.lang.nullpointexception
Add android:id="#+id/button2" in your second Button.
<Button
android:id="#+id/button2"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="117dp"
android:text="subtract" />
Problem was not giving any "id not defined" error is following line of code in second button view.
android:layout_alignLeft="#+id/button2"
Though you have declared it with "#+id" it will create a new id. And will not give any error in your Activity code. Change that code with what i have written above.
For the second button add
android:id="#+id/button2"
and change
android:layout_alignLeft="#+id/button1"
to
android:layout_alignLeft="#id/button1"
Change your layout as follow:
<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:gravity="start"
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" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="your total is 0"
android:textSize="65sp"
/>
<Button
android:id="#+id/button1"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_gravity="center"
android:text="add"
/>
<Button
android:id="#+id/button2"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/button1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="117dp"
android:text="subtract" />
</RelativeLayout>