views:

109

answers:

5

How can I had to class layout in R another XML file?

It should be automatic as I had new resources to res, but it's not. Someone knows what I did wrong?

I open an activity and now I want to open another activity that will work with another XML example. I have menu and main.xml. Now I want to go for another activity called gamescreen using this method:

newGameButton.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                Intent i = = new Intent(this, gameScreen.class);
                startActivity(i);
            }

}

I want to move to another "page" to another activity called gameScreen which should be associated to the XML called gameScreen.xml.

But in its onCreate:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gameScreen);

}

and gameScreen is not a member of the R file.

The main problem is that when I add resource to layout for example newxmlfile.xml the R file doesn't do any work and stays in this state.

public static final class layout {
    public static final int main=0x7f030000;
}where it should have been:
public static final class layout {
    public static final int main=0x7f030000;
    public static final int newxmlfile=0x7f031000;
}
A: 

If your file does exists in the res/layout folder, and probably you are in a different package, you have to import the reference to R, and remove any reference to android.R

import com.mypkg.R;

Also don't forget to add class gameScreen to your manifest file as activity.

EDIT

You should recreate the XML file with the wizard, and make sure everything is fine there. Compare against to some samples located in the SDK's install /samples/ directory.

Pentium10
where should this import b?import com.mypkgname.R?...
yoavstr
in the top of the class, where your other imports are. You can Edit your original question, and post more code if you need additional help.
Pentium10
it doesnt work it's not recognised as a layout resource.maybe something involve new settings somewhere?
yoavstr
A: 

You have got to add the new activity to the AndroidManifest.xml:

...
<activity 
    android:name=".gameScreen">
</activity>

Edit:
This is assuming gameScreen is the Class name of the new activity. Also, from what I understand, gameScreen is an XML file located in res/layout.
The filename of gameScreen.xml must be lowercase: gamescreen.xml.

Itsik
i tried this one already ... it doesnt work
yoavstr
A: 

Use aapt(exe is present in platforms/<platform>/tools/aapt) command to generate the R file :

aapt package -f -M ${manifest.file} -F ${packaged.resource.file} -I ${path.to.android-jar.library} -S ${android-resource-directory} [-m -J ${folder.to.output.the.R.java}]

It will generate a new R.java file. If there is any error then it will show you that as well.

Karan
A: 

In Eclipse: menu-project-clean-choose your project.

If not, check gameScreen.xml is under the res layout folder.

beralee
A: 

I found the answer. Somehow Eclipse did

 import Android.R;

which caused ambiguity on the R variable.

yoavstr