尝试将SwitchCompat设置为操作栏时出错。
这是我的abswitch.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/abswitch"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v7.widget.SwitchCompat
android:id="@+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="" />
</RelativeLayout>
这是我的profile_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/myswitch"
android:title=""
app:showAsAction="always"
android:actionLayout="@layout/abswitch"/>
</menu>
这里是来自片段
的onCreateOptionsMenu@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.profile_menu, menu);
MenuItem item = menu.findItem(R.id.myswitch);
item.setActionView(R.layout.abswitch);
SwitchCompat switchCompat = (SwitchCompat) item.getActionView();
super.onCreateOptionsMenu(menu, inflater);
}
每次尝试启动我的应用时都会出现错误
Process: ru.dennes544.vkapp, PID: 15974
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.SwitchCompat
at ru.dennes544.vkapp.Profile.onCreateOptionsMenu(Profile.java:216)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1868)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1989)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:276)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
at android.support.v7.widget.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:49)
at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:459)
at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:69)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
提前致谢。
P.S。抱歉,对于pastebin链接,stackoverflow没有正确格式化xml的
编辑:想通了。
在profile_menu.xml中更改
<item
android:id="@+id/toggleservice"
app:actionViewClass="android.support.v7.widget.SwitchCompat"
app:showAsAction="always"
android:title=""/>
并在Profile.class中
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.profile_menu, menu);
MenuItem item = menu.findItem(R.id.toggleservice);
SwitchCompat switchCompat = (SwitchCompat) MenuItemCompat.getActionView(item);
super.onCreateOptionsMenu(menu, inflater);
}
现在它的工作。谢谢大家!
答案 0 :(得分:0)
而不是:
SwitchCompat switchCompat = (SwitchCompat) MenuItemCompat.getActionView(item);
你应该这样做:
RelativeLayout relativeLayout= (RelativeLayout) MenuItemCompat.getActionView(item);
SwitchCompat switchCompat = (SwitchCompat) relativeLayout.findViewById(R.id.switchForActionBar);
左右