+ Reply to Thread
Results 1 to 3 of 3

Thread: Notifier Example

  1. #1
    Join Date
    May 2011
    Posts
    71
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Notifier Example

    When porting the Notifier example to my own project (basically copying the .h, .cpp and .java files over), and calling as shown in the Notifier example, I get this error:

    Qt Code:
    1. W System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' on a null object reference
    To copy to clipboard, switch view to plain text mode 

    It occurs when running this Java code:

    Qt Code:
    1. public static void notify(String s)
    2. {
    3. try {
    4. if (m_notificationManager == null) {
    5. m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
    6. m_builder = new Notification.Builder(m_instance);
    7. m_builder.setSmallIcon(R.drawable.icon);
    8. m_builder.setContentTitle("A message from Qt!");
    9. }
    10.  
    11. m_builder.setContentText(s);
    12. m_notificationManager.notify(1, m_builder.build());
    13. } catch(Throwable e) {
    14. e.printStackTrace();
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    To me it appears that the 'm_instance' variable is null... but why? If I open and run the Notifier example, it works fine.

  2. #2
    Join Date
    Jul 2008
    Posts
    315
    Thanks
    9
    Thanked 39 Times in 37 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Notifier Example

    Hi, from that code snippet it is impossible to tell because m_instance is only being used. Please show the code where you assign m_instance.

    Ginsengelf

  3. #3
    Join Date
    May 2011
    Posts
    71
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Notifier Example

    I do it the same way as it is done in the example, with one change... adding in the 'try...catch' section just to try to figure out what's going on. It does the same whether or not that is in the code.

    I call in my main.cpp:
    Qt Code:
    1. NotificationClient *notificationClient = new NotificationClient();
    To copy to clipboard, switch view to plain text mode 

    ...and add it to my QML files so they have access:
    Qt Code:
    1. myFunctions->engine->rootContext()->setContextProperty("_notifier", notificationClient);
    To copy to clipboard, switch view to plain text mode 

    ...and call it from QML:
    Qt Code:
    1. _notifier.setMyNotification(myNote);
    To copy to clipboard, switch view to plain text mode 

    notificationclient.h:
    Qt Code:
    1. #ifndef NOTIFICATIONCLIENT_H
    2. #define NOTIFICATIONCLIENT_H
    3.  
    4. #include <QObject>
    5.  
    6. class NotificationClient : public QObject
    7. {
    8. Q_OBJECT
    9. Q_PROPERTY(QString notification READ notification WRITE setNotification NOTIFY notificationChanged);
    10.  
    11. public:
    12. explicit NotificationClient(QObject *parent = 0);
    13.  
    14. void setNotification(const QString &notification);
    15. QString notification() const;
    16.  
    17. public slots:
    18. void setMyNotification(QString myNotification);
    19.  
    20. signals:
    21. void notificationChanged();
    22.  
    23. private slots:
    24. void updateAndroidNotification();
    25.  
    26. private:
    27. QString m_notification;
    28. };
    29.  
    30. #endif // NOTIFICATIONCLIENT_H
    To copy to clipboard, switch view to plain text mode 

    notificationclient.cpp:
    Qt Code:
    1. #include "notificationclient.h"
    2.  
    3. #include <QAndroidJniObject>
    4.  
    5. NotificationClient::NotificationClient(QObject *parent)
    6. : QObject(parent)
    7. {
    8. connect(this, SIGNAL(notificationChanged()), this, SLOT(updateAndroidNotification()));
    9. }
    10.  
    11. void NotificationClient::setNotification(const QString &notification)
    12. {
    13. if (m_notification == notification)
    14. return;
    15.  
    16. m_notification = notification;
    17. emit notificationChanged();
    18. }
    19.  
    20. void NotificationClient::setMyNotification(QString myNotification)
    21. {
    22. setNotification(myNotification);
    23. }
    24.  
    25. QString NotificationClient::notification() const
    26. {
    27. return m_notification;
    28. }
    29.  
    30. void NotificationClient::updateAndroidNotification()
    31. {
    32. QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
    33. QAndroidJniObject::callStaticMethod<void>("com/nascg/nascg/btstWxUpload/NotificationClient",
    34. "notify",
    35. "(Ljava/lang/String;)V",
    36. javaNotification.object<jstring>());
    37. }
    To copy to clipboard, switch view to plain text mode 

    JAVA:
    Qt Code:
    1. package MySuperCustomPackageName;
    2.  
    3. import android.app.Notification;
    4. import android.app.NotificationManager;
    5. import android.content.Context;
    6.  
    7. public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
    8. {
    9. private static NotificationManager m_notificationManager;
    10. private static Notification.Builder m_builder;
    11. private static NotificationClient m_instance;
    12.  
    13. public NotificationClient()
    14. {
    15. m_instance = this;
    16. }
    17.  
    18. public static void notify(String s)
    19. {
    20. try {
    21. if (m_notificationManager == null) {
    22. m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
    23. m_builder = new Notification.Builder(m_instance);
    24. m_builder.setSmallIcon(R.drawable.icon);
    25. m_builder.setContentTitle("A message from Qt!");
    26. }
    27.  
    28. m_builder.setContentText(s);
    29. m_notificationManager.notify(1, m_builder.build());
    30. } catch(Throwable e) {
    31. e.printStackTrace();
    32. }
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

+ Reply to Thread

Similar Threads

  1. Qt Notifier - Qt 5.2 Example App
    By Xevoius in forum Qt Programming
    Replies: 1
    Last Post: 14th February 2014, 08:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts