在进度条的布局片段 [英] ProgressBar in Fragment Layout
本文介绍了在进度条的布局片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法访问我在onCreateView在片段的AsyncTask的创建片段的进度条。其造成的误差进度在preExecute是给NullPointerException异常。
我创建进度之前,我打电话的AsyncTask。所以,可能是什么问题?
公共类HomeFragment扩展片段{ 进度进度; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ 查看查看= inflater.inflate(R.layout.home_fragment,集装箱,FALSE); 进度=(进度)view.findViewById(R.id.progressbar); 新getAnnouncements()执行(); 返回视图。
} 公共静态的newInstance HomeFragment(){
HomeFragment片段=新HomeFragment(); 返回片段;
} 私有类getAnnouncements扩展的AsyncTask<太虚,太虚,太虚> { @覆盖
在preExecute保护无效(){
super.on preExecute();
progressBar.setVisibility(View.VISIBLE); } @覆盖
保护无效doInBackground(虚空...... PARAMS){
返回null;
} @覆盖
保护无效onPostExecute(无效参数){
progressBar.setVisibility(View.GONE); } }
}
home_fragment.xml
<进度
机器人:ID =@ + ID /进度
风格=机器人:ATTR / progressBarStyleLarge
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerInParent =真
机器人:layout_marginTop =24dp
机器人:知名度=水涨船高/>
解决方案
进度=(进度)view.findViewById(R.id.progressBar);
I can't access the progress bar that I created in onCreateView of fragment in the Asynctask of fragment. Its caused by error is progressbar in onPreExecute is giving NullPointerException. I created progressBar before I call asynctask. So what might be the problem?
public class HomeFragment extends Fragment {
ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);
progressBar = (ProgressBar) view.findViewById(R.id.progressbar);
new getAnnouncements().execute();
return view;
}
public static HomeFragment newInstance() {
HomeFragment fragment = new HomeFragment();
return fragment;
}
private class getAnnouncements extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void args) {
progressBar.setVisibility(View.GONE);
}
}
}
home_fragment.xml
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="24dp"
android:visibility="gone" />
解决方案
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
这篇关于在进度条的布局片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文