我无法访问我在片段的Asynctask片段中onCreateView片段中创建的进度条。它由错误引起的是onPreExecute中的进度条给出了NullPointerException。 我在调用asynctask之前创建了progressBar。那可能是什么问题?
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" />
答案 0 :(得分:2)
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);