findviewbyid找不到控件

sephirothzjf 2012-09-28 12:12:13
苦脑了很久,请各位高手们帮我解决,我是新手!我的findViewById找不到定义好的控件不知是为什么。具体原码如下:
layout下xml文件原码:
<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".Activity01" />

<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
tools:context=".Activity01" />

R文件ID原码:
public static final class id {
public static final int menu_settings=0x7f070002;
public static final int mybutton=0x7f070001;
public static final int mytext=0x7f070000;
}

src文件下java文件原码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity01);
TextView myText = (TextView)this.findViewById(android.R.id.mytext);
Button mybutton=(Button)this.findViewById(android.R.id.mybutton);

}

如以上所示,mytext和mybutton提示mytext cannot be resolved or is not a field。请各位大虾帮忙。
...全文
1016 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
w1433189426 2013-12-16
  • 打赏
  • 举报
回复
你有没有先选择保存layout。
w1433189426 2013-12-09
  • 打赏
  • 举报
回复
你可以试下加红色的代码TextView myText = (TextView)this.myText.findViewById(android.R.id.mytext); Button mybutton=(Button)this.mybutton.findViewById(android.R.id.mybutton);
sky123123 2012-09-29
  • 打赏
  • 举报
回复
xinyuetonghua 2012-09-28
  • 打赏
  • 举报
回复
TextView myText = (TextView)this.findViewById(android.R.id.mytext);


这个有问题
owengood2012 2012-09-28
  • 打赏
  • 举报
回复
再不行,就project--clean一下,重新构造环境。
逛街的猫 2012-09-28
  • 打赏
  • 举报
回复
把Android去掉! 像楼上的那样!
Jobernowl 2012-09-28
  • 打赏
  • 举报
回复
把R的import给删除掉再照一楼所说的
i_aaronw 2012-09-28
  • 打赏
  • 举报
回复
同意楼上的
summersrest 2012-09-28
  • 打赏
  • 举报
回复
嗯,楼上说的都对。你的包导入错了。
TextView myText = (TextView)this.findViewById(android.R.id.mytext);
应该改为
TextView myText = (TextView)this.findViewById(R.id.mytext);
把你上面导入的删掉。然后重新导入。
逐Ls梦 2012-09-28
  • 打赏
  • 举报
回复
TextView myText = (TextView)this.findViewById(android.R.id.mytext);
Button mybutton=(Button)this.findViewById(android.R.id.mybutton);
你的R包导错了。要导你项目对应的R包
AMinfo 2012-09-28
  • 打赏
  • 举报
回复
android.R.id.mytext改为R.id.mytext
android.R.id.mybutton改为R.id.mybutton

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧