Id cannot be resolved android
Android studio still doesn't recognize my text view and buttons despite all the changes I've made to other questions I've seen. 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));
}
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'm a newbie so I don't understand what's going on with the errors. I've done the same thing before and it worked.
+3
source to share
3 answers
id
used to uniquely identify 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>
+1
source to share
try this it could be like
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;
}
}
0
source to share