6 Replies - 1180 Views - Last Post: 20 June 2012 - 08:48 PM

#1 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Screen Rotation

Posted 20 June 2012 - 07:20 PM

I just started android programming today using a book called Hello, Android and I have hit a wall.

The book told me to create a folder called layout-land and a main.xml file in it. Then gave some code to put in there and in the layout/main.xml files. Now I am having errors in the Sudoku.java file (I guess you could call it the activity file?...) saying that in the code
setContentView(R.layout.main);
, layout is underlined red and says that "layout cannot be resolved or is not a field" also in R.java , I am getting errors at

Syntax error on tokens, delete these tokens
public static final class =id{ 


Illegal modifier for the class string; only public, abstract & final are permitted
public static final class attr { 

public static final class color { 

public static final class drawable { 

public static final class id { 

public static final class layout { 

public static final class string { 


please help :(

I don't know if this is helpful but..
The code in layout-land/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="15dip" >
    
    <LinearLayout 
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center"
        android:paddingLeft="20dip"
        android:paddingRight="20dip">
        
        <TextView 
            android:text="@string/main_title"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dip"
            android:textSize="24.5sp" />
        
        <TableLayout 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:stretchColumns="*">
            <TableRow>
                <Button
                    android:id="@+=id/continue_button"
                    android:text="@string/continue_label" />
                <Button
                    android:id="@+id/new_button"
                    android:text="@string/new_game_label"/>
            </TableRow>
            
            <TableRow>
                <Button
                    android:id="@+id/about_button"
                    android:text="@string/about_label" />
                <Button
                    android:id="@+id/exit_button"
                    android:text="@string/exit_label" />
            </TableRow>
            
        </TableLayout>
    </LinearLayout>
</LinearLayout>



code in layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="30dip">

    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/main_title"
        android:layout_gravity="center"
        android:layout_marginBottom="25dip"
        android:textSize="24.5sp"/>

    <Button 
        android:id="@+id/continue_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/continue_label"/>
    
    <Button
        android:id="@+id/new_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/new_game_label"/>
    
    <Button
        android:id="@+id/about_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/about_label"/>
    
    <Button
        android:id="@+id/exit_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit_label"/>
	</LinearLayout>
</LinearLayout>



Is This A Good Question/Topic? 0
  • +

Replies To: Screen Rotation

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Screen Rotation

Posted 20 June 2012 - 07:30 PM

Are you using eclipse to make your apps?
One thing to make sure is that you importing the R.java from your project and not the android.R.java!
The reason you have an error here: public static final class =id{
is because you cannot name a class starting with a =!
As for the Illegal modifier for the class string; only public, abstract & final are permitted error your going to have to post more of your code for use to see the error.
Was This Post Helpful? 0
  • +
  • -

#3 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: Screen Rotation

Posted 20 June 2012 - 07:41 PM

Yes, I am using eclipse.
How do I import from R.java and not android.R.java?

The R.java class is auto generated and you can't edit and save it because it auto generates back... So what can I fix that?

And here is all the code that I have done...

colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background">#3500ffff</color>
</resources>


strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, Sudoku!</string>
    <string name="app_name">Sudoku</string>
    <string name="main_title">Android Sudoku</string>
    <string name="continue_label">Continue</string>
    <string name="new_game_label">New Game</string>
    <string name="about_label">About</string>
    <string name="exit_label">Exit</string>

</resources>


R.java
/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package org.example.sudoku;

public final class R {
    public static final class =id {
        public static final int continue_button=0x7f070000;
    }
    public static final class attr {
    }
    public static final class color {
        public static final int background=0x7f040000;
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int about_button=0x7f060002;
        public static final int continue_button=0x7f060000;
        public static final int exit_button=0x7f060003;
        public static final int new_button=0x7f060001;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int about_label=0x7f050005;
        public static final int app_name=0x7f050001;
        public static final int continue_label=0x7f050003;
        public static final int exit_label=0x7f050006;
        public static final int hello=0x7f050000;
        public static final int main_title=0x7f050002;
        public static final int new_game_label=0x7f050004;
    }
}



Sudoku.java
package org.example.sudoku;

import android.app.Activity;
import android.os.Bundle;

public class Sudoku extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="30dip">

    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/main_title"
        android:layout_gravity="center"
        android:layout_marginBottom="25dip"
        android:textSize="24.5sp"/>

    <Button 
        android:id="@+id/continue_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/continue_label"/>
    
    <Button
        android:id="@+id/new_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/new_game_label"/>
    
    <Button
        android:id="@+id/about_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/about_label"/>
    
    <Button
        android:id="@+id/exit_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit_label"/>
	</LinearLayout>
</LinearLayout>


layout-land/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="15dip" >
    
    <LinearLayout 
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center"
        android:paddingLeft="20dip"
        android:paddingRight="20dip">
        
        <TextView 
            android:text="@string/main_title"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dip"
            android:textSize="24.5sp" />
        
        <TableLayout 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:stretchColumns="*">
            <TableRow>
                <Button
                    android:id="@+=id/continue_button"
                    android:text="@string/continue_label" />
                <Button
                    android:id="@+id/new_button"
                    android:text="@string/new_game_label"/>
            </TableRow>
            
            <TableRow>
                <Button
                    android:id="@+id/about_button"
                    android:text="@string/about_label" />
                <Button
                    android:id="@+id/exit_button"
                    android:text="@string/exit_label" />
            </TableRow>
            
        </TableLayout>
    </LinearLayout>
</LinearLayout>

Was This Post Helpful? 0
  • +
  • -

#4 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Screen Rotation

Posted 20 June 2012 - 07:55 PM

Ok well I believe all your errors are because of =id in your R.java! You should try to clean your project because that usually fixes all errors associated with R.java! To clean your project click Project at the top of the eclipse window and select clean... From there make sure you project is checked and hit ok. It should now clean your project. Check your R.java file and the =id should be gone but if not post back and we'll see if we can't help you figure out why its still not working.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Screen Rotation

Posted 20 June 2012 - 08:09 PM

I remember having this problem when I worked through "Hello, Android". It's because Eclipse kept importing android.R at the top of my files. You have to just change it to yourpackage.R instead. Also, Try deleting R.java from your project completely and do a clean + compile as H3R3T1C suggested. Everything looks fine though.

However, I found that for your book, the developer site is well done and constantly monitored. You can check the forums, errata, and source code too to check.
http://forums.pragprog.com/forums/152
http://pragprog.com/...s/eband3/errata
http://pragprog.com/...nd3/source_code
Was This Post Helpful? 0
  • +
  • -

#6 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: Screen Rotation

Posted 20 June 2012 - 08:37 PM

@H3R3T1C
I tried cleaning the project before posting.. and I just tried again, it didn't work...

@Dogstopper

Quote

you just have to change it to yourpackage.R instead


like this?
setContentView(org.example.sudoku.R.layout.main);

or
setContentView(Sudoku.R.layout.main);

(the one above causes an error saying that R can not be resolved to a field or is not a field.

Also, I am looking over the source code for it now... The forum link, I already posted on and I am waiting for a response and the other link didn't help...

This post has been edited by Benzoate: 20 June 2012 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#7 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Screen Rotation

Posted 20 June 2012 - 08:48 PM

OK I found your error!
In your main.xml you have:
<Button
android:id="@+=id/continue_button"
android:text="@string/continue_label" />

Notice have you have "@+=id/continue_button". That should only be "@+id/continue_button" so there should not be a = in there!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1