Android ProgressBar赢得';不要在代码中的任何地方加载

Android ProgressBar赢得';不要在代码中的任何地方加载,android,android-viewpager,android-progressbar,Android,Android Viewpager,Android Progressbar,所以我试了一整天都没有成功 我连接了两个片段,带有ViewPager表格布局 我试着在每个片段上添加ProgressBar,但它不会出现。 我还试着把它放在启动两个片段的ViewPager的主活动上,它也不会显示 我尝试了一个新项目,它显示了。我做错了什么 我已经在代码中删除了ProgressBar,因为它不起作用。如果你能亲眼看到,这是代码 第一个片段 public class FilmShow extends Fragment { private static RecyclerVie

所以我试了一整天都没有成功

我连接了两个片段,带有ViewPager表格布局

我试着在每个片段上添加ProgressBar,但它不会出现。 我还试着把它放在启动两个片段的ViewPager的主活动上,它也不会显示

我尝试了一个新项目,它显示了。我做错了什么

我已经在代码中删除了ProgressBar,因为它不起作用。如果你能亲眼看到,这是代码

第一个片段

public class FilmShow extends Fragment {
    private static RecyclerView recyclerView;
    private static final String API_KEY = "2e08750083b7e21e96e915011d3f8e2d";
    private static final String TAG = FilmShow.class.getSimpleName();

public FilmShow() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getResponse getResponse = new getResponse();
    getResponse.execute();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_film_show, container, false);

    recyclerView = view.findViewById(R.id.recycler_tv);

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(layoutManager);

    recyclerView.setVisibility(View.GONE);



    return view;
}

private class getResponse extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {
        ApiInterface apiInterface = ApiClient.getList().create(ApiInterface.class);

        Call<Response> responseCall = apiInterface.getFilmList(API_KEY);
        responseCall.enqueue(new Callback<Response>() {
            @Override
            public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                List<ResultsItem> resultsItemList = response.body().getResults();
                recyclerView.setAdapter(new FilmShowRecyclerAdapter(resultsItemList, getContext()));
            }

            @Override
            public void onFailure(Call<Response> call, Throwable t) {
                Log.d(TAG, t.toString());
            }
        });
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}
}

编辑:

修改了另一个片段以在onCreateView上显示ProgressBar,它可以工作,但不会停止加载

...............

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_tv_show, container, false);

    recyclerView = view.findViewById(R.id.recycler_tv);
    progressBar = view.findViewById(R.id.progress_bar);

    ...............
XML文件

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:padding="16dp"
    >



        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="@layout/item_tv"
            android:visibility="gone" />


        <Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ProgressBar
            android:id="@+id/progress_bar"
            style="?android:attr/progressBarStyle"
            android:textAlignment="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />


</RelativeLayout>

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.view android.widget.ProgressBar.findViewById(int)”

您尝试过这样做吗

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_tv_show, container, false);

    recyclerView = view.findViewById(R.id.recycler_tv);
    progressBar = view.findViewById(R.id.progress_bar);

// progress bar Shows first
progressBar.setVisibility(VISIBLE);

ApiInterface apiInterface = ApiClient.getList().create(ApiInterface.class);

        Call<Response> responseCall = apiInterface.getFilmList(API_KEY);
        responseCall.enqueue(new Callback<Response>() {
            @Override
            public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                List<ResultsItem> resultsItemList = response.body().getResults();
                recyclerView.setAdapter(new FilmShowRecyclerAdapter(resultsItemList, getContext()));
progressBar.setVisibility(GONE);

            }

            @Override
            public void onFailure(Call<Response> call, Throwable t) {
                Log.d(TAG, t.toString());
        progressBar.setVisibility(GONE);

            }
        });
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
最终视图=充气机。充气(R.layout.fragment\u tv\u show,容器,假);
recyclerView=view.findViewById(R.id.recycler\u tv);
progressBar=view.findviewbyd(R.id.progress\u-bar);
//首先显示进度条
progressBar.setVisibility(可见);
ApiInterface ApiInterface=ApiClient.getList().create(ApiInterface.class);
调用responseCall=apinterface.getFilmList(API_键);
responseCall.enqueue(新的回调(){
@凌驾
公共void onResponse(呼叫,改装2.响应){
List resultsItemList=response.body().getResults();
setAdapter(新的FilmShowRecyclerAdapter(resultsItemList,getContext());
progressBar.setVisibility(已消失);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(TAG,t.toString());
progressBar.setVisibility(已消失);
}
});

此代码不需要Asyntask,我只是让它隐藏和显示

您需要显示如何实现和显示/隐藏progressbar,否则很难从上面了解问题发生的位置。@Azhar92内部实现onProgress和onPostExecute不起作用我不明白为什么在Async内部使用改型任务库。两者具有相似的特性function@NandaZ完成assignmentjava.lang.NullPointerException需要它:尝试调用虚拟方法'void android.widget.ProgressBar.setVisibility(int)'在空对象引用上,您必须先在xml中设置Progressbar小部件,然后在fraagmentprivate静态Progressbar Progressbar中实例化它;@Override public void onCreate(@Nullable Bundle savedInstanceState){super.onCreate(savedInstanceState);Progressbar.findViewById(R.id.progress\u bar);progressBar.setVisibility(可见);我编辑了我的答案,我在
onCreateView
方法中更改了执行。如果您在后台执行Asyntask,而您的状态无法访问主线程。Main thead只允许在执行后访问,但在您的情况下,这不合适。Asyntask是执行HttpRest的经典示例,如果您使用改型,您可以执行Asyncta它们在功能上是相同的
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:padding="16dp"
    >



        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="@layout/item_tv"
            android:visibility="gone" />


        <Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ProgressBar
            android:id="@+id/progress_bar"
            style="?android:attr/progressBarStyle"
            android:textAlignment="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />


</RelativeLayout>
public class TvShow extends Fragment {
    private static RecyclerView recyclerView;
    private static final String API_KEY = "2e08750083b7e21e96e915011d3f8e2d";
    private static final String TAG = TvShow.class.getSimpleName();
    private static ProgressBar progressBar;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        progressBar.findViewById(R.id.progress_bar);

        progressBar.setVisibility(VISIBLE);


        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                CallInternet callInternet = new CallInternet();
                callInternet.execute();
            }
        });

        thread.run();
    }

    public TvShow() {
        // Required empty public constructor
    }
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_tv_show, container, false);

    recyclerView = view.findViewById(R.id.recycler_tv);
    progressBar = view.findViewById(R.id.progress_bar);

// progress bar Shows first
progressBar.setVisibility(VISIBLE);

ApiInterface apiInterface = ApiClient.getList().create(ApiInterface.class);

        Call<Response> responseCall = apiInterface.getFilmList(API_KEY);
        responseCall.enqueue(new Callback<Response>() {
            @Override
            public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
                List<ResultsItem> resultsItemList = response.body().getResults();
                recyclerView.setAdapter(new FilmShowRecyclerAdapter(resultsItemList, getContext()));
progressBar.setVisibility(GONE);

            }

            @Override
            public void onFailure(Call<Response> call, Throwable t) {
                Log.d(TAG, t.toString());
        progressBar.setVisibility(GONE);

            }
        });