title: how to set imagebutton src in onCreateView?
body: <p>In my app, I have several <code>ImageButton</code>s that are controlled by their respective <code>StateListDrawable</code>s. I use them as tab menu at the bottom of the activity to switch fragments. They change states just fine (when pressed, focused, default). Switching of fragments also works okay. However, I don't know how can I make a button be highlighted (for example, use the image set when pressed) when the fragment it represents is currently shown. </p>
  
  <p>I've tried setting the <code>BackgroundResource</code>, <code>ImageDrawable</code>, <code>ImageResource</code> during runtime but I am getting a <code>NullPointerException</code> everytime I launch my app.</p>
  
  <p>Here's my log if this helps</p>
  
  <pre><code>05-16 14:15:19.489: E/AndroidRuntime(4800): FATAL EXCEPTION: main
  05-16 14:15:19.489: E/AndroidRuntime(4800): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meralco/com.meralco.pms.TestPMSActivity}: java.lang.NullPointerException
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.os.Handler.dispatchMessage(Handler.java:99)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.os.Looper.loop(Looper.java:130)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.app.ActivityThread.main(ActivityThread.java:3687)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at java.lang.reflect.Method.invoke(Method.java:507)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at dalvik.system.NativeStart.main(Native Method)
  05-16 14:15:19.489: E/AndroidRuntime(4800): Caused by: java.lang.NullPointerException
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at com.meralco.pms.HomeFragment.onCreateView(HomeFragment.java:19)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at  android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.app.Activity.performStart(Activity.java:3791)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
  05-16 14:15:19.489: E/AndroidRuntime(4800):     ... 11 more
  </code></pre>
  
  <p>Here's HomeFragment.xml:</p>
  
  <pre><code>public class HomeFragment extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.home, container, false);
      ImageButton btn_sin = (ImageButton) view.findViewById(R.drawable.button_sin);
      btn_sin.setBackgroundResource(R.drawable.pp_sin_1);
      return view;
      }
  }
  </code></pre>
  
  <p>home.xml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:gravity="center_horizontal|center_vertical"  &gt;
  
      &lt;TextView android:id="@+id/home_text1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/home_text1"
          android:textSize="20sp"
          android:textStyle="bold"
          android:textColor="#000000"
          android:padding="10dp"
          android:gravity="center_horizontal|center_vertical" /&gt;
  
      &lt;TextView android:id="@+id/home_text2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/home_text2"
          android:textSize="12sp"
          android:textColor="#000000"
          android:paddingLeft="10dp"
          android:paddingRight="10dp"
          android:gravity="center_horizontal|center_vertical"/&gt;
  
      &lt;TextView android:id="@+id/home_text3"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/home_text3"
          android:textSize="10sp"
          android:textColor="#000000"
          android:padding="15dp"
          android:textStyle="italic"
          android:gravity="center_horizontal|center_vertical"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>main.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;ImageView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="0.3"
          android:src="@drawable/phone_portrait_bg_01" /&gt;
  
      &lt;FrameLayout
          android:id="@+id/fragment_content"
          android:layout_weight="7"
          android:layout_width="match_parent"
          android:layout_height="0dp"&gt;
          &lt;ImageView android:src="@drawable/phone_portrait_bg_02"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:scaleType="centerCrop"/&gt;
      &lt;/FrameLayout&gt;
  
      &lt;fragment class="com.meralco.pms.ButtonsHandler"
          android:id="@+id/titles" 
          android:layout_weight="1"
          android:layout_width="match_parent"
          android:layout_height="0dp" &gt;
          &lt;!-- Preview: layout=@layout/buttons --&gt;
      &lt;/fragment&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>ButtonsHandler.java</p>
  
  <pre><code>public class ButtonsHandler extends Fragment{   
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
          // Inflate the layout for this fragment
  
          return inflater.inflate(R.layout.buttons, container, false);
      }
  }
  </code></pre>
  
  <p>buttons.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center_horizontal|center_vertical"&gt;
  
      &lt;ImageButton
          android:id="@+id/button_sin"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:background="#000000"
          android:src="@drawable/button_sin"
          android:onClick="buttonClick" /&gt;
  
      &lt;ImageButton
          android:id="@+id/button_city"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:background="#000000"
  
          android:src="@drawable/button_city"
          android:onClick="buttonClick" /&gt;
  
      &lt;ImageButton
          android:id="@+id/button_date"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:background="#000000"
          android:src="@drawable/button_date"
          android:onClick="buttonClick" /&gt;
  
      &lt;ImageButton
          android:id="@+id/button_all"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:background="#000000"
          android:src="@drawable/button_all"
          android:onClick="buttonClick" /&gt;
  
      &lt;ImageButton
          android:id="@+id/button_help"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:background="#000000"
          android:src="@drawable/button_help"
          android:onClick="buttonClick" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>TIA!</p>