简体   繁体   中英

Java notify function weird behavior

I have an app that I am building in Qt and I am implementing Java aswell so that I am able to use the Google-Play-Services.

I took this function from the Qt Notifier example but it seems that the function throws an error or something as soon as it calls the java API's.

Qt doesn't show any errors so I added print statements after every line to see where it goes wrong and as soon as a Java library gets called, the function stops.

this is the function:

public static void notify(String s)
{
    System.out.println(s);

    if (m_notificationManager == null) {
        System.out.println("1111");
        m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
        System.out.println("2222");
        m_builder = new Notification.Builder(m_instance);
        System.out.println("3333");
        m_builder.setContentTitle("A message from Qt!");
        System.out.println("4444");
    }

    System.out.println("5555");
    m_builder.setContentText(s);
    System.out.println("6666");
    m_notificationManager.notify(1, m_builder.build());
    System.out.println("7777");
}

and the output is:

I/System.out(25125): test string
I/System.out(25125): 1111

(s = test string)

I have also gone through the Notification example's directories and the AndroidManifest. But I think the libraries should be able to import fine as I have referenced the google-play-API to my project with the Android commandline tool provided by the Android-SDK.

So my question is: What could I have done wrong?

and for completeness sake, here is the code of the .java file:

package org.qtproject.qt5.example;

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()
    {
        System.out.println("it works2222");
        m_instance = this;
    }

    public static void notify(String s)
    {
        System.out.println(s);

        if (m_notificationManager == null) {
            System.out.println("1111");
            m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
            System.out.println("2222");
            m_builder = new Notification.Builder(m_instance);
            System.out.println("3333");
            m_builder.setContentTitle("A message from Qt!");
            System.out.println("4444");
        }

        System.out.println("5555");
        m_builder.setContentText(s);
        System.out.println("6666");
        m_notificationManager.notify(1, m_builder.build());
        System.out.println("7777");
    }
}

A nullptr Exception was thrown. I caught it with:

    public static void notify(String s)
    {
        try {
            if (m_notificationManager == null) {
                m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM