Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find symbol errors in Android Studio Tutorial?

Tags:

java

android

https://developer.android.com/training/basics/firstapp/building-ui.html#Weight

I am doing the tutorial above and to get it to build I had to comment out these lines inside MainActivity.java inside src folder (This code is in the MainActivity class inside OnCreate()).

//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//
//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });

If I do not comment out these lines, I get 'cannot find' errors and can't build, example:

error: cannot find symbol variable toolbar

Can someone explain in plain english why this is happening and how I can fix it? I have tried various import.R fixes that people found to combat Eclipse randomly adding that, but they don't work. My imports:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

I am using Android Studio like the tutorial says and get the same error on a clean install. Is it my automatically generated imports? Is the tutorial wrong or incompatible with the latest Android Studio? Are my build settings wrong?

like image 830
MrPickles7 Avatar asked Dec 09 '22 01:12

MrPickles7


1 Answers

The R class is auto-generated based on what is in your *.xml files, whether they are strings, dimensions, colors, ids within layouts, etc.

When you are calling code like this:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

You are saying, "Set the toolbar variable to the widget with an id of toolbar".

In an xml, say you had three textviews like this:

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_view_1" />
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_view_2" />
<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_view_3"  />

When I build the project, my R.java file will then be updated to have references in java code to those ids that I defined in xml, like this:

    public static final int text_view_1=0x7f0c0066;
    public static final int text_view_2=0x7f0c0067;
    public static final int text_view_3=0x7f0c0068;

To explain it at a very basic level, the R.java file is java code that is generated to reference xml elements within actual java code.

I am guessing that when you created a project in Android Studio, it came with the MainActivity.java class. That's where that code is coming from that you posted above. The part right above that in the default MainActivity.java class is

setContentView(R.layout.activity_main);

This is setting the activity's view to the xml layout file defined in activity_main.xml. Inside of this layout is the following

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout 
        android:layout_height="wrap_content"
        android:layout_width="match_parent" 
        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"         
                android:theme="@style/AppTheme.AppBarOverlay"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton 
        android:id="@+id/fab"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom" 
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

My guess is that perhaps your activity_main.xml does not contain this definition for the Toolbar and the FloatingActionButton widget.

If you want to really start from scratch so that you can follow the tutorial you linked, you should delete the MainActivity.java and just go create your own.

like image 159
jyanks Avatar answered Dec 11 '22 09:12

jyanks