java解决 No enclosing instance of type XXX is accessible的问题

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

在LifeCircle这个类中:

public class LifeCircle {

	public class Mybroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {

		}

	}
}

如果在Activity这样去new:

public class Settings extends Activity {

	@Override
	protected 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 {

		@Override
		public 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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_layout);
		LifeCircle lifeCircle = new LifeCircle(this);
		lifeCircle.registerBroadcaster();
	}

}
时间: 2024-12-09 02:59:40

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

Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题

今天在编译Java程序时遇到如下问题: No enclosing instance of type PrintListFromTailToHead is accessible. Must qualify the allocation with an enclosing instance of type PrintListFromTailToHead (e.g. x.new A() where x is an instance of PrintListFromTailToHead). 源代码为:

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

Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing 最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing ins

Java编译时出现No enclosing instance of type XXX is accessible.

今天在编译Java程序的时候出现以下错误: No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main). 我原来编写的源代码是这样的: public class Main {class Dog //定义一个“狗类”{privat

Java中出现No enclosing instance of type XXX is accessible问题

Java编写代码过程中遇到了一个问题,main方法中创建内部类的实例时,编译阶段出现错误,查看错误描述: Multiple markers at this line - The value of the local variable test is not used - No enclosing instance of type StaticCallDynamic is accessible. Must qualify the allocation with an enclosing insta

Java 出现 No enclosing instance of type Test1 is accessible.(转,自己也遇到了!)

最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type E(e.g.  x.new A() where x is an instance of E). E指代我写的那个内部类. 根据提示,没有可访问的内部类E的实例,必须分配一

Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing[转]

最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type E(e.g.  x.new A() where x is an instance of E). E指代我写的那个内部类. 根据提示,没有可访问的内部类E的实例,必须分配一

Java错误No enclosing instance of type Hidden is accessible

今天在写一个小的程序时出现No enclosing instance of type Hidden is accessible. Must qualify the allocation with an enclosing instance of type Hidden (e.g. x.new A() where x is an instance of Hidden).的错误,寻找了很长时间,终于解决了. 我的程序是: public class Hidden { class Father{ /*p

java出现No enclosing instance of type main is accessible. Must qualify the allocation with an enclosing instance of type main (e.g. x.new A() where x is an instance of main).

根据提示,没有可访问的内部类E的实例,必须分配一个合适的内部类E的实例(如x.new A(),x必须是E的实例.)看着这句提示,我就纳闷了,我已经用new实例化了这个类,为什么还不行呢.原来我写的内部类是动态的,也就是开头以public class开头.而主程序是public static class main.在Java中,类中的静态方法不能直接调用动态方法.只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法.所以在不做其他变动的情况下,最简单的解决办法是将publ

android No enclosing instance of type BasicActivity is accessible. Must qualify the allocation with

No enclosing instance of type BasicActivity is accessible. Must qualify the allocation with an enclosing instance of type BasicActivity (e.g. x.new A() where x is an instance of BasicActivity). 描述: 在BasicActivity.java中写了一个public class 的内部类,在另一个java文件