简体   繁体   中英

java.lang.IllegalStateException: Fragment ProgressDialog{x) not associated with a fragment manager

I migrate the android source from API8 to API32. Becase the progressdialog class was deprecated in API26, so i want custom it. I search and founded it as follow url: https://qiita.com/Uchikoba/items/478d604f417465700ba1

i copy it and create ProgressDialog class in my app. i show and dismiss it in the same class, no have any problem.

public class BaseActivity extends AppCompatActivity   {
protected ProgressDialog mProgressDialog = null;

  protected void callServerApi(
        ApiHttpClient client,
        String url, RequestParams params, int timeoutsec, boolean isAuth,
        AsyncHttpResponseHandler handler, String dmsg) {

    try {
         mProgressDialog = ProgressDialog.newInstance(msg);
        mProgressDialog.setCancelable(true);
        mProgressDialog.show(getSupportFragmentManager(), "tag");
        dosomething();
    }......

   private void dosomething() {
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... aVoid) {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (mProgressDialog == null) return;
             mProgressDialog.dismiss();
             mProgressDialog = null;
        }
    }.execute();
}

}

but i want use ProgressDialog as under consider.

  1. show the progressdialog
  2. communicate to server
  3. get the response data and close progressdialog

AccountActivity

public class AccountActivity extends BaseActivity {
 ApiHttpClient mApiClient = new ApiHttpClient(ApiCookieStore.getCookieStore());


public void callApiAuthentication(String uid, String pwd, String domain) {

    RequestParams params = mApiClient.makeBaseRequestParams("account");
    params.add("cj_uid", uid);
    params.add("cj_pwd", pwd);
    params.add("cj_domain", domain);
    AsyncHttpResponseHandler handler = new AuthResponseHandler(this, uid, pwd, domain);

    callServerApi(
            mApiClient, "http://www.sample.com/logon.do", params,
            1000, false,handler, "Now authenticating...");

}
    public void saveInputText(String id, String pass, String domain)
    throws SmartAppCipherException {

    SmartAppPreference pref = new SmartAppPreference(this);
    
    pref.setAccount(id, pass, domain);
}

}

BaseActivity

public class BaseActivity extends AppCompatActivity   {
protected ProgressDialog mProgressDialog = null;


protected void callServerApi(
        ApiHttpClient client,
        String url, RequestParams params, int timeoutsec, boolean isAuth,
        AsyncHttpResponseHandler handler, String dmsg) {

    try {
        mProgressDialog = ProgressDialog.newInstance(msg);
        mProgressDialog.setCancelable(true);
        mProgressDialog.show(getSupportFragmentManager(), "tag");

        client.post(url, params, handler, timeoutsec * 1000);
    }
    catch(Exception  e) {
     ...
    }
    
}

public void dismissProgress() {     
    try {
        if (mProgressDialog == null) return;
        mProgressDialog.dismiss();
        mProgressDialog = null;

        }
    catch(IllegalArgumentException e) {
          ...
    }
}

AuthResponseHandler.java:

public class AuthResponseHandler extends AsyncHttpResponseHandler {

private AccountActivity mParentActivity = null;
private String mInputID;
private String mInputPassword;
private String mInputDomain;

protected AuthResponseHandler() {}
public AuthResponseHandler(AccountActivity parent, String id, String pwd, String domain) {
    mParentActivity = parent;
    mInputID = id;
    mInputPassword = pwd;
    mInputDomain = domain;
}

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

    try {
        String responseBodyString = new String(responseBody);
        JsonReader jsr = new JsonReader(new StringReader(responseBodyString));
        Gson gson = new Gson();
        AuthResponse response = gson.fromJson(jsr, AuthResponse.class);
        Logger.d("responseBodyString", responseBodyString);
        if (response.getStatus() != 1) {

            disptachApiError(response.getApiError());
            return;
        }
        mParentActivity.saveInputText(mInputID, mInputPassword, mInputDomain);
        mParentActivity.finish();
    }
    catch(Exception e) {
        Logger.d("AuthResponseHandler.onReceive", "failed", e);
    }
}

    @Override
public void onFinish() {
    
    mParentActivity.dismissProgress();
}

}

build.gradle

dependencies {
implementation "androidx.constraintlayout:constraintlayout:2.1.3"
implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'androidx.work:work-runtime:2.7.1'
implementation 'androidx.fragment:fragment:1.4.1'
implementation 'androidx.core:core:1.7.0'
implementation 'com.google.code.gson:gson:2.9.0'
implementation "androidx.activity:activity:1.4.0"
implementation files('libs/android-viewbadger.jar')

i run this source,but when call mParentActivity.dismissProgress() in AuthResponseHandler.java. the java.lang.IllegalStateException has been occurred

java.lang.RuntimeException: java.lang.IllegalStateException: Fragment ProgressDialog{3a61bf9} (133e25a6-7db0-4a8f-8978-48deefd6308a) not associated with a fragment manager.
    at com.loopj.android.http.AsyncHttpResponseHandler.onUserException(AsyncHttpResponseHandler.java:304)
    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:395)
    at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:510)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.IllegalStateException: Fragment ProgressDialog{3a61bf9} (133e25a6-7db0-4a8f-8978-48deefd6308a) not associated with a fragment manager.
    at androidx.fragment.app.Fragment.getParentFragmentManager(Fragment.java:1059)
    at androidx.fragment.app.DialogFragment.dismissInternal(DialogFragment.java:352)
    at androidx.fragment.app.DialogFragment.dismiss(DialogFragment.java:309)
    at com.fujitsu.campus.smartapp.base.common.app.BaseActivity.dismissProgress(BaseActivity.java:173)
    at com.fujitsu.campus.smartapp.base.config.account.AuthResponseHandler.onFinish(AuthResponseHandler.java:91)
    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:368)

who can help me?

the problem has been resolved.

only use DialogFragment.show(fm,tag) replace ProgressDialog.show(fm,tag).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM