Using Dialog in a Fragment

Asked

Viewed 208 times

1

I implemented the Dialog method but is giving error and I am not able to solve, I would like a help follows my class

public class Iniciar_Host extends Fragment implements AdapterView.OnItemClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_iniciar__host, container, false);        
    list = (ListView)view.findViewById(R.id.lista_Inicial);
    Host = new String[] { "Host1 ", "Host2", "Host3"};
    Descricao = new String[] { "Estado do Host", "Estado do Host", "Host" };
    Status = new String[]{"DOWN","UNREACHABLE","DOWN"};
    adapter = new ListViewAdpterInicial(getActivity().getBaseContext(),Host,Descricao,Status,i);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);
    return view;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(getActivity().getBaseContext(), inalcancaveis_tela.class);
    startActivity(intent);
}

private void Dialago_Notificacao_ACK(){
    final Dialog dialog = new Dialog(getActivity().getBaseContext());

    dialog.setContentView(R.layout.ack_notificacao);

    final Button btcancelar = (Button)dialog.findViewById(R.id.bt_confirma_ack);

    btcancelar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
}

}

My mistake in Logcat

07-16 14:08:00.443    1803-1803/local.viewer.nocview.spac.com.br.nocview D/AndroidRuntime﹕ Shutting down VM
07-16 14:08:00.443    1803-1803/local.viewer.nocview.spac.com.br.nocview W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3a84ba8)
07-16 14:08:00.483    1803-1803/local.viewer.nocview.spac.com.br.nocview E/AndroidRuntime﹕ FATAL EXCEPTION: main
 Process: local.viewer.nocview.spac.com.br.nocview, PID: 1803
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.app.Dialog.show(Dialog.java:286)
        at local.viewer.nocview.spac.com.br.nocview.Iniciar_Host.Dialago_Notificacao_ACK(Iniciar_Host.java:90)
        at local.viewer.nocview.spac.com.br.nocview.Iniciar_Host.onItemLongClick(Iniciar_Host.java:65)
        at android.widget.AbsListView.performLongPress(AbsListView.java:2972)
        at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2921)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
07-16 14:08:03.023    1803-1803/local.viewer.nocview.spac.com.br.nocview I/Process﹕ Sending signal. PID: 1803 SIG: 9
07-16 14:08:06.483    1829-1829/local.viewer.nocview.spac.com.br.nocview D/dalvikvm﹕ GC_FOR_ALLOC freed 109K, 6% free 3241K/3416K, paused 134ms, total 138ms
  • 1

    Why don’t you just use the Activity as Context dialog instead of the getBaseContext? If you’re worried about Memory Leak use getApplicationContext, although problems may arise with the theme.

  • Place the Dialog call.

2 answers

1

Many Android components and objects need a Context to work, that is, you need to tell these newly created components and objects what is happening.

A hint is in the method onAttach of your Fragment, you refer to an instance of your Activity, so:

SuaActivity mActivity;
@Override
public void onAttach(Activity activity) {  
    super.onAttach(activity);
    mActivity = (SuaActivity) getActivity();
}

Now, when are you going to set anything on your fragment that needs a Context, you simply (using your Dialog):

...
final Dialog dialog = new Dialog(mActivity);
...

0

use only:

final Dialog dialog = new Dialog(getActivity());
  • 2

    Hi, welcome to [en.so]. It’s nice to explain because your code solves the problem. Check out the guide [Answer].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.