- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想简单地创建一个 Android 通知类以在任何地方使用它,但是在这段代码中我得到 notificationManager
变量的 NullPointerException
错误:
public class TsNotify extends Activity {
private NotificationManager notificationManager;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
notificationManager = (NotificationManager) getSystemService ( Context.NOTIFICATION_SERVICE );
}
public void notify( String title ) {
...
notificationManager.notify ( NOTIFICATION_ID, notification );
}
}
我将其更改为这一行,但这个问题没有解决:
notificationManager = (NotificationManager) this.getSystemService ( Context.NOTIFICATION_SERVICE );
或
notificationManager = (NotificationManager) getBaseContext().getSystemService ( Context.NOTIFICATION_SERVICE );
notificationManager
为 NULL
最佳答案
我想建议更好的方法,不要扩展 Activity 类,因为您需要始终启动该 Activity ,以便调用“oncreate”方法并初始化通知管理器。具有可用的应用程序上下文的单独类(在扩展应用程序的类上创建,并将其添加到应用程序标签中的 android list )。现在在该类中,将重写方法写在您想要通知的位置,例如图像通知、简单文本通知、 Action 通知。
空指针异常可能是因为您没有调用启动 Activity 。而且你的方法也不是很干净。
关于Android 为 getSystemService 获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27396151/
我正在尝试编写一个服务来获取 Gear Live 上的心率,按照这里的问题 Get Heart Rate from "Sensor" Samsung Gear Live 如果我把这部分放在 L
所以我跟随this tutorial ,我被困住了。在 Eclipse 中,我收到一条错误消息,指出“UserItemAdapter 类型未定义方法 getSystemService(String)”
在 Android eclipse 中当我想使用非静态方法“getSystemService”时,它显示错误: Cannot make a static reference to the non-st
我已按照本教程获取我的 ListView 填充数据 -- http://android.amberfog.com/?p=296 . 但它只有在我像教程那样子类化我的自定义列表适配器 (MyCustom
如果我连接到 WLAN,以下行将不会导致空指针异常: ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.C
我遵循了如何制作下载 .pdf 文件的应用程序的教程。 代码如下: protected void onCreate(Bundle savedInstanceState) { super.onC
我需要在自定义微调器中使用“getSystemService”,所以自定义微调器会扩展 getSystemService Will Use To Copy Text To Clip Board is
我试图将 onClickListener 添加到 RecyclerView 中的一个按钮,该按钮将复制一个字符串,但它说 getSystemService(CLIPBOARD_SERVICE) 不可用
乍一看下面的代码 mLocationManager对象在 onCreate(...) 之后应该超出范围已完成,预期的行为是 onLocationChanged在对象被垃圾回收之前,永远不会被调用或调用
我想测试一些关于USB设备的东西,我尝试编写一个小程序,我确信这是错误的,但这不是这个问题的重点。我确信我的导入没问题,但 Android Studio 拒绝构建此类,并出现有关 GetSystemS
现在已经为此苦苦挣扎了很长一段时间,足以让此处的用户实际上在堆栈上溢出。我们正在开发一个 android 应用程序,我想测试一个 Activity 类中的方法。我以前做过一些单元测试,但从来没有针对
此方法是否检查实际连接到互联网或设备是否连接到无线网络。 我很想知道,如果设备已连接到路由器,但路由器未连接到网络或已关闭,它是否会返回 true。 我的代码:- private boolean is
当我使用“getSystemService”时,我遇到了这个错误The method getSystemService(String) is undefined for the type WebApp
所以我现在只是试图在我的 getView 函数中膨胀一个 View ,而 getContext() 出于某种原因说它是未定义的.. package com.MTSUAndroid; import co
Context.getSystemService 的文档州 Returns: The service or null if the name does not exist. 接受的名称列表列为 AC
getSystemService 的文档建议不要在各种不同的上下文之间共享服务对象。 对于单个上下文,是通过将服务对象分配给 onCreate() 中的实例字段来缓存服务对象,还是应该在使用时获取?惯
context.getSystemService() 是一个昂贵的调用吗? 即我已经构建了一个小 http 网络库(我知道还有其他可用的 http 网络库),它使用 ConnectivityManag
我遇到了这个问题:getSystemService 定义在 Context 类中,所以我假设它在 context.getSystemService 时被调用。我无法理解以下代码,其中 Broadcas
我怎样才能像下面的代码那样在 fragment 类中使用 getSystemService final EditText input = new EditText(getContext()); Inp
我的问题是,在我的 MainActivity 的 onCreate() 方法中,我正在创建新的 Thread 对象,我想将对 this Activity 的引用传递给该对象,然后在该线程中使用它来调用
我是一名优秀的程序员,十分优秀!