最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【已解决】Android中创建变量出错:No enclosing instance of type crifanLibSongtaste is accessible

Android crifan 522浏览 0评论

【问题】

Android的app中,创建一个嵌套的类的变量:

public class crifanLibSongtaste {
	public class albumInfo
	{
		public String url;
		public String name;
		public String author;
	};
	
	public class songInfo {
		public String id;       // 2224853
		public String url;      // http://www.songtaste.com/song/2224853/
		public String realAddr; // http://m6.songtaste.com/201204131040/90ee3460a764e82816d5233fe2acaccc/6/62/625c4102d5a2f89f614d874b2c2ca402.mp3
		public String artist;   // DJ OKAWARI
		public String title;    // Flower Dance
		public String suffix;   // .mp3
		public String storedName; //Flower Dance - DJ OKAWARI.mp3
		
		//public string recommender;// loveqian1314
		//public string recommenderId;// 3334687
		//public string recommenderUrl;// http://songtaste.com/user/3334687/
		
		public songInfo()
		{
			id = "";
			url = "";
			realAddr = "";
			artist = "";
			title = "";
			suffix = "";
			storedName = "";
		}
	};

 

代码是:

import crifan.com.crifanLibSongtaste;
import crifan.com.crifanLibSongtaste.songInfo;
crifanLibSongtaste.songInfo curSongInfo = new crifanLibSongtaste.songInfo();

结果出错:

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

 

【解决过程】

1.根据其提示,自己终于弄出来了:

import crifan.com.crifanLibSongtaste;
import crifan.com.crifanLibSongtaste.songInfo;

public class MainActivity extends Activity {
	crifanLibSongtaste songtaste = null;
    
		//crifanLibSongtaste.songInfo curSongInfo = new crifanLibSongtaste.songInfo();
		crifanLibSongtaste.songInfo curSongInfo = songtaste.new songInfo();
        
}
    

 

【总结】

新建一个嵌套的类的变量,需要先新建一个主类的变量,即实例

然后用主类实例去new一个嵌套类的变量。

转载请注明:在路上 » 【已解决】Android中创建变量出错:No enclosing instance of type crifanLibSongtaste is accessible

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
15 queries in 0.326 seconds, using 10.28MB memory