supsup
supsup

Reputation: 33

Id can't be resolved Android

Android studio is still not recognizing my text view and buttons despite all the changes I made in other questions I saw. Please help

public void onButtonClick(View v)
{
    int a,c;
    EditText e1 = (EditText)findViewById(android.R.id.num);

    TextView t1= (TextView)findViewById(android.R.id.num1);
    a=Integer.parseInt(e1.getText().toString());


    c= a*a;
    t1.setText(Integer.toString(c));



}

The XML file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
>

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/num"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="73dp"
    android:text="Enter Text" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click me"
    android:id="@+id/but"
    android:layout_below="@+id/num"
    android:layout_toRightOf="@+id/textView"
    android:layout_toEndOf="@+id/textView"
    android:layout_marginTop="44dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView2"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/but"
    android:layout_alignEnd="@+id/but"
    android:layout_marginBottom="50dp" />


 </RelativeLayout>

Please help me I am a newbie so I don't get what errors are occuring I did the same some time early and it worked.

Upvotes: 3

Views: 104

Answers (3)

Android Dev
Android Dev

Reputation: 433

try this ,it may be like this

private Button add;
@Override
    protected void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       add= (Button) findViewById(R.id.button1);
        add.setOnClickListener(this);

}
@Override
    public void onClick(View v)
    {

    switch (v.getId()) {

case R.id.But:   // this is your xml id for button

     //Your code 

        break;
}
}

Upvotes: 0

Anup Dasari
Anup Dasari

Reputation: 488

id is used to uniquely define components.

So for the first time we use @+id/name. And after that we just use @id/name

So change your layout to this:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"           
     android:layout_width="match_parent"
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     android:paddingBottom="@dimen/activity_vertical_margin"    
     tools:context=".MainActivity"
     >

    <TextView android:text="@string/hello_world"    
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

   <EditText
   android:layout_height="wrap_content"
   android:inputType="number"
   android:ems="10"
   android:id="@+id/num"
   android:layout_below="@id/textView" //changed (removed + sign)
   android:layout_alignParentLeft="true"
   android:layout_alignParentStart="true"
   android:layout_marginTop="73dp"
   android:text="Enter Text" />

   <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="click me"
   android:id="@+id/but"
   android:layout_below="@id/num" //changed (removed + sign)
   android:layout_toRightOf="@id/textView" //changed (removed + sign)
   android:layout_toEndOf="@id/textView" //changed (removed + sign)
   android:layout_marginTop="44dp" />

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="New Text"
   android:id="@+id/textView2"
   android:layout_alignParentBottom="true"
   android:layout_alignRight="@id/but" //changed (removed + sign)
   android:layout_alignEnd="@id/but" //changed (removed + sign)
   android:layout_marginBottom="50dp" />



   </RelativeLayout>

Upvotes: 1

Sushrita
Sushrita

Reputation: 725

Try this:

  public void onButtonClick(View v)
{
int a,c;
EditText e1 = (EditText)findViewById(R.id.num);

TextView t1= (TextView)findViewById(R.id.num1);
a=Integer.parseInt(e1.getText().toString());


c= a*a;
t1.setText(Integer.toString(c));



}

Upvotes: 0

Related Questions