【问题标题】:Hiding a MenuItem button and showing a ProgressBar in an ActionBar隐藏 MenuItem 按钮并在 ActionBar 中显示 ProgressBar
【发布时间】:2017-05-31 12:23:17
【问题描述】:

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

无法开始活动 组件信息{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);
    }

}

menu_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>

refresh_menuitem.xml

<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>

【问题讨论】:

    标签: java android android-activity menuitem android-progressbar


    【解决方案1】:

    onCreateOptionsMenuonCreate 和你的第一个refreshData() 调用之后执行,所以'itemBtnRefresh' 仍然为空。

    尝试在itemBtnRefreshonCreateOptionsMenu 中初始化之后立即调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多