在我的应用程序中,我有几个ImageButton
由各自的 s 控制的StateListDrawable
s。我将它们用作活动底部的选项卡菜单来切换片段。它们可以很好地改变状态(按下时,聚焦,默认)。片段的切换也可以。但是,我不知道如何在当前显示按钮所代表的片段时突出显示按钮(例如,按下时使用图像集)。
我尝试在运行时设置BackgroundResource
, ImageDrawable
,但每次启动我的应用程序时都会ImageResource
得到一个。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;
}
}
主页.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>
主.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>
按钮处理程序.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);
}
}
按钮.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>
蒂亚!