Beginner Android why is "id" is "Unknown member" with R.id? - android

I'm sure this is something dead simple as it's my first android app with code (the hello world example I did was just assigning a value to a string in XML). My problem is when trying to get the reference for my button in my variable then the id as in R.id is not defined?
The compiler error is in a comment in the code below where it occurs:
package com.geeksonhugs.simplecalc;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
private Button AddButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddButton = (Button)findViewById(R.id.btnAdd);
//"Unknown member 'id' of 'com.geeksonhugs.simplecalc.R'"
//AddButton.setOnClickListener(this);
}
}
XML layout file:
<?xml version='1.0' encoding='utf-8' ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LayoutRoot"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txtFirstNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/strFirstNum" />
<EditText
android:id="#+id/edtFirstNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
<TextView
android:id="#+id/txtSecondNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/strSecondNum" />
<EditText
android:id="#+id/edtSecondNum"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
<Button
android:id="#+id/btnAdd"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/strAdd"
android:gravity="center" />
<TextView
android:id="#+id/txtResult"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="" />
</LinearLayout>

I resolved the issue by deleting the bin folder and recompiling using the AIDE App.

The R class is code-generated for you by the Android build tools when you build your project. This means there are four possibilities:
You have not tried building the project yet, in which case, try that.
Eclipse doesn't think it needs to build the project for some reason -- try Project > Clean from the Eclipse menu (only relevant if you are using Eclipse, of course).
There is some bug in your manifest or one of your resources that is preventing R from being built. There should be Eclipse error indicators for this.
You do not have an android:id attribute in your layout, and therefore there is no R.id available.

Please make sure the xml file you pasted is called main.xml and under layout folder.
And try to generate the R file again.
That may help.

For those using AIDE having issues referencing Id, try deleting the "gen folder" located in the bin folder and recompile. The problem is the R.java class did not create a constructor called Id. So deleting the gen folder containing the R.java class and recompiling solves this issue.

You must add id to a component in the XML file. Like this:
android:id="#+id/buton1"
Now can you click mainActivity.java and write code "R.id." you a see hint to write "buton1" and no error of "id"

Related

Automatically Generating java code for xml layout in Eclipse

I have got an idea to get rid of some coding when we do initializion of views.
When we create an xml layout in android. At that movement same name class is created with UpperCase Letters with a dialogue with permission. And as i created views with ids. It should create Views and initialize them automatically.
for e.g
close.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" >
<Button
android:id="#+id/closeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/close_img" />
<TextView
android:id="#+id/closeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing Text" />
</LinearLayout>
And automatically generated java code should be.
public class Close extends Activity
{
TextView CloseText;
Button CloseBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CloseText = (TextView)findViewById(R.id.closeText);
CloseBtn = (Button)findViewById(R.id.closeBtn);
}
}
I can read xml and do the other stuff as I explained. But how could i add this module to eclipse and How could i create a service which work in Background all the time in eclipse.
Suggest me how to add this module(Plugin) to eclipse and i am going to use JAXB to generate Java Objects from XML document. Is there any better option.
Here in this website i find that just paste the xml and you will get your java code ready for activity class.
i had attached the link

fiewViewById not finding my button (Eclipse)

I downloaded latest ADT from the main website and it came with Eclipse. So, I set everything up fine > Start new Android project. Set my settings, and the Activity to "Blank Activity", click finish.
Now, I go to the visual editor and add a button and some text. The problem is, is that "findViewById" is not detecting the button for some reason. Yes, I "Cleaned" the project and all that..
Here's the xml portion:
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:layout_marginTop="37dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/editText1"
android:layout_marginTop="52dp"
android:text="#string/button" />
</RelativeLayout>
Here's my source code:
package com.example.tcp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ibtn = (Button) findViewById(R.id.button1); // Error
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Did you actually save the activity_main.xml? View ID's are not accessible to the rest of the project until you save the layout you're working on.
I copied and pasted your code straight into a blank project and it worked fine after saving.
Its just the basic error so clean your project and build again if its not work then restart eclipse and start once again it will display your button.In the same way you followed
don't even import R.java it will show you error..
so clean and build your project
In your TextView you have
android:layout_below="#+id/button1"
Delete the plus so you have
android:layout_below="#id/button1"
Also it's better when you move the code of the textview below the code of the button. The Relative layout will position it where it's supposed to be anyway. But this way you don't link to an id that doesn't exist yet.
You declare a id with the + in the id. So in your case you declare it twice.
(Also for furure reference when you post something on StackOverflow with an error. Please mention the error in all cases. It makes finding the error and helping you a lot easier.)
I looked at the R file and it was empty before I "ran" the project. After clicking "run" it added the id fields in the file. Seems to be working fine now. I guess it was actually a problem with the activity_main.xml not saving. Thanks again

Unable to get the reference to spinner in the onCreate statement

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
Spinner spinner=(Spinner)findViewById(R.id.spinner1);
XML
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android";
xmlns:tools="schemas.android.com/tools";
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Suppose you have proper XML layout. Try to clean your project (aka recreate gen).
Project-> Clean -> Ok. After, refresh your project to recreate R.java. Might help you.
The other reason might wrong path for R.java file
Sometimes you don't see any import of R at all.
So add it manually: import [yourpackage].R
For example: import com.demo.agnt.R
or replace old one: for example import android.R to import [yourpackage].R

Create Layout and test the changes on the emulator failed

i have the problem, that if i create a new layout and change the layout in the graphical layout for example add a button or a textfield to the layout, that the application canĀ“t be installed on the emulated device. I get the unusable error "Failed to install *.apk on device 'emulator-5556': timeout
Launch canceled!".
If i remove the button or textfield then the emulator runs the application. I tested many things. I removed Eclipse and the Android SDK. Installed different Versions of the Eclipse Software (Classic, JAVAEE) and then installed the android SDK every time new. Nothing changed. I deleted the .android folder in my Windows Userprofile. But nothing changed.
What is wrong.
The Code of my layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
the code of the Activity:
package de.androidpraxis.HelloAndroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
}
}
i hope someone can help me. Thanks in advance.
greetings
edit:
I found something out. I have a project with a standard layout and an folder for land-layout. If i delete the land-layout folder with the xml files in that the application runs in the emulator. How could i do it right without deleting the other folder?
> I get the unusable error "Failed to install *.apk on device 'emulator-5556':
> timeout Launch
this issue has nothing to do with your layout design.
this is a adb issue.
try executing this command from any terminal or console window
prior to installing your application
path/to/your/android-sdk/platform-tools/.adb kill-server
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
You can try this new layout xml file. And, you should notice that, this file name "layout.xml" and put it in layout folder. But, I don't think this is a problem, because when you don't do this will see error when compile

Android problem

Hi iam working on android getting the following problem for my login but i didn't got my mistake... The error notice is the application has been terminated unexpectedly
my code is
package layout.program;
import android.app.Activity;
import android.os.Bundle;
public class Tablelayout extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tablelayout);
}
}
tablelayout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content" android:text="TextView" android:id="#+id/textView1" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content"></TextView>
<EditText android:id="#+id/editText1" android:layout_height="wrap_content" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</EditText>
</TableRow>
</TableLayout>
Try Cleaning your Project..
By Selecting the Project and then go to top project option and then select clean.
and then select OK..
Sometimes Changes are not made in R.java...Thats y this error comes
Your code is working fine at my end..please look in your code if there is anything which is causing the exception..Try to debug with debugger....
There's nothing wrong with your code or xml, so it has to be an error in your manifest.
As this application is just a stub, why not do this:
Create a new Android project, call it anything you like.
Call the activity 'TableLayoutActivity'
Put your tablelayout.xml into the layout folder.
Change the automatically generated code in TablelayoutActivity.java to set the content view from setContentView(R.layout.main) to setContentView(R.layout.tablelayout)
This should work, then have a look at the manifest in the new project, to see where you went wrong in your original project.
change the class name and try again,also check if the activity name mentioned in the android manifest is correct.

Categories

Resources