??斗地主捕鱼电竞提现秒到 广告位招租 - 15元/月全站展示
??支付宝搜索579087183领大额红包 ??伍彩集团官网直营彩票
??好待遇→招代理 ??伍彩集团官网直营彩票
??络茄网 广告位招租 - 15元/月全站展示
java解决 No enclosing instance of type XXX is accessible的问题

转载   2014-08-26   浏览量:585


有些时候我们要把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();
	}

}


转载自:https://www.2cto.com/kf/201408/328679.html

招聘 不方便扫码就复制添加关注:程序员招聘谷,微信号:jobs1024



报错:Accessdeniedforuser'root'@'localhost' usingpassword:YES 的解决办法
报错:[email protected](usingpassword:YES)的解决办法。解决方案:打开MySQL目录下的myini文件,在文件的最后添加一行“skip-grant-tables”,保存并关闭文件。(WIN7默认安装,myini在C:ProgramDataMySQLMySQLServer56)
java解决 No enclosing instance of type XXX is accessible的问题
有些时候我们要把Activity的一些实现类移到java类里来实现,比如把写以下两个类:在LifeCircle这个类中:publicclassLifeCircle{publicclassMybroadcastextendsBroadcastReceiver{@Overrid
RandomAccessFile类的简单介绍,以及代码演示
RandomAccessFile类,支持对文件的读取和写入。并且可以设置写入和读取的位置。该位置主要是以字节体现。相当于该文件存储在一个大型byte[]数组。隐含存在一个指向该数组的指针。类似于该类对象可以设置该隐含...
windows环境下出现Error 1045 28000 Access Denied for user 'root'@'localhost'解决方法
windows环境下出现Error1045(28000)[email protected]解决方法。1先在安装目录找到myini配置文件,打开配置文件,找到[mysqld]一行,在下面添加skip-grant-tables后保存该文件,重新启mysql动服务。
##Accessdeniedforuser'root'@'localhost' usingpassword:YES 问题解决方案
[email protected](usingpassword:YES)问题解决方案。解决方案:虚拟机里面安装的mysql数据库
ERROR1045 28000 :Accessdeniedforuser'root'@'192.168.8.123' usingpassword:NO
ERROR1045(28000):Accessdeniedforuser&39;root&39;@&39;1921688123&39;(usingpassword:NO),链接MySQL报错这个。
ERROR1045 28000 :Accessdeniedforuser'root'@'localhost' usingpassword:NO
ERROR1045(28000):Accessdeniedforuser39;root39;@39;localhost39;(usingpassword:NO),最近在大修服务器数据库的时候,发现了一个问题!输入mysql会报一个错误。
ERROR1045 28000 : Access denied for user ‘root’@’localhost’ using password: NO
ERROR1045(28000):Accessdeniedforuser‘root’@’localhost’(usingpassword:NO)1停止mysql数据库etcinitdmysqldstop2执行如下命令(等待时间可能会久)mysqld_safe-
SequelizeAccessDeniedError的原因和解决方式
SequelizeAccessDeniedError的原因和解决方式。最后发现是写mysql的时候授权后没有flushprivileges;的原因。刚开始只写了第一句,没有写第二句。就出现了上面的错误。