Java通知函数奇怪的行为

时间:2015-04-23 12:31:10

标签: java android qt

我有一个我在Qt中构建的应用程序,我正在实现Java,以便我能够使用Google-Play-Services。

我从Qt Notifier示例中获取了此函数,但似乎该函数在调用java API时会抛出错误或其他内容。

Qt没有显示任何错误,所以我在每一行之后添加了print语句以查看它出错的地方,一旦调用了Java库,该函数就会停止。

这是功能:

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");
}

,输出为:

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

(s =测试字符串)

我还浏览了Notification示例的目录和AndroidManifest。但我认为这些库应该可以导入正常,因为我已经使用Android-SDK提供的Android命令行工具将google-play-API引用到我的项目中。

所以我的问题是:我做错了什么?

为了完整起见,这里是.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");
    }
}

1 个答案:

答案 0 :(得分:0)

抛出了nullptr异常。我抓住了它:

    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();
        }
    }