0

I'm writing a simple Android App using AIDE (Android IDE). I gave one of my layout elements an ID, but when I try to access the element using findViewById(), I get an error tht says: "Unknown member 'id' of 'com.mycompany.mailscomunes.R'. I haven't seen this error outside of AIDE.

This is the Java code:

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);

    }
}

And this is the relevant XML:

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/one"/>
Larpee
  • 800
  • 1
  • 7
  • 18
  • 1
    Can you preview the layout? Hit the little landscape icon on the right of the toolbar that shows in the layout XML editor when you dismiss the keyboard. If there's no problem, it'll automatically rebuild the resources, too, and the error in `MainActivity` should eventually be resolved, though it may take a bit. – Mike M. May 22 '17 at 21:06
  • 1
    @MikeM. Thank you very much, I did what you said and my program fixed itself. Would you mind explaining why? – Larpee May 22 '17 at 21:38
  • AIDE's basic app template doesn't have any IDs defined in resources anywhere, or assigned to `View`s in the example layout, so there is no `R.id` class until you add some IDs, and rebuild the resources. Previewing a layout causes that rebuild, so each time you change something in layouts, it'll make things easier if you just immediately preview. Just FYI, the imports mentioned below weren't the cause of the issue, but that is how the free version of AIDE will organize them by default, so if you change them manually, be warned that "Fix imports" and "Organize Imports" will revert them to that. – Mike M. May 22 '17 at 21:56
  • Actually, now that I think about, I believe it's technically the file save that causes the rebuild, but a preview will trigger a save, so same basic outcome. – Mike M. May 22 '17 at 22:02
  • 1
    @MikeM. Thank you very much for your explanation, I understand now. – Larpee May 22 '17 at 22:03

3 Answers3

1

Remove these lines:

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

You're literally importing the entire Android framework, which includes layout files, which has ID's in them.
And you're not including your own ID file (called "R.java").

So remove those two lines, and include this one:

import com.mycompany.mailscomunes.R;
Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • I don't think the problem was with my imports, but your answer still helped me make my program more efficient, so thank you very much. I wasn't aware that I was importing things I didn't need. – Larpee May 22 '17 at 21:39
0

The root of an activity xml should be of Layout type.

<?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>
Francis Nduba Numbi
  • 2,499
  • 1
  • 11
  • 22
0

Goto your project and delete the build folder and do a rebuild. I had the same problem and is fixed

DiLDoST
  • 335
  • 3
  • 12