PDA

View Full Version : Notifier Example



scgrant327
13th September 2017, 17:04
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:


W System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.St ring)' on a null object reference

It occurs when running this Java code:


public static void notify(String s)
{
try {
if (m_notificationManager == null) {
m_notificationManager = (NotificationManager)m_instance.getSystemService(C ontext.NOTIFICATION_SERVICE);
m_builder = new Notification.Builder(m_instance);
m_builder.setSmallIcon(R.drawable.icon);
m_builder.setContentTitle("A message from Qt!");
}

m_builder.setContentText(s);
m_notificationManager.notify(1, m_builder.build());
} catch(Throwable e) {
e.printStackTrace();
}
}

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

Ginsengelf
14th September 2017, 07:19
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

scgrant327
14th September 2017, 13:43
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:


NotificationClient *notificationClient = new NotificationClient();

...and add it to my QML files so they have access:


myFunctions->engine->rootContext()->setContextProperty("_notifier", notificationClient);

...and call it from QML:

_notifier.setMyNotification(myNote);

notificationclient.h:

#ifndef NOTIFICATIONCLIENT_H
#define NOTIFICATIONCLIENT_H

#include <QObject>

class NotificationClient : public QObject
{
Q_OBJECT
Q_PROPERTY(QString notification READ notification WRITE setNotification NOTIFY notificationChanged);

public:
explicit NotificationClient(QObject *parent = 0);

void setNotification(const QString &notification);
QString notification() const;

public slots:
void setMyNotification(QString myNotification);

signals:
void notificationChanged();

private slots:
void updateAndroidNotification();

private:
QString m_notification;
};

#endif // NOTIFICATIONCLIENT_H


notificationclient.cpp:

#include "notificationclient.h"

#include <QAndroidJniObject>

NotificationClient::NotificationClient(QObject *parent)
: QObject(parent)
{
connect(this, SIGNAL(notificationChanged()), this, SLOT(updateAndroidNotification()));
}

void NotificationClient::setNotification(const QString &notification)
{
if (m_notification == notification)
return;

m_notification = notification;
emit notificationChanged();
}

void NotificationClient::setMyNotification(QString myNotification)
{
setNotification(myNotification);
}

QString NotificationClient::notification() const
{
return m_notification;
}

void NotificationClient::updateAndroidNotification()
{
QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
QAndroidJniObject::callStaticMethod<void>("com/nascg/nascg/btstWxUpload/NotificationClient",
"notify",
"(Ljava/lang/String;)V",
javaNotification.object<jstring>());
}


JAVA:

package MySuperCustomPackageName;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;

public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
{
private static NotificationManager m_notificationManager;
private static Notification.Builder m_builder;
private static NotificationClient m_instance;

public NotificationClient()
{
m_instance = this;
}

public static void notify(String s)
{
try {
if (m_notificationManager == null) {
m_notificationManager = (NotificationManager)m_instance.getSystemService(C ontext.NOTIFICATION_SERVICE);
m_builder = new Notification.Builder(m_instance);
m_builder.setSmallIcon(R.drawable.icon);
m_builder.setContentTitle("A message from Qt!");
}

m_builder.setContentText(s);
m_notificationManager.notify(1, m_builder.build());
} catch(Throwable e) {
e.printStackTrace();
}
}
}