theJava
theJava

Reputation: 15034

Layouts in Android [ id or field cannot be resolved]

public class Page1 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.welcome);
         final Button button = (Button) findViewById(R.id.welcome);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent myIntent = null;
                        myIntent = new Intent(view.getContext(), Page1.class);
                    startActivity(myIntent);
                }
            });          
    }

}

I want to load contents from another XML file named welcome.xml, but i do get an error welcome cannot be resolved or is not a field

This Page1.java class is next screen of my Android Application.

My Welcome.xml

<Button android:text="@+id/Button01" android:id="@+id/welcome" 
android:layout_width="wrap_content" android:layout_height="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android">
</Button>

Upvotes: 0

Views: 1319

Answers (6)

yeradis
yeradis

Reputation: 5347

Your Welcome.xml is not complete, should be something like this :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:padding="3dip"
    android:orientation="vertical">

    <Button android:text="@+id/Button01" android:id="@+id/welcome" 
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        xmlns:android="http://schemas.android.com/apk/res/android">
    </Button>

</LinearLayout>

Also, if your are still having problemas, try to clean your project so the R.java get updated with new id values like welcome id (R.id.welcome),because if R.java does not contain welcome id, you will get errors like that.

Upvotes: 0

Naresh
Naresh

Reputation: 305

The Welcome.xml contains Button with id welcome which is not an layout to setContentView Views can be List, relative, absolute table etc .. in which you can add a button.

And also check for the Case in filename and specified R.layout.*

Sample xml file with linearlayout and a button. Save it as welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/linearlayoutmain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<Button
android:id="@+id/ButtonWelcome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
>
</Button>

</LinearLayout>

in Your Code

public class Page1 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.welcome);
         final Button button = (Button) findViewById(R.id.ButtonWelcome);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent myIntent = null;

                **//You have called Page1.class again which is the name of this class //again** 
                        myIntent = new Intent(view.getContext(), Page1.class);
                    startActivity(myIntent);
                }
            });          
    }

}

Create another activity similarly and the call that class in the intent marked bold.

Upvotes: 0

Aman Aalam
Aman Aalam

Reputation: 11251

Is this all what your welcome.xml has?

Your button isn't under a layout. thus, the layout file itself will be throwing out exceptions. secondly, android:text is not correct. the entry you have made there, should be under android:id

and it shouldn't be:

final Button button = (Button) findViewById(R.id.welcome);

but:

final Button button = (Button) findViewById(R.id.Button01);

Upvotes: 1

chikka.anddev
chikka.anddev

Reputation: 9629

firend you are making silly mistake:see this



public class Page1 extends Activity {

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.welcome);
     final Button button = (Button) findViewById(**R.id.Button01**);//use id of button here not layout name

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = null;
                    myIntent = new Intent(view.getContext(), Page1.class);
                startActivity(myIntent);
            }
        });          
}

}

Upvotes: 1

Pedro Loureiro
Pedro Loureiro

Reputation: 11514

It should be working.

If you don't set the handler, do you see the button in the screen?

Is the file actually named «*W*elcome.xml»? Try to remove the capital letter (rename it to welcome.xml). Then do a clean, rebuild and check if it works now...

Upvotes: 1

Ravi Vyas
Ravi Vyas

Reputation: 12375

Could you paste your complete xml file and log? my first guess is you have a case issue , your layout file is named "Welcome" and you have setContentView to "welcome" . Also dont have same names for layouts and controls , it will get confusing.

Upvotes: 1

Related Questions