ProgressBar不会在代码中的任何地方加载

时间:2019-06-30 14:30:34

标签: android android-viewpager android-progressbar

所以我整天都尝试了这个,但没有成功。

我用ViewPager TabLayout连接了两个片段,

,并且我尝试将ProgressBar放在每个片段上,它不会显示。 我还尝试将其放在MainActivity上,该MainActivity初始化两个片段的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);
    }
}

另一个片段

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

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);


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

    thread.run();

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


    return view;
}

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


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

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


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

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

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

    @Override
    protected void onPostExecute(Void aVoid) {
        recyclerView.setVisibility(View.VISIBLE);

    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;


public MainActivity() {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);

    // placing toolbar in place of action bar
    setSupportActionBar(toolbar);



    /*Membuat tabLayout*/
    tabLayout = findViewById(R.id.tablayout);
    viewPager = findViewById(R.id.viewpager);

    adapter = new ViewPagerAdapter(getSupportFragmentManager());


    //adding fragment
    adapter.AddFragment(new TvShow(), "Tv");
    adapter.AddFragment(new FilmShow(), "Film");

    // Adapter Setrup
    viewPager.setAdapter(adapter);

    tabLayout.setupWithViewPager(viewPager);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_change_settings) {
        Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

}

编辑:

修改了另一个片段,以在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>

编辑II:

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
    }

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

1 个答案:

答案 0 :(得分:1)

您尝试过这样吗

 @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);

            }
        });

此代码不需要Asyntask,我只是将其隐藏并显示