有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓错误:无法解析活动_main或不是字段

获得上述错误followed this

但没有找到解决办法。检查了我的资源文件,但没有使用建议我可用于此的解决方案。这里是活动的主视图。xml 和problem

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

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

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

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

</RelativeLayout>

共 (1) 个答案

  1. # 1 楼答案

    改变

    setContentView(R.id.activity_main);
    

    setContentView(R.layout.activity_main);
    

    From the Docs代表setContentView()

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

    如果查看R.java文件夹中的gen,则会发现不同的类R.id是指id你给出的ViewR.layout是指layout文件,您可以创建.setContentView either takes a布局or a视图. Not anid of a视图`