0

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);

    }
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Shalom Balulu
  • 379
  • 1
  • 9
  • 20

2 Answers2

1

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);
Naddy
  • 2,664
  • 6
  • 25
  • 38
0

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);
Hamad
  • 5,096
  • 13
  • 37
  • 65