Kenny Ong
Kenny Ong

Reputation: 401

android: how to show ProgressDialogFragment in MainActivity

I got some problem on code below, when i build the app it show error at the MainActivity side. Below is the code: ProgressDialogFragment.java

public class ProgressDialogFragment extends DialogFragment
{

    public static ProgressDialogFragment newInstance() {
        ProgressDialogFragment frag = new ProgressDialogFragment ();
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setCancelable(false);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        ProgressDialog dialog = new ProgressDialog(getActivity(), getTheme());
        dialog.setTitle(getString(R.string.updatename_loading_title));
        dialog.setMessage(getString(R.string.updatename_loading_message));
        dialog.setIndeterminate(true);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        return dialog;
    }

}

MainActivity.java

public void doPositiveClick(DialogFragment dialog) {
                Dialog dialogView = dialog.getDialog();
                EditText editName = (EditText) dialogView.findViewById(R.id.editName);
                String updateName = editName.getText().toString().trim();
                    login.setName(updateName);
                    new HttpAsyncTask().execute(uNameUrl);
                    dialogView.dismiss();
            }

        private class HttpAsyncTask extends AsyncTask<String, Void, String> {

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                   progressDialog.show(MainActivity.getFragmentManager(),"loading");
                }

                @Override
                protected String doInBackground(String... urls) {

                    // Do Action Here
                }
    }

I want to show loading after submit, but got error at line below:

progressDialog.show(MainActivity.getFragmentManager(),"loading"); 

Error Message below show after i build the app.

Error:(216, 45) error: non-static method getFragmentManager() cannot be referenced from a static context

How can i fix this? Thanks

@Edit I fixed the first error, i just forgot to add this on ProgressDialogFragment.java

@Override
    public void show(FragmentManager manager, String tag) {
        if (manager.findFragmentByTag(tag) == null) {
            super.show(manager, tag);
        }
    }

for prevent duplicate dialog when rotate the mobile. Now it got second error

Error:(40, 5) error: method does not override or implement a method from a supertype
Error:(43, 18) error: no suitable method found for show(android.app.FragmentManager,String)
        method DialogFragment.show(android.support.v4.app.FragmentManager,String) is not applicable
        (argument mismatch; android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager)
        method DialogFragment.show(FragmentTransaction,String) is not applicable
        (argument mismatch; android.app.FragmentManager cannot be converted to FragmentTransaction)

Upvotes: 1

Views: 1510

Answers (2)

Kenny Ong
Kenny Ong

Reputation: 401

I fixed the problem: ProgressDialogFragment.java

public class ProgressDialogFragment extends DialogFragment
{

    public static ProgressDialogFragment newInstance() {
        ProgressDialogFragment frag = new ProgressDialogFragment ();
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setCancelable(false);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        ProgressDialog dialog = new ProgressDialog(getActivity(), getTheme());
        dialog.setTitle(getString(R.string.updatename_loading_title));
        dialog.setMessage(getString(R.string.updatename_loading_message));
        dialog.setIndeterminate(true);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        return dialog;
    }

}

MainActivity.java

private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        ProgressDialogFragment progressDialog = new ProgressDialogFragment();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.show(getFragmentManager(), "loading");
        }

        @Override
        protected String doInBackground(String... urls) {

            return UpdateData(MainActivity.this, urls[0], login);
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            progressDialog.dismiss();
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
            finish();
              Log.d("posted", result);
            Toast.makeText(getBaseContext(), "Display Name Successful Changed!", Toast.LENGTH_LONG).show();
        }
    }

First Question about

Error:(216, 45) error: non-static method getFragmentManager() cannot be referenced from a static context

I fixed by @Gennadii Saprykin and handrenliang with below code:

progressDialog.show(MainActivity.getFragmentManager(),"loading"); 

to

progressDialog.show(MainActivity.this.getFragmentManager(),"loading"); 

The second error :

Error:(216, 27) error: no suitable method found for show(android.app.FragmentManager,String) method DialogFragment.show(android.support.v4.app.FragmentManager,String) is not applicable (argument mismatch; android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager) method DialogFragment.show(FragmentTransaction,String) is not applicable (argument mismatch; android.app.FragmentManager cannot be converted to FragmentTransaction)

is fixed by @handrenliang with below: used android-v4 DialogFragment, but your activity is not v4, you can import non v4 DialogFragment in your ProgressDialogFragment.

import android.support.v4.app.DialogFragment;

to

import android.app.DialogFragment;

and last error that crash, i just move the dialog declaration from MainActivity Class to inside HttpAsyncTask Class.

ProgressDialogFragment progressDialog = new ProgressDialogFragment();

Thanks @Gennadii Saprykin and @handrenliang For the help.

Upvotes: 0

handrenliang
handrenliang

Reputation: 1047

You are call outer class method in inner class, you should change MainActivity.getFragmentManager() to MainActivity.this.getFragmentManager()

Upvotes: 1

Related Questions