Android snippets - adding a progress bar

I'm new here. Let me know if I ask the question in the wrong way or if I need to clarify this.

I am creating an Android application and I use fragments in my code (for example, 4 tabs that can be shifted). I am trying to add a panel to show the percentage of how much the user has used. I thought a progress bar would be the best option, but I can't get it to work.

Here is my code for the fragment I want to add a panel to;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class UserFragment extends Fragment {

    private ProgressDialog progress;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_calls, container, false);

    progress = new ProgressDialog(this);
    return rootView;
}

public void open(View view){
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progress.setIndeterminate(true);
      progress.show();

       final int totalProgressTime = 100;

       final Thread t = new Thread(){

       @Override
       public void run(){

          int jumpTime = 0;
          while(jumpTime < totalProgressTime){
             try {
                sleep(200);
                jumpTime += 5;
                progress.setProgress(jumpTime);
             } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
             }

          }

       }
       };
       t.start();
}

} , , , " - undefined". , - , , . - , .

+4
3
0

, getActivity(), , . Activity, , Activity .

0

:

ProgressDialog progress = new ProgressDialog(getContext());

ProgressDialog progress = new ProgressDialog(getActivity());

onAttach Fragment :

private Context context;
@Override
public void onAttach(Context context) {
   super.onAttach(context);
   this.context = context; //when fragment is created, context will be initialised for use.
}

onCreateView :

ProgressDialog progress = new ProgressDialog(context);

, , Activity, . getContext() getActivity() NullPointerException.

0

Source: https://habr.com/ru/post/1523420/


All Articles