有些时候我们要把Activity的一些实现类移到java类里来实现,比如把写以下两个类:

在LifeCircle这个类中:

public class LifeCircle {public class Mybroadcast extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {}}
}

如果在Activity这样去new:

public class Settings extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.test_layout);LifeCircle lifeCircle = new LifeCircle(this);Mybroadcast cast = new Mybroadcast();}}

编译器会报一个错误:

 - No enclosing instance of type LifeCircle is accessible. Must qualify the allocation with an enclosing instance of type LifeCircle (e.g. x.new A() where x is an instance of LifeCircle).

一种解决方法是把Mybroadcast定义为static class

令一种这样来弄:

package com.example.shoplistdownload;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;public class LifeCircle {Context mContext;private static LifeCircle sInstance;private Mybroadcast mBroadcast;private Settings mInstance;LifeCircle(Context context) {mContext = context;mInstance = (Settings)context;}public static LifeCircle getIntance(Context context) {if (sInstance == null) {sInstance = new LifeCircle(context);}return sInstance;}public class Mybroadcast extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {}}public void registerBroadcaster() {mBroadcast = new Mybroadcast();IntentFilter filter = new IntentFilter();filter.addAction("com.example.action");mInstance.registerReceiver(mBroadcast, filter);}
}
public class Settings extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.test_layout);LifeCircle lifeCircle = new LifeCircle(this);lifeCircle.registerBroadcaster();}}

java解决 No enclosing instance of type XXX is accessible的问题相关推荐

  1. Java变异出现错误:No enclosing instance of type XXX is accessible

    摘要:写java代码时遇到下面的编译错误. 本文分享自华为云社区<Java中出现No enclosing instance of type XXX is accessible问题>,作者: ...

  2. Java出现No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosin

    最近研究LDA源代码时,里面涉及到Comparable方法的使用.以前用过这个排序方法,现在想回顾一下.以下是程序,感觉没问题啊,结果报错了: Exception in thread "ma ...

  3. [Java]Error:No enclosing instance of type E is accessible. Must qualify the allocation with an enclo

    在自己试着写一个链表的例子时,由于参考的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible. Must ...

  4. enclosing type java_Java中出现No enclosing instance of type XXX is accessible问题

    Java编写代码过程中遇到了一个问题,main方法中创建内部类的实例时,编译阶段出现错误,查看错误描述: Multiple markers at this line - The value of th ...

  5. No enclosing instance of type xxx is accessible

    No enclosing instance of type test5 is accessible. Must qualify the allocation with an enclosing ins ...

  6. 异常: No enclosing instance of type xx is accessible. Must qualify (Java 内部类)

    异常 前言. 开发时候遇到内部类的问题. 异常 No enclosing instance of type SparkProgrammingGuide is accessible. Must qual ...

  7. No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc

    java : No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing ...

  8. Java中出现No enclosing instance of type Demo01 is accessible解决方法参考

    出现类似以下错误 No enclosing instance of type Demo01 is accessible. Must qualify the allocation with an enc ...

  9. 解决No enclosing instance of type * is accessible

    写一个内部类,并在构造函数中初始化时,遇到报错,搜索问题后发现,有网友出现过类似的问题,下面这个是说的浅显明白的,并确实解决了问题.于是,以下内容照搬过来,不再多费键盘了. public class ...

最新文章

  1. 在GitHub上搭建GitHub Pages博客-- Jekyll
  2. Spring Cloud Edgware新特性之一:解决Eureka中Jersey 1.x版本过旧的问题-不使用Jersey
  3. Leetcode5635. 构建字典序最大的可行序列[C++题解]:dfs暴搜
  4. [Embeding-1]Efficient Estimation of Word Representations in Vector Space 译文
  5. less css用法思维导图
  6. ue4 设置intellisence_UE4.22编辑器界面操控设置(4)
  7. Ubuntu安装完后设置root密码
  8. LeetCode 652. 寻找重复的子树(DFS)
  9. 美国 cs 工作_我是如何退出美国CS计划的,回到埃及,然后立即在一家公司获得了一份开发工作。...
  10. final、finally、finalize
  11. matplotlib的学习
  12. 笔记-Attention机制
  13. 初始MySQL增删改查数据
  14. Unity Shader - ShaderLab: Pass Tags Pass的Tags标签
  15. HTTP压测工具之wrk
  16. 20个最热门的IT技术职位及薪资
  17. JavaScript 中 List转Tree,Tree转List
  18. UG\NX二次开发 获取UG窗口句柄。实现操作UG窗口最小化、最大化、关闭功能
  19. python怎么做表格_零基础小白怎么用Python做表格?
  20. linux 开发板传文件,Linux下用USB传输文件到开发板

热门文章

  1. 蓝桥杯单片机学习记录(8)(PWM脉宽调制)
  2. windows快捷方式
  3. 分布式链路追踪系统深入理解
  4. Nginx实现https反向代理配置
  5. GBase XDM用户管理
  6. 后台站点-菜单管理功能(一)
  7. 手机耳机插入电脑没有反应
  8. pci 中断冲突_PCI设备中断冲突的基本解决办法
  9. 尝试一下LLJ大佬的理论AC大法
  10. 使用机器学习数据集构建销售预测Web应用程序