-2

I want to set the layout background programatically, depending on the genre of the song to be played in my application. I tried this:

public class AnswerActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play); 
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        LinearLayout root = (LinearLayout) findViewById(R.id.ctr1);
        Bundle data = getIntent().getExtras();
        if(data.getString("genre").equals("rock")){
            root.setBackgroundResource(R.drawable.wprock);
        }
        else if(data.getString("genre").equals("pop")){
            root.setBackgroundResource(R.drawable.wppop);
        }
        else if(data.getString("genre").equals("hiphop")){
            root.setBackgroundResource(R.drawable.wphiphop);
        }

    }

But it doesn't work, it's throwing a Null Pointer Exception in the root.setBackgroundResource lines, whenever anyone of these take place. Any ideas? Thanks in advance EDIT: I do have R.drawable.wprock/pop/hiphop, plus I ruled out that possiblity bevause I tried to use a color instead with the setBackgroundColor mehtod and I had the same exception.

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#1d1d1d" >

    <ImageView
        android:id="@+id/res"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"

        android:layout_marginTop="130dp"
        android:onClick="go"
        android:background="@drawable/playbtn" />

</LinearLayout>
OrangeWall
  • 79
  • 1
  • 8

2 Answers2

0

You got the error because you root is a new layout that has not been in your Activity :

LinearLayout root = new LinearLayout(this);

Remove the above code and give an android:id to the outer layout in R.layout.play, and use findViewbyId instead.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
0

First you check the root layout id of your xml if it is correct then follow the code

protected void onCreate(Bundle savedInstanceState) {
        Log.e("TRANSITION", "TRANSITIONED TO ANSWER ACTIVITY");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play); 
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        LinearLayout root = (LinearLayout) findViewById(R.id.ctr1);
        Bundle data = getIntent().getExtras();
        if(data.getString("genre").equals("rock")){
            root.setBackgroundResource(R.drawable.wprock);
        }
        else if(data.getString("genre").equals("pop")){
            root.setBackgroundResource(R.drawable.wppop);
        }
        else if(data.getString("genre").equals("hiphop")){
            root.setBackgroundResource(R.drawable.wphiphop);
        }

then check Bundle data = getIntent().getExtras(); data in bundle is null or some thing else

Your xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/ctr1" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#1d1d1d" >

<ImageView
    android:id="@+id/res"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"

    android:layout_marginTop="130dp"
    android:onClick="go"
    android:background="@drawable/playbtn" /></LinearLayout>
Omar HossamEldin
  • 3,033
  • 1
  • 25
  • 51
Furqan
  • 1,417
  • 1
  • 15
  • 22