简体   繁体   English

Java通知功能怪异的行为

[英]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. 我有一个要在Qt中构建的应用程序,同时也正在实现Java,以便能够使用Google Play服务。

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 Notifier示例中获取了此函数,但似乎该函数在调用Java API时立即引发错误或其他错误。

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. Qt没有显示任何错误,因此我在每一行之后添加了print语句,以查看错误的位置,并在调用Java库后立即停止该函数。

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) (s =测试字符串)

I have also gone through the Notification example's directories and the AndroidManifest. 我还浏览了Notification示例的目录和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. 但是我认为这些库应该能够很好地导入,因为我已经使用Android-SDK提供的Android命令行工具将google-play-API引用到了我的项目中。

So my question is: What could I have done wrong? 所以我的问题是:我做错了什么?

and for completeness sake, here is the code of the .java file: 为了完整起见,这是.java文件的代码:

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. 抛出了nullptr异常。 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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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