我正在使用AIDE (Android IDE)编写一个简单的Android应用程序。我给我的一个布局元素一个ID,但是当我尝试使用findViewById()访问这个元素时,我得到了一个错误,它说:“未知成员'id‘of 'com.mycompany.mailscomunes.R’。我在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"/>发布于 2017-05-23 05:31:23
删除以下行:
import android.app.*;
import android.os.*;您实际上是在导入整个Android框架,其中包括布局文件,其中包含ID。
并且您没有包含您自己的ID文件(称为"R.java")。
因此,删除这两行,并包括这一行:
import com.mycompany.mailscomunes.R;发布于 2017-08-23 22:42:06
活动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>发布于 2020-08-27 07:03:04
转到您的项目,删除build文件夹并进行重建。我也有同样的问题,现在已经解决了。
https://stackoverflow.com/questions/44121994
复制相似问题