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>
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>
So, when I press the Increment or Decrement button on my device, the app force closes. I tried using debugger but can't seem to figure out the problem. It is basically a single screen app that shows Order summary.
Here is the XML Code:
<ScrollView 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/name_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="QUANTITY!" />
<CheckBox
android:id="#+id/whipped_cream_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:text="Whipped Cream"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"
android:text="-" />
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="0"
android:textColor="#android:color/black"
android:textSize="16sp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:text="Order" />
<TextView
android:id="#+id/order_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"/>
</LinearLayout>
</ScrollView>
The JAVA code:
package com.example.android.timeforacoffee;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int quantity=0;
String priceMessage="Name: Moksh \n" + "Your total is ₹" +quantity;
public void displayQuanity(int quantity) {
TextView quantity_value=(TextView) findViewById(R.id.quantity_text_view);
quantity_value.setText(quantity);
}
/**
*This method is called when the - button is pressed.
*/
public void decrement(View view) {
displayQuanity(quantity - 1);
}
/**
*
* This method is called when the + button is clicked.
*/
public void increment(View view) {
displayQuanity(quantity + 1);
}
public void displayMessage(String priceMessage) {
TextView order_summary=(TextView) findViewById(R.id.order_summary);
order_summary.setText(priceMessage);
}
/**
* This method is called when the Order button is clicked.
*/
public void submitOrder(View view) {
displayMessage(priceMessage);
}
}
I think the issue is in the below line of code
quantity_value.setText(quantity);
Note that when you pass any integer to setText function android considers the integer as an reference to a string reource and searches for that string in the reource file, if that string is not found your app will crash with android.content.res.Resources$NotFoundException.
You can simply fix this issue by explicitly parsing the integer as string as below
quantity_value.setText(String.valueOf(quantity));
Am unable to import Android.view when creating methods for changeColor() in my main activity. Instead it ask me to import ViewCompat and there isn't a getId() in the ViewCompat.
changeColor is an onclick method in my main xml.
Kindly need your advice to get the Resource Id of my radio button to change my text color.
My code is as enclosed below.
package com.example.rigmiklos.yt30sharedpreference;
import android.graphics.Color;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.EditText;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
EditText message;
SeekBar seekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = (EditText) findViewById(R.id.message);
seekBar = (SeekBar) findViewById(R.id.seekbar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
message.setTextSize(TypedValue.COMPLEX_UNIT_PX, progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void changeColor(View view)
{
switch(view.getId())
{
case R.id.id_red_colour:
message.setTextColor(Color.parseColor("&FC0116"));
break;
case R.id.id_blue_colour:
message.setTextColor(Color.parseColor("&0810F5"));
break;
case R.id.id_green_colour:
message.setTextColor(Color.parseColor("&03FF20"));
break;
}
}
}
XML code
<?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"
android:background="#000000"
tools:context="com.example.rigmiklos.yt30sharedpreference.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Message"
android:background="#FFFFFF"
android:id="#+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/message"
android:background="#FFFFFF"
android:layout_below="#+id/textView3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Adjust Font Size"
android:layout_marginTop="22dp"
android:layout_below="#+id/message"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#FFFFFF"
android:id="#+id/textView" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/seekbar"
android:background="#FFFFFF"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Font Color"
android:background="#FFFFFF"
android:layout_below="#+id/seekbar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="22dp"
android:id="#+id/textView2" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_below="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/radioGroup">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Red"
android:id="#+id/id_red_colour"
android:onClick="changeColor"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Blue"
android:id="#+id/id_blue_colour"
android:onClick="changeColor"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Green"
android:id="#+id/id_green_colour"
android:onClick="changeColor"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Setting"
android:background="#FFFFFF"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Setting"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/radioGroup"
android:layout_alignEnd="#+id/radioGroup"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp"
android:id="#+id/button" />
</RelativeLayout>
i import Android.view.View to troubleshoot it. Still if anyone got other alternate answer or view, kindly share here.
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 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);