• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps findViewById() problem

Hi everyone,

I am struggling with the following code. Basically,I want to create buttons with declared ids as "bAdd", and "bSub"( "android:id="@+id/bAdd" and "android:id="@+id/bSub"). Somehow the methods "findViewById(R.id.bAdd) and findViewById(R.id.bSub) generate errors, as they can not find the variable s I declared in the XML file. What is the problem? Some stated that "import android.R" should be removed. I didn't find this package in the java file..Maybe it is somewhere else, and I am missing it. I would be grateful if someone could help me out.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height=fill_parent" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your result is 0"
android:textSize="25dp"
android:layout_gravity="center"
android:id="@+id/tvDispay"
></TextView>

<Button

android:layout_width="250"
android:layout_height="wrap_content"
android:text="Add One"
android:textSize="25dp"
android:layout_gravity="center"
android:id="@+id/bAdd"

></Button>

<Button

android:layout_width="250"
android:layout_height="wrap_content"
android:text="Substract One"
android:textSize="25dp"
android:layout_gravity="center"
android:id="@+id/bSub"

></Button>
</LinearLayout>

Here is the Java file MainActivity.java
package com.example.simplecalculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;

public class MainActivity extends Activity {

int counter;
Button add,sub;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add=(Button)findViewById(R.id.bAdd);//ERROR!!!
sub=(Button)findViewById(R.id.bSub);//ERROR!!!
}

}
 
I moved this to the Application Development forum so our other coders can see it.:)

Also, if you use code tags it makes it easier for those folks to help (so they say).

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height=fill_parent" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your result is 0"
android:textSize="25dp"
android:layout_gravity="center"
android:id="@+id/tvDispay"
></TextView>

<Button 

android:layout_width="250"
android:layout_height="wrap_content"
android:text="Add One"
android:textSize="25dp"
android:layout_gravity="center"
android:id="@+id/bAdd"

></Button>

<Button 

android:layout_width="250"
android:layout_height="wrap_content"
android:text="Substract One"
android:textSize="25dp"
android:layout_gravity="center"
android:id="@+id/bSub"

></Button>
</LinearLayout>

Here is the Java file MainActivity.java
package com.example.simplecalculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;

public class MainActivity extends Activity {

int counter;
Button add,sub;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add=(Button)findViewById(R.id.bAdd);//ERROR!!!
sub=(Button)findViewById(R.id.bSub);//ERROR!!!
}

}
 
Thank you for doing this for me...I hope someone out there will be able to help me..

No problem, I'm not a coder, but there are a few real smart dudes that hang out here. I'm sure they will at least have a look. Good luck with your app.
 
This happens a lot for me. The thing that usually fixes it when I code is making sure both the XML files and java file are both saved. Eclipse doesn't recognize updates until the newly changed code has been saved.

If eclipse still doesn't recognized it then make sure the names are typed the same.
If you still have a problem copy and paste the errors on here so we can figure it out for ya.
 
So there is nothing wrong with my code which good. I have written it from the scratch, but I still get the same problem..This is what I get.

"bAdd can not be resolved or is not a field"..The same error message appears in the next line too. I could have fixed these errors by declaring constants?? in the R class, but I don't believe that is a wise think to do,as this class should remain unmodified.

I think that Eclipse is not that efficient as I thought. Can I develop Android applications using Netbeans? I am much more familiar with this package.

Thanks
Theo
 
Just throwing this out there, when placing code in the CODE tags, you need to re-copy the code from the project, not from the unformatted post. This way, you keep your formatting (the whole point to using CODE tags ;) ) Can you do that for us, please, TheoAndroid?
 
Just throwing this out there, when placing code in the CODE tags, you need to re-copy the code from the project, not from the unformatted post. This way, you keep your formatting (the whole point to using CODE tags ;) ) Can you do that for us, please, TheoAndroid?

Sorry jon, that was me and I didn't know it made a difference.:rolleyes:
 
The code looks fine. In eclipse the xml throws an error. Integer not allowed on layout_width


Code:
<Button
		android:id="@+id/bSub"
		android:layout_width="250"
		android:layout_height="wrap_content"
		android:layout_gravity="center"
		android:text="Substract One"
		android:textSize="25dp" >
	</Button>
 
Back
Top Bottom