App falls down with API 27 but wolks correctly with API 21












0















I`m trying to set title text size for DialogFragment

I coded for my old test device (Android 5.0 API 21) and all was ok

But app falls after I began to use new test device (Android 8.1 API 27)



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_multiple_info_dialog, null, false);
getDialog().setTitle(R.string.dialog_fragment_BAC_info_title);
TextView title = getDialog().findViewById(android.R.id.title);
title.setTextSize(30);
title.setTextColor(getResources().getColor(R.color.white));


it falls at the line setTextSize(30) with error




java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setTextSize(float)' on a null object reference




Yes, log say me that title == null

I tried to replace part of code to onDialogCreate(Bundle savedInstanceState)...

But same thing - now app falls on same line inside onDialogCreate...

Any ideas how to fix it?

from Gradle



    compileSdkVersion 27
minSdkVersion 21
targetSdkVersion 27









share|improve this question


















  • 1





    Apparently getting a title on a DialogFragment is kind of a pain, these days. Have a look at this answer. I had to do that to get it to work, but after I did, your code works as expected.

    – Mike M.
    Nov 18 '18 at 18:53






  • 1





    yep. it works for me. thanx a lot

    – Kirguduck
    Nov 18 '18 at 21:02











  • @MikeM. but why? why did it work correctly with previous device ?

    – Kirguduck
    Nov 19 '18 at 6:52











  • Dunno. I didn't go digging through the code, and I'm not able to do any significant testing, at the moment. It was a quick test and fix. I felt guilty for having misled you when I got my questions mixed up earlier, so I figured it was the least I could do. :-)

    – Mike M.
    Nov 19 '18 at 6:57
















0















I`m trying to set title text size for DialogFragment

I coded for my old test device (Android 5.0 API 21) and all was ok

But app falls after I began to use new test device (Android 8.1 API 27)



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_multiple_info_dialog, null, false);
getDialog().setTitle(R.string.dialog_fragment_BAC_info_title);
TextView title = getDialog().findViewById(android.R.id.title);
title.setTextSize(30);
title.setTextColor(getResources().getColor(R.color.white));


it falls at the line setTextSize(30) with error




java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setTextSize(float)' on a null object reference




Yes, log say me that title == null

I tried to replace part of code to onDialogCreate(Bundle savedInstanceState)...

But same thing - now app falls on same line inside onDialogCreate...

Any ideas how to fix it?

from Gradle



    compileSdkVersion 27
minSdkVersion 21
targetSdkVersion 27









share|improve this question


















  • 1





    Apparently getting a title on a DialogFragment is kind of a pain, these days. Have a look at this answer. I had to do that to get it to work, but after I did, your code works as expected.

    – Mike M.
    Nov 18 '18 at 18:53






  • 1





    yep. it works for me. thanx a lot

    – Kirguduck
    Nov 18 '18 at 21:02











  • @MikeM. but why? why did it work correctly with previous device ?

    – Kirguduck
    Nov 19 '18 at 6:52











  • Dunno. I didn't go digging through the code, and I'm not able to do any significant testing, at the moment. It was a quick test and fix. I felt guilty for having misled you when I got my questions mixed up earlier, so I figured it was the least I could do. :-)

    – Mike M.
    Nov 19 '18 at 6:57














0












0








0








I`m trying to set title text size for DialogFragment

I coded for my old test device (Android 5.0 API 21) and all was ok

But app falls after I began to use new test device (Android 8.1 API 27)



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_multiple_info_dialog, null, false);
getDialog().setTitle(R.string.dialog_fragment_BAC_info_title);
TextView title = getDialog().findViewById(android.R.id.title);
title.setTextSize(30);
title.setTextColor(getResources().getColor(R.color.white));


it falls at the line setTextSize(30) with error




java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setTextSize(float)' on a null object reference




Yes, log say me that title == null

I tried to replace part of code to onDialogCreate(Bundle savedInstanceState)...

But same thing - now app falls on same line inside onDialogCreate...

Any ideas how to fix it?

from Gradle



    compileSdkVersion 27
minSdkVersion 21
targetSdkVersion 27









share|improve this question














I`m trying to set title text size for DialogFragment

I coded for my old test device (Android 5.0 API 21) and all was ok

But app falls after I began to use new test device (Android 8.1 API 27)



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_multiple_info_dialog, null, false);
getDialog().setTitle(R.string.dialog_fragment_BAC_info_title);
TextView title = getDialog().findViewById(android.R.id.title);
title.setTextSize(30);
title.setTextColor(getResources().getColor(R.color.white));


it falls at the line setTextSize(30) with error




java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setTextSize(float)' on a null object reference




Yes, log say me that title == null

I tried to replace part of code to onDialogCreate(Bundle savedInstanceState)...

But same thing - now app falls on same line inside onDialogCreate...

Any ideas how to fix it?

from Gradle



    compileSdkVersion 27
minSdkVersion 21
targetSdkVersion 27






android title dialogfragment






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '18 at 17:44









KirguduckKirguduck

234




234








  • 1





    Apparently getting a title on a DialogFragment is kind of a pain, these days. Have a look at this answer. I had to do that to get it to work, but after I did, your code works as expected.

    – Mike M.
    Nov 18 '18 at 18:53






  • 1





    yep. it works for me. thanx a lot

    – Kirguduck
    Nov 18 '18 at 21:02











  • @MikeM. but why? why did it work correctly with previous device ?

    – Kirguduck
    Nov 19 '18 at 6:52











  • Dunno. I didn't go digging through the code, and I'm not able to do any significant testing, at the moment. It was a quick test and fix. I felt guilty for having misled you when I got my questions mixed up earlier, so I figured it was the least I could do. :-)

    – Mike M.
    Nov 19 '18 at 6:57














  • 1





    Apparently getting a title on a DialogFragment is kind of a pain, these days. Have a look at this answer. I had to do that to get it to work, but after I did, your code works as expected.

    – Mike M.
    Nov 18 '18 at 18:53






  • 1





    yep. it works for me. thanx a lot

    – Kirguduck
    Nov 18 '18 at 21:02











  • @MikeM. but why? why did it work correctly with previous device ?

    – Kirguduck
    Nov 19 '18 at 6:52











  • Dunno. I didn't go digging through the code, and I'm not able to do any significant testing, at the moment. It was a quick test and fix. I felt guilty for having misled you when I got my questions mixed up earlier, so I figured it was the least I could do. :-)

    – Mike M.
    Nov 19 '18 at 6:57








1




1





Apparently getting a title on a DialogFragment is kind of a pain, these days. Have a look at this answer. I had to do that to get it to work, but after I did, your code works as expected.

– Mike M.
Nov 18 '18 at 18:53





Apparently getting a title on a DialogFragment is kind of a pain, these days. Have a look at this answer. I had to do that to get it to work, but after I did, your code works as expected.

– Mike M.
Nov 18 '18 at 18:53




1




1





yep. it works for me. thanx a lot

– Kirguduck
Nov 18 '18 at 21:02





yep. it works for me. thanx a lot

– Kirguduck
Nov 18 '18 at 21:02













@MikeM. but why? why did it work correctly with previous device ?

– Kirguduck
Nov 19 '18 at 6:52





@MikeM. but why? why did it work correctly with previous device ?

– Kirguduck
Nov 19 '18 at 6:52













Dunno. I didn't go digging through the code, and I'm not able to do any significant testing, at the moment. It was a quick test and fix. I felt guilty for having misled you when I got my questions mixed up earlier, so I figured it was the least I could do. :-)

– Mike M.
Nov 19 '18 at 6:57





Dunno. I didn't go digging through the code, and I'm not able to do any significant testing, at the moment. It was a quick test and fix. I felt guilty for having misled you when I got my questions mixed up earlier, so I figured it was the least I could do. :-)

– Mike M.
Nov 19 '18 at 6:57












2 Answers
2






active

oldest

votes


















0















this may help, pass the textsize as well as the unit.




* @param unit The desired dimension unit.
* @param size The desired size in the given units.
*
* @attr ref android.R.styleable#TextView_textSize
*/
public void setTextSize(int unit, float size)





share|improve this answer































    0














    thanx to Mike M.

    correct solution is

    to define style for dialog fragment inside onCreate() and set title params inside onCreateDialog() methods of DialogFragment class



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentStyle);
    }


    need to add style in styles.xml



    <style name="DialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">false</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">false</item>
    </style>


    and after need to design dialog



    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    TextView title = dialog.findViewById(android.R.id.title); //here it works!
    title.setTextSize(30);
    title.setText(R.string.dialog_fragment_title);
    title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    title.setTextColor(getResources().getColor(R.color.orange));
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    return dialog;
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363773%2fapp-falls-down-with-api-27-but-wolks-correctly-with-api-21%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0















      this may help, pass the textsize as well as the unit.




      * @param unit The desired dimension unit.
      * @param size The desired size in the given units.
      *
      * @attr ref android.R.styleable#TextView_textSize
      */
      public void setTextSize(int unit, float size)





      share|improve this answer




























        0















        this may help, pass the textsize as well as the unit.




        * @param unit The desired dimension unit.
        * @param size The desired size in the given units.
        *
        * @attr ref android.R.styleable#TextView_textSize
        */
        public void setTextSize(int unit, float size)





        share|improve this answer


























          0












          0








          0








          this may help, pass the textsize as well as the unit.




          * @param unit The desired dimension unit.
          * @param size The desired size in the given units.
          *
          * @attr ref android.R.styleable#TextView_textSize
          */
          public void setTextSize(int unit, float size)





          share|improve this answer














          this may help, pass the textsize as well as the unit.




          * @param unit The desired dimension unit.
          * @param size The desired size in the given units.
          *
          * @attr ref android.R.styleable#TextView_textSize
          */
          public void setTextSize(int unit, float size)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 19:04









          MR.RobotMR.Robot

          355




          355

























              0














              thanx to Mike M.

              correct solution is

              to define style for dialog fragment inside onCreate() and set title params inside onCreateDialog() methods of DialogFragment class



              @Override
              public void onCreate(@Nullable Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentStyle);
              }


              need to add style in styles.xml



              <style name="DialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog">
              <item name="windowActionBar">false</item>
              <item name="windowNoTitle">false</item>
              <item name="android:windowActionBar">false</item>
              <item name="android:windowNoTitle">false</item>
              </style>


              and after need to design dialog



              @NonNull
              @Override
              public Dialog onCreateDialog(Bundle savedInstanceState) {
              Dialog dialog = super.onCreateDialog(savedInstanceState);
              TextView title = dialog.findViewById(android.R.id.title); //here it works!
              title.setTextSize(30);
              title.setText(R.string.dialog_fragment_title);
              title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
              title.setTextColor(getResources().getColor(R.color.orange));
              dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
              return dialog;
              }





              share|improve this answer




























                0














                thanx to Mike M.

                correct solution is

                to define style for dialog fragment inside onCreate() and set title params inside onCreateDialog() methods of DialogFragment class



                @Override
                public void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentStyle);
                }


                need to add style in styles.xml



                <style name="DialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog">
                <item name="windowActionBar">false</item>
                <item name="windowNoTitle">false</item>
                <item name="android:windowActionBar">false</item>
                <item name="android:windowNoTitle">false</item>
                </style>


                and after need to design dialog



                @NonNull
                @Override
                public Dialog onCreateDialog(Bundle savedInstanceState) {
                Dialog dialog = super.onCreateDialog(savedInstanceState);
                TextView title = dialog.findViewById(android.R.id.title); //here it works!
                title.setTextSize(30);
                title.setText(R.string.dialog_fragment_title);
                title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                title.setTextColor(getResources().getColor(R.color.orange));
                dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                return dialog;
                }





                share|improve this answer


























                  0












                  0








                  0







                  thanx to Mike M.

                  correct solution is

                  to define style for dialog fragment inside onCreate() and set title params inside onCreateDialog() methods of DialogFragment class



                  @Override
                  public void onCreate(@Nullable Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentStyle);
                  }


                  need to add style in styles.xml



                  <style name="DialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog">
                  <item name="windowActionBar">false</item>
                  <item name="windowNoTitle">false</item>
                  <item name="android:windowActionBar">false</item>
                  <item name="android:windowNoTitle">false</item>
                  </style>


                  and after need to design dialog



                  @NonNull
                  @Override
                  public Dialog onCreateDialog(Bundle savedInstanceState) {
                  Dialog dialog = super.onCreateDialog(savedInstanceState);
                  TextView title = dialog.findViewById(android.R.id.title); //here it works!
                  title.setTextSize(30);
                  title.setText(R.string.dialog_fragment_title);
                  title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                  title.setTextColor(getResources().getColor(R.color.orange));
                  dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                  return dialog;
                  }





                  share|improve this answer













                  thanx to Mike M.

                  correct solution is

                  to define style for dialog fragment inside onCreate() and set title params inside onCreateDialog() methods of DialogFragment class



                  @Override
                  public void onCreate(@Nullable Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentStyle);
                  }


                  need to add style in styles.xml



                  <style name="DialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog">
                  <item name="windowActionBar">false</item>
                  <item name="windowNoTitle">false</item>
                  <item name="android:windowActionBar">false</item>
                  <item name="android:windowNoTitle">false</item>
                  </style>


                  and after need to design dialog



                  @NonNull
                  @Override
                  public Dialog onCreateDialog(Bundle savedInstanceState) {
                  Dialog dialog = super.onCreateDialog(savedInstanceState);
                  TextView title = dialog.findViewById(android.R.id.title); //here it works!
                  title.setTextSize(30);
                  title.setText(R.string.dialog_fragment_title);
                  title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                  title.setTextColor(getResources().getColor(R.color.orange));
                  dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                  return dialog;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 12:26









                  KirguduckKirguduck

                  234




                  234






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363773%2fapp-falls-down-with-api-27-but-wolks-correctly-with-api-21%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()