首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

findviewbyid找不到控件解决方法

2013-12-13 
findviewbyid找不到控件苦脑了很久,请各位高手们帮我解决,我是新手!我的findViewById找不到定义好的控件不

findviewbyid找不到控件
苦脑了很久,请各位高手们帮我解决,我是新手!我的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。请各位大虾帮忙。
[解决办法]
android.R.id.mytext改为R.id.mytext
android.R.id.mybutton改为R.id.mybutton
[解决办法]
把Android去掉! 像楼上的那样!
[解决办法]
嗯,楼上说的都对。你的包导入错了。 
TextView myText = (TextView)this.findViewById(android.R.id.mytext);
应该改为
 TextView myText = (TextView)this.findViewById(R.id.mytext);
把你上面导入的删掉。然后重新导入。
[解决办法]
1楼正解 http://dalvik.co.cc

热点排行