Using Dialog in a Fragment

1

I implemented the DIalog method plus this is giving error and I am not able to solve, I would like a help follow 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 error 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
    
asked by anonymous 16.07.2014 / 20:11

2 answers

1

Many Android components and objects need a Context to work, meaning you need to tell what's going on for these components and newly created objects.

A tip is in the onAttach method of your Fragment , you reference an instance of your activity, like this:

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

Now, when you're going to set anything in your snippet that needs a Context , you simply (using your Dialog):

...
final Dialog dialog = new Dialog(mActivity);
...
    
11.09.2014 / 15:39
0

use only:

final Dialog dialog = new Dialog(getActivity());
    
12.08.2014 / 14:54