如何在onCreateView中设置imagebutton src?

时间:2012-05-16 06:56:04

标签: android

在我的应用中,我有几个ImageButton由各自的StateListDrawable控制。我将它们用作活动底部的标签菜单来切换片段。它们可以很好地改变状态(按下,聚焦,默认)。切换片段也可以正常工作。但是,我不知道如何在当前显示它所代表的片段时突出显示按钮(例如,在按下时使用图像集)。

我已尝试在运行时设置BackgroundResourceImageDrawableImageResource,但每次启动应用时都会收到NullPointerException

如果这有帮助,这是我的日志

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

这是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!

5 个答案:

答案 0 :(得分:11)

ImageButton btn = (ImageButton)findViewById(R.id.button1);
btn.setImageResource(R.drawable.newimage);

答案 1 :(得分:5)

试试这种方式

ImageButton btn_sin = (ImageButton) view.findViewById(R.id.button_sin);
btn_sin.setImageResource(R.drawable.pp_sin_1);

答案 2 :(得分:0)

您无法访问 HomeFragment 中的 button_sin ,因为 HomeFragment 使用 home.xml 布局。

哟可以做下一个

public class ButtonsHandler extends Fragment{   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View v = inflater.inflate(R.layout.buttons, container, false);

        ImageButton btn_sin = (ImageButton) view.findViewById(R.id.button_sin);
        btn_sin.setBackgroundResource(R.drawable.pp_sin_1);

        return 
    }
}

答案 3 :(得分:0)

要将片段与活动相关联,请查看

http://developer.android.com/guide/topics/fundamentals/fragments.html#CommunicatingWithActivity

首先,您将ButtonsHandler与您的活动连接起来。然后在HomeFragment中创建一个方法,您只需要从您的活动中调用此方法。

答案 4 :(得分:0)

问题在于你的代码

   ImageButton btn_sin = (ImageButton) view.findViewById(R.drawable.button_sin);

将此更改为

   ImageButton btn_sin = (ImageButton) view.findViewById(R.id.button_sin);

R.id.button_sin必须是布局中的imagebutton id