无法收到片段中的菜单项单击

时间:2018-08-29 06:28:34

标签: android android-fragments android-actionbar android-toolbar

我在Fragment中将ToolBar制作为ActionBar。我可以添加ActionBar菜单项,但是单击任何ActionBar菜单项时都无法收到点击响应。

我已经阅读了很多类似的问题,我尝试了所有问题,但仍然遇到问题,因此在这里提出了一个问题。

片段

public class DetailFragment extends Fragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setHasOptionsMenu(true);
        return inflater.inflate(R.layout.project_detail, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mToolBar = (Toolbar)view.findViewById(R.id.tb_toolbar); 

        ((AppCompatActivity)getActivity()).setSupportActionBar(mToolBar);
        mToolBar.setTitle(R.string.project_details);
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.project_detail_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.options:
                // do something
                break;
        }
        return true;
    }

在我上面的代码中, onOptionsItemSelected 甚至没有被调用。 任何指针为什么?

我要对该片段进行夸大的活动-

public class TestJust extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_just);

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container, new ProjectDetailFragment());
        ft.commit();
    }
}

project_detail_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/options"
        android:title="options"
        android:icon="@android:drawable/ic_menu_more"
        app:showAsAction="always"/>

</menu>

4 个答案:

答案 0 :(得分:0)

尝试一下

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear()   
    inflater.inflate(R.menu.project_detail_menu, menu);
    }


   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
        case R.id.options:
            // do something
              return true;
        default:
        return super.onOptionsItemSelected(item);
    }

答案 1 :(得分:0)

com/grp

答案 2 :(得分:0)

解决方案:

您正在onCreateView()中呼叫Updating Homebrew... ==> Auto-updated Homebrew! Updated 2 taps (homebrew/core, homebrew/cask). ==> New Formulae topgrade ==> Updated Formulae bison ✔ azure-cli bwfmetaedit erlang@20 ghostscript jfrog-cli-go ldc p11-kit smlnj youtube-dl sphinx-doc ✔ babel crystal fauna-shell helmfile juju mkvtoolnix pyside tarsnap-gui alexjs bat doctl fortio influxdb kore nginx re2c thors-serializer Warning: kubernetes-cli 1.11.2 is already installed and up-to-date To reinstall 1.11.2, run `brew reinstall kubernetes-cli`

请在onCreate()中调用该方法

赞:

InvalidKey: A key should contain at least a kind.
    at keyToKeyProto (/Volumes/Drive B/dev/zapi/node_modules/@google-cloud/datastore/src/entity.js:696:11)
    at Array.map (<anonymous>)
    at DatastoreRequest.createReadStream (/Volumes/Drive B/dev/zapi/node_modules/@google-cloud/datastore/src/request.js:226:23)
    at DatastoreRequest.get (/Volumes/Drive B/dev/zapi/node_modules/@google-cloud/datastore/src/request.js:461:8)
    at /Volumes/Drive B/dev/zapi/node_modules/@google-cloud/common/build/src/util.js:681:32
    at new Promise (<anonymous>)
    at Datastore.wrapper [as get] (/Volumes/Drive B/dev/zapi/node_modules/@google-cloud/common/build/src/util.js:662:20)
    at fetchEntity (/Volumes/Drive B/dev/zapi/node_modules/gstore-node/lib/model.js:204:36)
    at Function.get (/Volumes/Drive B/dev/zapi/node_modules/gstore-node/lib/model.js:174:16)
    at Promise (/Volumes/Drive B/dev/zapi/node_modules/gstore-node/lib/utils.js:39:35)
    at new Promise (<anonymous>)
    at Function.wrapper (/Volumes/Drive B/dev/zapi/node_modules/gstore-node/lib/utils.js:27:16)
    at resolve (/Volumes/Drive B/dev/zapi/graphql/mutations/user/linkConsult.js:101:44)

在onCreate()中调用此方法可确保菜单必须参与Fragment。

然后

setHasOptionsMenu(true);

最后,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

现在应该可以工作。

答案 3 :(得分:0)

onCreateOptionMenu中的

初始化选项,然后检查它。您只需要充气菜单,不使用初始化选项,先初始化然后使用它即可。

like:MenuItem item = menu.findItem(R.id.option);

  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater); 
        MenuItem item = menu.findItem(R.id.option);
}