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