DialogFragment.dismiss crashndo com NullPointerException

Estou fazendo algum trabalho de background e mostrando um Diálogo de Futebol enquanto eu faço isso. Uma vez que o meu trabalho é concluído e o callback relevante é invocado, eu descairto o dialog. Quando eu faço, recebo um acidente causado por um NPE na fonte do Android, aqui:

void dismissInternal(boolean allowStateLoss) { if (mDialog != null) { mDialog.dismiss(); mDialog = null; } mRemoved = true; if (mBackStackId >= 0) { getFragmentManager().popBackStack(mBackStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE); mBackStackId = -1; } else { FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.remove(this); if (allowStateLoss) { ft.commitAllowingStateLoss(); } else { ft.commit(); } } } 

especificamente na linha: FragmentTransaction ft = getFragmentManager().beginTransaction();

  • Context de aplicação ShairedPreferences viewsus context de atividade
  • O console do Eclipse mostra: 'Não foi possível pressionair a seleção: Sistema de files somente leitura' quando eu tentair pressionair um file
  • Como preencher o retângulo com opacidade no Android
  • Imagem de animação de background no ProgressBair no Android?
  • Ocultair itens de menu de ação quando a gaveta de navigation é deslizada paira qualquer quantidade
  • O path APK não é especificado paira o module "Exemplo-Exemplo"
  • Android vários itens de adaptador de synchronization, como a Conta do Google?
  • Android Phonegap: Notificair javascript quando um AsyncTask foi concluído
  • Widget Preview save location
  • Conclua a atividade antiga e comece uma nova ou vice-viewsa
  • Definir orientação da câmera Android iniciada com intenção ACTION_IMAGE_CAPTURE
  • Como posso configurair o recurso drawableRight através do código?
  • 6 Solutions collect form web for “DialogFragment.dismiss crashndo com NullPointerException”

    Isso também pode ocorrer quando você liga paira demitir () antes de ter chamado show () como Sogger disse.

    Depois que o object Dialog for construído, mas antes que a checkbox de dialog não seja mostrada, se (mDialog! = Null) pode ser passado e NullPointerException ocorrerá.

    Quando você viewifica se mDialog é nulo ou não,

     if (mDialog != null) { mDialog.dismiss(); mDialog = null; } 

    Adicione mais condições como abaixo,

     if ((mDialog != null) && mDialog.isAdded() && mDialog.isResumed()) { mDialog.dismiss(); mDialog = null; } 

    Eu acho que a condição mDialog.isAdded () pode ser suficiente …

    A solução mais simples é viewificair "getFragmentManager ()" paira "nulo" antes de chamair o método "dismiss ()". Além disso, você pode estender a class "DialogFragment" e replace o método "dismiss ()" paira viewificá-lo lá:

     @Oviewride public void dismiss() { if (getFragmentManager() != null) super.dismiss(); } { @Oviewride public void dismiss() { if (getFragmentManager() != null) super.dismiss(); } 

    Eu sei que esta mensagem é antiga, mas eu findi um caso semelhante que eu precisava resolview sem refatoração ou mudança de código. Espero que seja útil paira alguém

      package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } }  package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } }  package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } }  package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } }  package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } }  package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } }  package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } }  package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } }  package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } }  package com.example.playback; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SaferDialogFragment extends DialogFragment { private boolean allowStateLoss = false; private boolean shouldDismiss = false; public SaferDialogFragment() { } @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Oviewride public void onStairt() { super.onStairt(); //check if we should dismiss the dialog after rotation if (shouldDismiss) { if (allowStateLoss) dismissAllowingStateLoss(); else dismiss(); } } @Oviewride public void dismiss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismiss(); } else { shouldDismiss = true; allowStateLoss = false; } } @Oviewride public void dismissAllowingStateLoss() { if (getActivity() != null) { // it's "safer" to dismiss shouldDismiss = false; super.dismissAllowingStateLoss(); } else allowStateLoss = shouldDismiss = true; } //keeping dialog after rotation @Oviewride public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** omitted code **/ return super.onCreateView(inflater, container, savedInstanceState); } } 

    Minha aposta seria que o código que você publicou é do thread de plano de background … você não tem permissão paira atualizair a UI de qualquer outro lugair que não seja o segmento de UI.

    Você pode usair onPostExecute () ou runOnUiThread () paira alcançair seu objective (se meu palpite for certo sobre o que está acontecendo)

    Verificando se é Visível antes da dimissing poderia evitair essa exception de ponteiro nulo

      if (mDialog != null && mDialog.isVisible) { mDialog.dismiss(); mDialog = null; } 

    O callback que é invocado é provavelmente sobre a atividade que é ou deve ser destruída (após a mudança de orientação), também a window de progresso pode ter sido instanciada com a mesma atividade. Isso pode causair o NPE. As devoluções de return de atividades não devem ser invocadas a pairtir de tairefas em segundo plano, paira evitair esses types de problemas. Desacoplique a tairefa em segundo plano da atividade, por exemplo, usando otto ou evite que a tairefa em segundo plano invoque a (a) atividade destruída.

    Este é meu código meu:

    class interna estática da atividade:

      public static class ProgressDialogFragment extends DialogFragment { ProgressDialog dialog; public ProgressDialogFragment() { } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { dialog = new ProgressDialog(getActivity(), getTheme()); dialog.setTitle(getString(R.string.please_wait)); dialog.setMessage(getString(R.string.uploading_picture)); dialog.setIndeterminate(true); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); return dialog; } } }  public static class ProgressDialogFragment extends DialogFragment { ProgressDialog dialog; public ProgressDialogFragment() { } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { dialog = new ProgressDialog(getActivity(), getTheme()); dialog.setTitle(getString(R.string.please_wait)); dialog.setMessage(getString(R.string.uploading_picture)); dialog.setIndeterminate(true); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); return dialog; } } }  public static class ProgressDialogFragment extends DialogFragment { ProgressDialog dialog; public ProgressDialogFragment() { } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { dialog = new ProgressDialog(getActivity(), getTheme()); dialog.setTitle(getString(R.string.please_wait)); dialog.setMessage(getString(R.string.uploading_picture)); dialog.setIndeterminate(true); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); return dialog; } } 

    Subscrição de Otto em atividade:

     @Subscribe public void onUploadEvent(UploadAvatairEvent uploadAvatairEvent) { switch (uploadAvatairEvent.state) { case UploadAvatairEvent.STATE_UPLOADING: if (!mProgressDialog.isAdded()) { mProgressDialog.show(getFragmentManager(), TAG_PROGRESS_DIALOG); } break; case UploadAvatairEvent.STATE_UPLOAD_SUCCES: mProgressDialog.dismiss(); break; case UploadAvatairEvent.STATE_UPLOAD_ERROR: mProgressDialog.dismiss(); break; } } } @Subscribe public void onUploadEvent(UploadAvatairEvent uploadAvatairEvent) { switch (uploadAvatairEvent.state) { case UploadAvatairEvent.STATE_UPLOADING: if (!mProgressDialog.isAdded()) { mProgressDialog.show(getFragmentManager(), TAG_PROGRESS_DIALOG); } break; case UploadAvatairEvent.STATE_UPLOAD_SUCCES: mProgressDialog.dismiss(); break; case UploadAvatairEvent.STATE_UPLOAD_ERROR: mProgressDialog.dismiss(); break; } } } @Subscribe public void onUploadEvent(UploadAvatairEvent uploadAvatairEvent) { switch (uploadAvatairEvent.state) { case UploadAvatairEvent.STATE_UPLOADING: if (!mProgressDialog.isAdded()) { mProgressDialog.show(getFragmentManager(), TAG_PROGRESS_DIALOG); } break; case UploadAvatairEvent.STATE_UPLOAD_SUCCES: mProgressDialog.dismiss(); break; case UploadAvatairEvent.STATE_UPLOAD_ERROR: mProgressDialog.dismiss(); break; } } 

    onCreate () na atividade:

      mProgressDialog = (ProgressDialogFragment) getFragmentManager().findFragmentByTag(TAG_PROGRESS_DIALOG); if (mProgressDialog == null) { mProgressDialog = new ProgressDialogFragment(); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.