切换操作栏无法正常工作

时间:2015-03-18 10:45:05

标签: android android-fragments android-actionbar

我想将Switch置于Action Bar并为Switch OnCheckedChangeListener设置。 这是我的代码: 的 check_words_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/switch_action_bar"/>
</menu>

switch_action_bar.xml

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

<Switch
    android:id="@+id/switchForActionBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</RelativeLayout>

SomeFragment.java

public class SomeFragment extends Fragment implements CompoundButton.OnCheckedChangeListener {
// ...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.check_words_menu, menu);
    MenuItem item = menu.findItem(R.id.myswitch);
    item.setActionView(R.layout.switch_action_bar);
    Switch mySwitch = (Switch) item.getActionView();
    mySwitch.setOnCheckedChangeListener(this);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // some code
}

运行后我得到ClassCastException:

  

03-18 13:39:55.750 7206-7206 / com.chubarich.android.translator E / AndroidRuntime:FATAL EXCEPTION:main       java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.Switch               在com.chubarich.android.translator.CheckWordsFragment.onCreateOptionsMenu(CheckWordsFragment.java:65)               在android.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1871)               在android.app.Activity.onCreatePanelMenu(Activity.java:2714)               在android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)               在android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)               在android.support.v7.app.ActionBarActivityDelegate $ 1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)               在android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)               在android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)               在android.support.v7.app.ActionBarActivityDelegateBase.access $ 100(ActionBarActivityDelegateBase.java:79)               在android.support.v7.app.ActionBarActivityDelegateBase $ 1.run(ActionBarActivityDelegateBase.java:115)               在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725)               在android.view.Choreographer.doCallbacks(Choreographer.java:555)               在android.view.Choreographer.doFrame(Choreographer.java:524)               在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711)               在android.os.Handler.handleCallback(Handler.java:615)               在android.os.Handler.dispatchMessage(Handler.java:92)               在android.os.Looper.loop(Looper.java:137)               在android.app.ActivityThread.main(ActivityThread.java:4954)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:798)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)               在dalvik.system.NativeStart.main(本地方法)

SomeFragment.java 中的65行是Switch mySwitch = (Switch) item.getActionView();。 请告诉我,我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

 Switch mySwitch = (Switch) item.getActionView();

将以上行更改为

View actionView=item.getActionView();
Switch mySwitch = (Switch)actionView.findViewById(R.id.switch_id);