-1
case R.id.button_next_question:
         if ((this.question_number > -1 + this.total_question) || (this.question_number == -1 + this.total_question))
            {
            new AlertDialog.Builder(getActivity().getApplicationContext()).setTitle("Thank you").show();

}

I Want to show dialog when question list is finish in project but whenever following if executed then i got the following error

07-08 06:15:53.775: E/AndroidRuntime(1327): FATAL EXCEPTION: main
07-08 06:15:53.775: E/AndroidRuntime(1327): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.app.Dialog.show(Dialog.java:281)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at info.androidhive.slidingmenu.CommunityFragment.onClick(CommunityFragment.java:120)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.view.View.performClick(View.java:4204)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.view.View$PerformClick.run(View.java:17355)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.os.Handler.handleCallback(Handler.java:725)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.os.Looper.loop(Looper.java:137)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at java.lang.reflect.Method.invoke(Method.java:511)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-08 06:15:53.775: E/AndroidRuntime(1327):     at dalvik.system.NativeStart.main(Native Method)
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
Nawaf
  • 424
  • 5
  • 17
  • 3
    Try to use getActivity() insted of getActivity().getApplicationContext(). Read here why http://stackoverflow.com/a/5466600/2893307 – nnesterov Jul 08 '14 at 06:32
  • i got it and it work perfect now i want to inflate xml layout in that dialog box case R.id.button_next_question: if ((this.question_number > -1 + this.total_question) || (this.question_number == -1 + this.total_question)) { LayoutInflater li = null; View v1 =li.inflate(R.layout.dialog_layout, null); new AlertDialog.Builder(getActivity()).setTitle("Thank you") .setView(v1) .show(); } getting following error 07-08 06:42:54.316: E/AndroidRuntime(1836): FATAL EXCEPTION: main 07-08 06:42:54.316: E/AndroidRuntime(1836): java.lang.NullPointerException – Nawaf Jul 08 '14 at 06:44
  • please, add this code and logcat output to your question. Difficult to read large chunks of code in comments – nnesterov Jul 08 '14 at 07:01

1 Answers1

1
Thank for your reply

finally i  got the solution & its working


if ((this.question_number > -1 + this.total_question) || (this.question_number == -1 + this.total_question))
            {
             LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService
                      (Context.LAYOUT_INFLATER_SERVICE);
             View v1 =inflater.inflate(R.layout.dialog_layout, null);
            new AlertDialog.Builder(getActivity()).setTitle("Thank you")
            .setView(v1)        
            .show();
}
Nawaf
  • 424
  • 5
  • 17