I am making my first testing program with Android Studio.
The program has two activities, you insert one text in the first activity, push one button, and then the text is showed on another activity.
I am following one tutorial on YT but I find two errors:
cannot find symbol variable and I canĀ“t find the solution.
I have searched here in SO but no answer works. I have tried:
Clean Project and rebuild Project.
I have no "import android.R".
I have used
import mypackagename.R
but it tells me "unused import statement".I have changed the ID names and removed every capital letters, since I have read it causes troubles.
I have performed a Gradle sync (via Tools > Android > Sync Project with Gradle Files).
Codes are:
MainActivity.java:
package pack1.p1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button boton1;
EditText texto1;
boton1=(Button)findViewById(R.layout.idboton1);
texto1=(EditText)findViewById(R.layout.edittext1);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="pack1.p1.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PRIMERA VENTANA"
android:id="@+id/textview1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:layout_below="@+id/textview1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_marginTop="39dp"
android:id="@+id/edittext1" />
<Button
android:text="Abrir ventana nueva"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edittext1"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:id="@+id/idboton1" />
</RelativeLayout>
activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="pack1.p1.Main2Activity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main2" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
content_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="pack1.p1.Main2Activity"
tools:showIn="@layout/activity_main2">
</RelativeLayout>
And finally, the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
This is where I have looked for:
Android Studio: Gradle: error: cannot find symbol variable
In MainActivity.java,
R.id.idboton1
instead ofR.layout.idboton1
. Same with edittext1