#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Create a simple Layout


    Hi,

    I'm newbie in Android and I can't create a simple LinearLayout. I have the following code in several files:

    Principal.xml

    Code:
        <?xml version="1.0" encoding="utf-8"?>  
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            android:orientation="vertical" >  
              
            <TextView   
                android:text="Nombre"  
                />  
              
            <EditText   
                android:width="100px"  
                />  
              
        </LinearLayout>
    strings.xml

    Code:
        <resources>  
          
         <string name="app_name">Interfaces</string>  
         <string name="hello_world">Hello world!</string>  
         <string name="menu_settings">Settings</string>  
         <string name="title_activity_layout">LayoutActivity</string>  
         <string name="Nombre">Nombre</string>  
        
        </resources>
    LayoutActivity.java

    Code:
        
        package example.interfaces;  
          
        import android.os.Bundle;  
        import android.app.Activity;  
        import android.view.Menu;  
          
        public class LayoutActivity extends Activity {  
          
            @Override  
            public void onCreate(Bundle savedInstanceState) {  
                super.onCreate(savedInstanceState);  
                setContentView(R.layout.principal); //error  
            }  
          
            @Override  
            public boolean onCreateOptionsMenu(Menu menu) {  
            getMenuInflater().inflate(R.menu.principal, menu); //error  
                return true;  
            }  
        }
    Errors are in the last file, when I try to change the main activity's name. I have errors in lines setContentView and getMenuInflater. The error is:

    principal cannot be resolved or is not a field.

    I save principal.xml file in res/layout and string.xml in res/values folder. LayoutActivity.java is in src/example.interfaces folder.

    How can I fix this error?

    I'm working with Eclipse Indigo and API Level 16.

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    heya!!


    Originally Posted by axmug
    Hi,

    I'm newbie in Android and I can't create a simple LinearLayout. I have the following code in several files:

    Principal.xml

    Code:
        <?xml version="1.0" encoding="utf-8"?>  
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            android:orientation="vertical" >  
              
            <TextView   
                android:text="Nombre"  
                />  
              
            <EditText   
                android:width="100px"  
                />  
              
        </LinearLayout>
    strings.xml

    Code:
        <resources>  
          
         <string name="app_name">Interfaces</string>  
         <string name="hello_world">Hello world!</string>  
         <string name="menu_settings">Settings</string>  
         <string name="title_activity_layout">LayoutActivity</string>  
         <string name="Nombre">Nombre</string>  
        
        </resources>
    LayoutActivity.java

    Code:
        
        package example.interfaces;  
          
        import android.os.Bundle;  
        import android.app.Activity;  
        import android.view.Menu;  
          
        public class LayoutActivity extends Activity {  
          
            @Override  
            public void onCreate(Bundle savedInstanceState) {  
                super.onCreate(savedInstanceState);  
                setContentView(R.layout.principal); //error  
            }  
          
            @Override  
            public boolean onCreateOptionsMenu(Menu menu) {  
            getMenuInflater().inflate(R.menu.principal, menu); //error  
                return true;  
            }  
        }
    Errors are in the last file, when I try to change the main activity's name. I have errors in lines setContentView and getMenuInflater. The error is:

    principal cannot be resolved or is not a field.

    I save principal.xml file in res/layout and string.xml in res/values folder. LayoutActivity.java is in src/example.interfaces folder.

    How can I fix this error?

    I'm working with Eclipse Indigo and API Level 16.

    Thanks.
    java is case sensitive as u mentioned ur filename "Principal.xml"
    u have written
    setContentView(R.layout.principal); //error
    ie wrong case so it is throwing error

    for your textview and edit text u must set an id
    eg:android:id="@+id/mytext" same for edittext

    else you cannot call textview and edittext in your java file
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by swapy
    java is case sensitive as u mentioned ur filename "Principal.xml"
    u have written
    setContentView(R.layout.principal); //error
    ie wrong case so it is throwing error

    for your textview and edit text u must set an id
    eg:android:id="@+id/mytext" same for edittext

    else you cannot call textview and edittext in your java file
    Thanks for replying. I changed some code but I still have the same error in the line

    Code:
    getMenuInflater().inflate(R.menu.principal, menu);
    in the LayoutActivity.java file.

    I changed the line
    Code:
    android:text="Nombre"
    for
    Code:
    android:text="@string/Nombre"
    in principal.xml file.

    What I don't understand is why it's necessary to type a text property in a EditText.

    Thanks.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    java is case sensitive as u mentioned ur filename "Principal.xml"
    u have written

    getMenuInflater().inflate(R.menu.principal, menu); u should have changed this to
    getMenuInflater().inflate(R.menu.Principal, menu);
    bro your problem is with word Principal ..... you did same mistake again !!!

    Edittext means eg: username password fields where user have to enter data in given textboxes so there text property isnt necessary
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Create a Layout


    Originally Posted by swapy
    java is case sensitive as u mentioned ur filename "Principal.xml"
    u have written

    getMenuInflater().inflate(R.menu.principal, menu); u should have changed this to
    getMenuInflater().inflate(R.menu.Principal, menu);
    bro your problem is with word Principal ..... you did same mistake again !!!

    Edittext means eg: username password fields where user have to enter data in given textboxes so there text property isnt necessary
    Yes. I made a mistake typing the the name of the file Principal.xml. Instead of Principal.xml the right name of the file is principal.xml. So I have only one error and it is in the line getMenuInflater().inflate(R.menu.principal, menu);

    The error is principal cannot be resolved or is not a field

    I don't know how to fix it.

    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo