TheoAndroid
Lurker
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
rientation="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 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
rientation="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!!!
}
}

) Can you do that for us, please, TheoAndroid?
