最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android中找不到getContext:The method getContext() is undefined for the type

Android crifan 7163浏览 0评论

【问题】

折腾:

【未解决】Android中用TabHost去addTab添加TAB结果出错:Unable to start activity ComponentInfo: android.content.res.Resources$NotFoundException: Resource ID #0x0

期间,去尝试改为:

		//TabHost tabHost = new TabHost(this);
		TabHost tabHost = new TabHost(getContext(), null);

结果出错:

The method getContext() is undefined for the type

 

【解决过程】

1.其实之前在:

【已解决】android中在Tab页面中动态创建多个Group

就发现此问题:

找不到getContext()

了,但是当时没去深究。

2.参考:

java – Why am I getting "The method getContext() is undefined for the type MainActivity"? – Stack Overflow

没用。

3.参考:

dialog – Android AlertDialog.Builder method getContext() not defined – Stack Overflow

其也提到了:

错误提示中的:

换成

getBaseContext()

所以去试试:

		//TabHost tabHost = new TabHost(getContext(), null);
		TabHost tabHost = new TabHost(getBaseContext(), null);

貌似真的就可以了,至少没有编译错误了:

change getContext to getBaseContext no error

4.然后去运行,看看是否正常。然后果真是可以了。

 

【总结】

对于:

TabHost tabHost = new TabHost(getContext(), null);

中找不到getContext,则可以去换成:

TabHost tabHost = new TabHost(getBaseContext(), null);

就可以了。

转载请注明:在路上 » 【已解决】Android中找不到getContext:The method getContext() is undefined for the type

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.140 seconds, using 22.13MB memory