1

I am developing and app, where I use ProgressDialog to show loading screen when data is being received from the socket. Here is my code for the Fragment

public class MainFragment extends Fragment {
 public View onCreateView (LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        Button button = (Button) view.findViewById(R.id.button);

button.setOnLongClickListener(new View.OnLongClickListener() {
public void onClick(View view) {
// do some stuff
}
} );

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
progressDialog = ProgressDialog.show(getActivity(), "", "Loading..",true);
new Thread() {

                    public void run() {
                    try{
// establish socket connection and get data
                        }

                    } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                    }
                progressDialog.dismiss();
                    }
                }.start();

} );
return view
}
}

The Problem now is with respect to getActivity(). While executing the app once, the correct views are established but on the consecutive runs, the view is null, since the getActivity doesn't properly instantiate or is set.

I tried to save the activity with onAttach() and then pass this to ProgressDialog as shown below

public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = activity;
}

But still the view returned was null. Can anyone help me on how to to achieve this? Thanks!

user3747512
  • 203
  • 1
  • 6
  • 15

3 Answers3

1

You are trying to do some UI operations in a thread. That is prohibited, unless you set a Looper for a thread (which is not professional in my opinion).

Create ASyncTask where you can easily define your background process. I did a small example for you, please enjoy:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
            new SomethingInBackground().execute();
        } 
    });




private class SomethingInBackground extends AsyncTask<Void, Void, Boolean>
{
    private ProgressDialog pd;


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

        pd = ProgressDialog.show(YourActivity.this, "", "Loading..",true);
        // it's your UI thread, you can use your activity things here
    }


    @Override
    protected Boolean doInBackground(Void... params) {

        // background process, no UI operations allowed

        // establish socket connection and get data

        // return true if success, false if error (or use some strings instead of boolean)

        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);

        // your UI thread, draw on screen

        pd.dismiss();

        if (result)
            Log.i("tag", "success!");
        else
            Log.e("tag", "error");
    }
}
krzynek91
  • 140
  • 10
  • I did instantiate and try with the Async task too with my socket running in background. Since my button have 2 options, normal click and longclick there is something that is messing up with the activity. They aren't picked up currectly when I put YourActivity.this or anything with respect to context or getActivity. When the app runs for the first time it works fine, but consequent views are not inflated. – user3747512 Jul 21 '14 at 10:03
  • Please check out [this](http://stackoverflow.com/questions/11631408/android-fragment-getactivity-sometime-returns-null). – krzynek91 Jul 21 '14 at 10:07
0

Try to pass the context(or activity) when initializing this MainFragment:

private YourAvtivity context;

public MainFragment(YourAvtivity c){
    this.context = c;
}

......

progressDialog = ProgressDialog.show(context, "", "Loading..",true);
betteroutthanin
  • 7,148
  • 8
  • 29
  • 48
0
  1. first im not show your dialod declare like private ProgressDialog progressDialog;
  2. use this syntax

    progressDialog = ProgressDialog.show(getActivity(),"title","massage", true);
    progressDialog.show();
    
  3. you dont need pass Context in onAttach() meyhod. getActivity do the Job

itzhar
  • 12,743
  • 6
  • 56
  • 63