1. Are you ready for the Galaxy S20? Here is everything we know so far!

Using a DialogFragment

Discussion in 'Android Development' started by Fruity Faiz, Aug 8, 2017.

  1. Fruity Faiz

    Fruity Faiz Lurker
    Thread Starter

    Hey, I'm new to android dev and can't get my head around using a DialogFragment.

    So I've got this inside a Fragment class, but the application crashes when displaying the dialog, throwing an exception that a DialogFragment has to be static and public.

    Code (Java):
    1.  
    2. DialogFragment quizResults = new DialogFragment() {
    3.    @Override
    4.    public Dialog onCreateDialog(Bundle bundle) {
    5.        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    6.        builder.setMessage(getString(R.string.results,vtotalGuesses,
    7.                                  (1000 / (double) totalGuesses)));
    8.  
    9.        builder.setPositiveButton(R.string.reset_quiz, new DialogInterface.OnClickListener() {
    10.            public void onClick(DialogInterface dialog, int id) {
    11.                resetQuiz();
    12.             }
    13.         }
    14.         );   return builder.create();
    15. }
    16.  
    Anyways, I understand I'll have to make a sub-class of DialogFragment in another file, however I don't know how to have it call resetQuiz() and retrieve the value of totalGuesses.

    Thank you!
     


    sweetndreemy73 likes this.

    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    Please include the stack trace, showing the exception.
     
  3. Fruity Faiz

    Fruity Faiz Lurker
    Thread Starter

    Hey, I apologise for the late reply. Here is the stack trace I believe you're looking for. If I need to provide any other info or logs please let me know.

    Code (Text):
    1. 08-20 16:08:50.528 22992-22992/com.fruityfaiz.flagquiz W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    2. 08-20 16:08:50.749 22992-22999/com.fruityfaiz.flagquiz W/art: Not able to initialize class for SetValues: java.lang.Class<com.fruityfaiz.flagquiz.MainActivityFragment$2$1>
    3. 08-20 16:08:50.770 22992-22999/com.fruityfaiz.flagquiz I/art: Starting a blocking GC Instrumentation
    4. 08-20 16:08:50.804 22992-22999/com.fruityfaiz.flagquiz W/art: Not able to initialize class for SetValues: java.lang.Class<com.fruityfaiz.flagquiz.MainActivityFragment$2$1>
    5. 08-20 16:08:50.855 22992-22999/com.fruityfaiz.flagquiz W/art: Not able to initialize class for SetValues: java.lang.Class<com.fruityfaiz.flagquiz.MainActivityFragment$2$1>
    6. 08-20 16:08:50.861 22992-22992/com.fruityfaiz.flagquiz W/art: Verification of void com.fruityfaiz.flagquiz.MainActivityFragment$2.onClick(android.view.View) took 194.713ms
    7. 08-20 16:08:50.869 22992-22999/com.fruityfaiz.flagquiz W/art: Not able to initialize class for SetValues: java.lang.Class<com.fruityfaiz.flagquiz.MainActivityFragment$2$1>
    8. 08-20 16:08:51.025 22992-22997/com.fruityfaiz.flagquiz I/art: Do partial code cache collection, code=12KB, data=13KB
    9. 08-20 16:08:51.025 22992-22997/com.fruityfaiz.flagquiz I/art: After code cache collection, code=12KB, data=13KB
    10. 08-20 16:08:51.025 22992-22997/com.fruityfaiz.flagquiz I/art: Increasing code cache capacity to 128KB
    11. 08-20 16:08:51.027 22992-22997/com.fruityfaiz.flagquiz I/art: Compiler allocated 7MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
    12. 08-20 16:09:06.505 22992-22999/com.fruityfaiz.flagquiz I/art: Starting a blocking GC Instrumentation
    13. 08-20 16:09:11.219 22992-23689/com.fruityfaiz.flagquiz D/AppTracker: App Event: start
    14. 08-20 16:09:11.230 22992-23691/com.fruityfaiz.flagquiz D/AppTracker: App Event: stop
    15. 08-20 16:09:11.235 22992-23692/com.fruityfaiz.flagquiz D/AppTracker: App Event: start
    16. 08-20 16:09:11.370 22992-22992/com.fruityfaiz.flagquiz W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
    17. 08-20 16:09:44.747 22992-24193/com.fruityfaiz.flagquiz D/AppTracker: App Event: stop
    18. 08-20 16:10:32.083 22992-24910/com.fruityfaiz.flagquiz D/AppTracker: App Event: start
    19. 08-20 16:10:32.099 22992-22997/com.fruityfaiz.flagquiz I/art: Do partial code cache collection, code=61KB, data=53KB
    20. 08-20 16:10:32.099 22992-22997/com.fruityfaiz.flagquiz I/art: After code cache collection, code=61KB, data=53KB
    21. 08-20 16:10:32.099 22992-22997/com.fruityfaiz.flagquiz I/art: Increasing code cache capacity to 256KB
    22. 08-20 16:10:34.325 22992-22997/com.fruityfaiz.flagquiz I/art: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()
    23. 08-20 16:10:34.439 22992-22997/com.fruityfaiz.flagquiz I/art: Do full code cache collection, code=118KB, data=123KB
    24. 08-20 16:10:34.439 22992-22997/com.fruityfaiz.flagquiz I/art: Starting a blocking GC JitCodeCache
    25. 08-20 16:10:34.440 22992-22997/com.fruityfaiz.flagquiz I/art: After code cache collection, code=74KB, data=69KB
    26. 08-20 16:10:34.731 22992-22999/com.fruityfaiz.flagquiz W/art: Suspending all threads took: 8.698ms
    27. 08-20 16:10:43.039 22992-22997/com.fruityfaiz.flagquiz I/art: Do partial code cache collection, code=118KB, data=120KB
    28. 08-20 16:10:43.039 22992-22997/com.fruityfaiz.flagquiz I/art: After code cache collection, code=115KB, data=118KB
    29. 08-20 16:10:43.039 22992-22997/com.fruityfaiz.flagquiz I/art: Increasing code cache capacity to 512KB
    30. 08-20 16:11:14.908 22992-22992/com.fruityfaiz.flagquiz D/AndroidRuntime: Shutting down VM
    31. 08-20 16:11:14.931 22992-22992/com.fruityfaiz.flagquiz E/AndroidRuntime: FATAL EXCEPTION: main
    32.                                                                          Process: com.fruityfaiz.flagquiz, PID: 22992
    33.                                                                          java.lang.IllegalStateException: Fragment null must be a public static class to be  properly recreated from instance state.
    34.                                                                              at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:398)
    35.                                                                              at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:377)
    36.                                                                              at android.support.v4.app.DialogFragment.show(DialogFragment.java:142)
    37.                                                                              at com.fruityfaiz.flagquiz.MainActivityFragment$2.onClick(MainActivityFragment.java:274)
    38.                                                                              at android.view.View.performClick(View.java:5669)
    39.                                                                              at android.view.View$PerformClick.run(View.java:22546)
    40.                                                                              at android.os.Handler.handleCallback(Handler.java:751)
    41.                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
    42.                                                                              at android.os.Looper.loop(Looper.java:154)
    43.                                                                              at android.app.ActivityThread.main(ActivityThread.java:6334)
    44.                                                                              at java.lang.reflect.Method.invoke(Native Method)
    45.                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    46.                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    47. 08-20 16:11:14.932 22992-22992/com.fruityfaiz.flagquiz D/AppTracker: App Event: crash
    48. 08-20 16:11:14.967 22992-22997/com.fruityfaiz.flagquiz I/art: Do full code cache collection, code=250KB, data=217KB
    49. 08-20 16:11:14.967 22992-22997/com.fruityfaiz.flagquiz I/art: Starting a blocking GC JitCodeCache
    50. 08-20 16:11:14.968 22992-22997/com.fruityfaiz.flagquiz I/art: After code cache collection, code=234KB, data=185KB
    51. 08-20 16:11:14.977 22992-22992/com.fruityfaiz.flagquiz I/Process: Sending signal. PID: 22992 SIG: 9
     
  4. Fruity Faiz

    Fruity Faiz Lurker
    Thread Starter

    Hey, so I'm going to explain what I'm trying to do and maybe someone can help me do that which will avoid this crashing issue.

    So I have a fragment which has data(quiz). I'm trying to display a dialog to the user that shows the results and resets the quiz when they're done.
    How can I create a dialog and share data from the fragment class with it?

    Thank you!
     
    sweetndreemy73 likes this.
  5. Fruity Faiz

    Fruity Faiz Lurker
    Thread Starter

    Okay, so android does not allow fragments to be instantiated from an inner class. They need to be seen by android.

    Anyways this is what I did:
    ActivityFragment:
    Code (Java):
    1. QuizResultDialogFragment quizResults = new QuizResultDialogFragment();
    2.                     quizResults.setCancelable(false);
    3.                     quizResults.show(getFragmentManager(), "quiz results");
    DialogFragment:
    Code (Java):
    1. package com.fruityfaiz.flagquiz;
    2.  
    3. import android.app.AlertDialog;
    4. import android.app.Dialog;
    5. import android.app.Fragment;
    6. import android.content.DialogInterface;
    7. import android.os.Bundle;
    8. import android.support.v4.app.DialogFragment;
    9.  
    10. /**
    11. * Created by fruity on 27/08/17.
    12. */
    13.  
    14. public class QuizResultDialogFragment extends DialogFragment {
    15.     public Dialog onCreateDialog(Bundle bundle) {
    16.         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    17.  
    18.         final MainActivityFragment quizFragment = getQuizFragment();
    19.  
    20.         builder.setMessage(getString(R.string.results, quizFragment.totalGuesses, (1000 / (double) quizFragment.totalGuesses)));
    21.  
    22.         builder.setPositiveButton(R.string.reset_quiz,
    23.             new DialogInterface.OnClickListener() {
    24.                 public void onClick(DialogInterface dialog, int id) {
    25.                     quizFragment.resetQuiz();
    26.                 }
    27.              }
    28.         );
    29.  
    30.         return builder.create();
    31.     }
    32.  
    33.     private MainActivityFragment getQuizFragment() {
    34.         return (MainActivityFragment) getFragmentManager().findFragmentById(R.id.quizFragment);
    35.     }
    36. }
    37.  
     
Loading...

Share This Page

Loading...