如果网络无法访问,我需要在我的片段中向我的操作栏添加刷新按钮。
例如:
private void beginRetriveData() {
final StringBuilder Request = "some_url_here"
if(!isNetworkAvailable()) {
Toast.makeText(getSherlockActivity(), "No active internet connection", Toast.LENGTH_LONG).show();
// I NEED TO ADD REFRESH BUTTON TO ACTIONBAR
} else {
//AsyncTasc here
}
}
这是最好的方法吗?
我尝试了以下方法:
public MenuItem mRefreshMenuItem;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
getSupportMenuInflater().inflate(R.menu.main, menu);
mRefreshMenuItem = menu.findItem(R.id.navigation_refresh);
mRefreshMenuItem.setVisible(false);
return true;
}
@Override
public void onNetworkUnavailable()
{
mRefreshMenuItem.setVisible(true); //ERROR HERE
}
然后在片段中我调用了:
if(getActivity() instanceof FragmentsHolderActivity){
FragmentsHolderActivity myactivity = (FragmentsHolderActivity) activity;
myactivity.onNetworkUnavailable();
}
但是当我调用 onNetworkUnavailable() 时,出现了 NullPointerException 。