AIDE 中的 ID 出现问题



我正在使用AIDE(Android IDE(编写一个简单的Android应用程序。我给了我的一个布局元素一个 ID,但是当我尝试使用 findViewById() 访问该元素时,我收到一个错误,说:"'com.mycompany.mailscomunes.R'的未知成员'id'。我在AIDE之外还没有看到这个错误。

这是 Java 代码:

package com.mycompany.mailscomunes;
import android.app.*;
import android.os.*;
import android.content.Intent;
import android.provider.ContactsContract;
public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.one);
    }
}

这是相关的 XML:

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/one"/>

删除以下行:

import android.app.*;
import android.os.*;

您实际上是在导入整个Android框架,其中包括布局文件,其中有ID。
而且您没有包含自己的 ID 文件(称为"R.java"(。

因此,删除这两行,并包括这一行:

import com.mycompany.mailscomunes.R;

活动 xml 的根目录应为布局类型。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Text"/>
</LinearLayout>

转到您的项目并删除构建文件夹并执行重建。我遇到了同样的问题并已修复

最新更新