Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set imagebutton src in onCreateView?

Tags:

android

In my app, I have several ImageButtons that are controlled by their respective StateListDrawables. 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.

I've tried setting the BackgroundResource, ImageDrawable, ImageResource during runtime but I am getting a NullPointerException everytime I launch my app.

Here's my log if this helps

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

Here's HomeFragment.xml:

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

home.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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"  >

    <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" />

    <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"/>

    <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"/>

</LinearLayout>

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:src="@drawable/phone_portrait_bg_01" />

    <FrameLayout
        android:id="@+id/fragment_content"
        android:layout_weight="7"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <ImageView android:src="@drawable/phone_portrait_bg_02"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"/>
    </FrameLayout>

    <fragment class="com.meralco.pms.ButtonsHandler"
        android:id="@+id/titles" 
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp" >
        <!-- Preview: layout=@layout/buttons -->
    </fragment>

</LinearLayout>

ButtonsHandler.java

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

buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <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" />

    <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" />

    <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" />

    <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" />

    <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" />

</LinearLayout>

TIA!

like image 656
yojoannn Avatar asked May 16 '12 06:05

yojoannn


2 Answers

ImageButton btn = (ImageButton)findViewById(R.id.button1);
btn.setImageResource(R.drawable.newimage);
like image 167
Android Avatar answered Oct 27 '22 08:10

Android


try this way

ImageButton btn_sin = (ImageButton) view.findViewById(R.id.button_sin);
btn_sin.setImageResource(R.drawable.pp_sin_1);
like image 44
Khan Avatar answered Oct 27 '22 09:10

Khan