传递 context 导致 NullPointerException
来源:互联网 发布:11月经济数据点评 编辑:程序博客网 时间:2024/06/05 19:11
因需要在非activity 中使用getSystemService,需要将context传递过程中。
原先代码如下:
private Context mContext; public GetServerInfo(Context context) { mContext = context; } public String HostIP = getWIFILocalIpAdress(); public String getWIFILocalIpAdress() { //获取wifi服务 WifiManager wifiManager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE); //判断wifi是否开启 if (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); } WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = formatIpAddress(ipAddress); return ip; }
或直接使用getWIFILocalIpAdress,则不会则正常。
不止原因,mark预留。
0 0
- 传递 context 导致 NullPointerException
- InvocationTargetException导致NullPointerException
- context.getSystemService(context.LAYOUT_INFLATER_SERVICE)调用时报NullPointerException
- 消除Context为空的NullPointerException异常
- 消除Context为空的NullPointerException异常
- Context 传递数据(转载)
- Context 传递数据
- React数据传递---context
- 多线程传递Context multi-thread context(MTC)
- <5> go 上下文传递context
- Android中Context的传递
- NullPointerException
- NullPointerException
- NullPointerException
- NullPointerException
- NullPointerException
- NullPointerException
- NullPointerException
- 初探Swift 3.0的重大变化
- Linux C 函数指针应用---回调函数
- Java 并发之 CountDownLatch、CyclicBarrier 和 Semaphore
- oracle学习之bulk collect用法
- STM32时钟系统
- 传递 context 导致 NullPointerException
- C#访问修饰符
- 定制适用于ARM平台的Ubuntu rootfs(根文件系统)
- MySQL5.7 ERROR 1142 (42000)问题
- view绘制流程
- 第4章第1节练习题3 二叉树特殊节点个数统计
- 解放军上将:日本精神上永远是侏儒 心理永远是弱者
- 数据库事务隔离级别
- Ubuntu下好用的软件推荐