title: Set Background Image dynamically of One Activity from Another Activity?
body: <p>I am trying to set the background image of one activity from another activity</p>
  
  <p>Activity 1 - is a horizontal scroll view where i am loading a few images for the user to choose using this method</p>
  
  <pre><code>//set an onclick listener for imageView
           imageView.setOnClickListener(new OnClickListener(){
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  Toast.makeText(getApplicationContext(), "Clicked - " + imageNames[i] , Toast.LENGTH_LONG).show();
                  NewNote note = new NewNote();
                  note.setMyBackground(images[i]);
                  finish();
              }
  
           });
  </code></pre>
  
  <p>Here images[i] is definitely not null since ive used it previously to load the HorizontalScrollView</p>
  
  <p>Activity 2 - NewNote - The Activity whose background I am trying to set using the following method</p>
  
  <pre><code>public void setMyBackground(Drawable d){
          FrameLayout fl = (FrameLayout)findViewById(R.id.FLNote);
          fl.setBackground(d);
      }
  </code></pre>
  
  <p>If i comment out the note.setMyBackground(images[i]); there is no null pointer exception </p>
  
  <p>Other wise i get the following Logcat</p>
  
  <pre><code>01-06 03:58:34.150: E/AndroidRuntime(1430): FATAL EXCEPTION: main
  01-06 03:58:34.150: E/AndroidRuntime(1430): Process: com.example.mynote, PID: 1430
  01-06 03:58:34.150: E/AndroidRuntime(1430): java.lang.NullPointerException
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.app.Activity.findViewById(Activity.java:1883)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at com.example.mynote.activities.NewNote.setMyBackground(NewNote.java:50)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at com.example.mynote.activities.NoteBackGroundPicker$1.onClick(NoteBackGroundPicker.java:65)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.view.View.performClick(View.java:4424)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.view.View$PerformClick.run(View.java:18383)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.os.Handler.handleCallback(Handler.java:733)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.os.Handler.dispatchMessage(Handler.java:95)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.os.Looper.loop(Looper.java:137)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.app.ActivityThread.main(ActivityThread.java:4998)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invoke(Method.java:515)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  01-06 03:58:34.150: E/AndroidRuntime(1430):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>NewNote.java</p>
  
  <pre><code>package com.example.mynote.activities;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.drawable.Drawable;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  
  import com.example.mynote.R;
  
  public class NewNote extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.note_layout);
          //getActionBar().setDisplayHomeAsUpEnabled(true);
          getActionBar().setHomeButtonEnabled(true);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.new_note_action_bar_items, menu);
          return super.onCreateOptionsMenu(menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item){
          switch(item.getItemId()){
          case R.id.action_done:
              finish();
              return true;
          case R.id.action_bgchooser:
              Log.d("ACTION","BackgroundChooser Activity");
              Intent i2 = new Intent(NewNote.this,NoteBackGroundPicker.class);
              startActivity(i2);
              return true;
          default:
              return super.onOptionsItemSelected(item);
          }
      }
  
      public void setMyBackground(Drawable d){
          FrameLayout fl = (FrameLayout)findViewById(R.id.FLNote);
          fl.setBackground(d);
      }
  
  }
  </code></pre>
  
  <p>note_layout.xml</p>
  
  <pre><code>&lt;FrameLayout 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/diary"
      tools:context=".NewNote"
      android:id="@+id/FLNote"
       &gt;
  
      &lt;ImageView
          android:id="@+id/imageView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="top"
          android:src="@drawable/icon" /&gt;
  
      &lt;ScrollView
          android:id="@+id/scrollView1"
          android:layout_width="match_parent"
          android:layout_height="346dp"
          android:layout_gravity="bottom" &gt;
  
          &lt;LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" &gt;
  
           &lt;EditText
               android:id="@+id/editText1"
               android:layout_width="match_parent"
               android:layout_height="290dp"
               android:ems="10" &gt;
  
              &lt;/EditText&gt;
  
           &lt;Button
               android:id="@+id/button1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center"
               android:text="Save" /&gt;
  
          &lt;/LinearLayout&gt;
  
  
  
      &lt;/ScrollView&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>How can i get rid of the null pointer exception????</p>