ProgressBar in Fragment Layout

ε祈祈猫儿з 提交于 2019-12-13 04:44:28

问题


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

回答1:


progressBar = (ProgressBar) view.findViewById(R.id.progressBar);


来源:https://stackoverflow.com/questions/27720958/progressbar-in-fragment-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!