조각이 포함 된 활동이 있습니다. 작업 표시 줄에 새로 고침 버튼이 있습니다.이 버튼을 누르면 진행률 표시 줄로 전환하여 작업을 표시하고 싶습니다. 이 라인에MenuItem 버튼 숨기기 및 ActionBar에 ProgressBar 표시
Unable to start activity ComponentInfo{com.mycompany.myapp.Views.MasterDetails.MasterActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setActionView(android.view.View)' on a null object reference
: 나는 다음과 같은 오류가 발생하고 있지만 배경 작업으로 즉시 itemBtnRefresh.setActionView(abprogress);
는 ("refreshData()")
제대로 불확정 ProgressBar의 버튼을 교환에 어떤 도움을 시작?
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 'itemBtnRefresh' 여전히 널 있도록 onCreate
및 첫 번째 refreshData()
호출 후 실행
<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>