Java 隐藏菜单项按钮并在操作栏中显示进度栏

Java 隐藏菜单项按钮并在操作栏中显示进度栏,java,android,android-activity,menuitem,android-progressbar,Java,Android,Android Activity,Menuitem,Android Progressbar,我有一个包含片段的活动。操作栏中有一个刷新按钮,按下该按钮时,我希望转换为进度栏以显示活动。虽然我得到了以下错误: 无法启动活动 ComponentInfo{com.mycompany.myapp.Views.MasterDetails.MasterActivity}: java.lang.NullPointerException:尝试调用接口方法 'android.view.MenuItem 空值上的android.view.MenuItem.setActionView(android.vie

我有一个包含片段的活动。操作栏中有一个刷新按钮,按下该按钮时,我希望转换为进度栏以显示活动。虽然我得到了以下错误:

无法启动活动 ComponentInfo{com.mycompany.myapp.Views.MasterDetails.MasterActivity}: java.lang.NullPointerException:尝试调用接口方法 'android.view.MenuItem 空值上的android.view.MenuItem.setActionView(android.view.view)“” 对象引用

在这一行:
itemBtnRefresh.setActionView(abprogress)一旦后台操作开始(“refreshData()”)

关于正确地将按钮替换为不确定的进度条,有什么帮助吗

public class MasterActivity extends AppCompatActivity  {

    private String TAG = getClass().getSimpleName();

    private Toolbar mToolbar;
    private ActionBar ab;
    private MenuItem itemBtnRefresh;
    private View abprogress;

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

        mToolbar = (Toolbar) findViewById(R.id.masterToolBar);
        setSupportActionBar(mToolbar);
        ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);

        LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        abprogress = inflater.inflate(R.layout.refresh_menuitem, null);

        refreshData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        invalidateOptionsMenu();
        getMenuInflater().inflate(R.menu.menu_master, menu);
        itemBtnRefresh = menu.findItem(R.id.master_refresh_action);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.master_refresh_action:
                refreshData();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void refreshData() {

        //... do a background task
        // show the progressbar and hide the refresh button

        itemBtnRefresh.setActionView(abprogress);


        //.. finish the background task
        // hide progressbar and show button
        itemBtnRefresh.setActionView(null);
    }

}
菜单\u master.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/master_refresh_action"
        android:icon="@drawable/ic_refresh"
        android:title="Refresh"
        app:showAsAction="ifRoom"/>
</menu>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:addStatesFromChildren="true"
              android:focusable="true"
              android:paddingLeft="4dp"
              android:paddingRight="4dp"
              android:gravity="center"
              android:orientation="horizontal"
              style="?attr/actionButtonStyle">
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        style="@android:style/Widget.Holo.ProgressBar.Small"/>

</LinearLayout>

刷新\u menuitem.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/master_refresh_action"
        android:icon="@drawable/ic_refresh"
        android:title="Refresh"
        app:showAsAction="ifRoom"/>
</menu>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:addStatesFromChildren="true"
              android:focusable="true"
              android:paddingLeft="4dp"
              android:paddingRight="4dp"
              android:gravity="center"
              android:orientation="horizontal"
              style="?attr/actionButtonStyle">
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        style="@android:style/Widget.Holo.ProgressBar.Small"/>

</LinearLayout>

oncreateoptions菜单
onCreate
和第一次
refreshData()
调用之后执行,因此“itemBtnRefresh”仍然为空

oncreateoptions菜单
中初始化
itemBtnRefresh
后尝试调用它