1. ホーム
  2. アンドロイド

getMenuInflater()を使用すると、メニューオプションが表示されない

2022-03-23 11:59:04

回避策

でメニューバーのアクティビティ(PullFreshActivityは現在のアクティビティ名)を使用することになります。

public class PullFreshActivity extends Activity

に変更する

public class PullFreshActivity extends AppCompatActivity

実行中の結果

MY_APPはプロジェクト名で、MY_APPのコードは以下の通りです。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >
    <item android:id="@+id/action_material"
        android:title="Material"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item android:id="@+id/action_circles"
        android:title="Circles"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item android:id="@+id/action_water_drop"
        android:title="WaterDrop"
        android:orderInCategory="100"
        app:showAsAction="never"/>

    <item android:id="@+id/action_ring"
        android:title="Ring"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item android:id="@+id/action_smartisan"
        android:title="Smartisan"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>


メニューバーPullFreshActivity on menuのコードを使用したページは以下のようになります。

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.pull_type, menu);//R.menu.pull_type is the menu style, for the previous code
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) { // set the content of different options for the menu
        int id = item.getItemId();
        //5 different styles
        switch (id){
            case R.id.action_material:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_MATERIAL); //change the theme style
                return true;
            case R.id.action_circles:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_CIRCLES); 
                return true;
            case R.id.action_water_drop:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_WATER_DROP);
                return true;
            case R.id.action_ring:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_RING);
                return true;
            case R.id.action_smartisan:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_SMARTISAN);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }


メニューバーPullFreshActivityを使用したページ フルコードです。

package com.example.my_app;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.baoyz.widget.PullRefreshLayout;
import java.lang.reflect.Field;

public class PullFreshActivity extends AppCompatActivity {

    private PullRefreshLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.pull_fresh); //the layout file used by the current page

        String[] array = new String[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = "test " + i;
        }

        final ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(new android.widget.ArrayAdapter(this, android.R.layout.simple_list_item_1, array));

        layout = (PullRefreshLayout) findViewById(R.id.prl_view);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                        showDialog();
                    }
                }, 3000);
            }
        });

    }

    private void showDialog(){
        Builder builder=new AlertDialog;
// builder.setIcon(R.drawable.picture);
        builder.setTitle("Alert message");
        builder.setMessage("Refresh successful! ");
        builder.setPositiveButton("I got it",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
        AlertDialog dialog=builder.create();
        dialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.pull_type, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //5 different styles
        switch (id){
            case R.id.action_material:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_MATERIAL);
                return true;
            case R.id.action_circles:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_CIRCLES);
                return true;
            case R.id.action_water_drop:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_WATER_DROP);
                return true;
            case R.id.action_ring:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_RING);
                return true;
            case R.id.action_smartisan:
                layout.setRefreshStyle(PullRefreshLayout.STYLE_SMARTISAN);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private void getOverflowMenu() {
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if(menuKeyField ! = null) {
                menuKeyField.s