错误:无法解析activity_main或没有字段 [英] error :activity_main can not be resolved or not a field

查看:73
本文介绍了错误:无法解析activity_main或没有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

得到上述错误.遵循了这个

但是没有找到解决方案.检查了我的资源文件,所有但没有用的地方为我提供了一个可行的解决方案.在这里放置activity_main.xml 和

but didn't find solution.checked my resource file and all but no use suggest me sutable solution for this.here placing the activity_main.xml and

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="87dp"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_centerVertical="true"
    android:text="Button2" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button2"
    android:layout_below="@+id/button2"
    android:layout_marginTop="52dp"
    android:text="Button" />

</RelativeLayout>

推荐答案

更改

setContentView(R.id.activity_main);

setContentView(R.layout.activity_main);

从文档中 >

从布局资源设置活动内容.资源将被放大,将所有顶级视图添加到活动中.

Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.

如果您在gen文件夹中的R.java中查找,则存在不同的类. R.id表示您给定Viewid. R.layout用于创建的layout文件. setContentView either takes a布局or a View . Not an id of a View`.

If you look in R.java in the gen folder, there are different classes. R.id is for an id that you give a certain View. R.layout is for a layout file that you create.setContentVieweither takes alayoutor aView. Not anidof aView`.

这篇关于错误:无法解析activity_main或没有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆