Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Toasts on Android and "getApplicationContext()" problem
Forum Updated to NodeBB v4.3 + New Features

Toasts on Android and "getApplicationContext()" problem

Scheduled Pinned Locked Moved Mobile and Embedded
2 Posts 2 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MathiasH
    wrote on 19 May 2014, 20:42 last edited by
    #1

    Hi,
    in my app I wanna use the Toast function of Android and wanna call it through callStaticMethod. Here is my code:
    @ public static void settoast(String s)
    {
    Toast.makeText(____, s, Toast.LENGTH_SHORT).show();
    }@
    There, where u can see ____ has to be the contex but I really dont know how to get it. I also tried this:

    @ private static NotificationClient m_instance;

    public NotificationClient()
    {
        m_instance = this;
    }
    
    public static void settoast(String s)
    {
    
        Toast toast = new Toast();
    
        toast.makeText(m_instance.getApplicationContext(), s, Toast.LENGTH_LONG).show();
    }@
    

    but it didnt work.
    Actually the App crashed instant...
    Could u please tell me what context i have to use in this gap?

    Thx for helping me
    MathiasH

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexleutgoeb
      wrote on 20 May 2014, 22:25 last edited by
      #2

      We are using the application context in our "plugins":http://plugins.v-play.net with the help of the QtNative java class (bundled within the default Qt classes) with a call to the "activity()":https://qt.gitorious.org/qt/qtbase/source/439a6fec84a62b9d166b84dc7574ddc250a38bfb:src/android/jar/src/org/qtproject/qt5/android/QtNative.java#L91 method:

      @org.qtproject.qt5.android.QtNative.activity()@

      This method should be globally accessible as it's declared as static & public and gets set by Qt itself.

      Make also sure to call the UI-related Toast method on the UI thread (with runOnUiThread()).

      Hope this helps!

      Best,
      Alex from V-Play

      1 Reply Last reply
      0

      1/2

      19 May 2014, 20:42

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved