Android Studio cannot find symbol variable - java

I installed Android Studio about two days ago, I also installed the most updated version of Java and JDK 1.8 (I think it was Java SE 8u101). I am able to edit code and I'm following a tutorial from the official Android Studio website. (Here: https://developer.android.com/training/basics/firstapp/building-ui.html)
However, upon hitting run I've gotten the two following errors:
Error:(24, 57) error: cannot find symbol variable activity_display_message
Code for this:
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
Error:(33, 25) error: cannot find symbol variable EXTRA_MESSAGE
Code for this:
intent.putExtra(EXTRA_MESSAGE, message);
What might I be doing wrong? The emulator pops up and I can get to other apps on the phone but I just can't get my app to load. Should I have installed Java SE 8u102 instead?
I also got a second error message:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.
Any help is appreciated.
Below is my code for the first xml file, "activity_main.xml":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
Below is my code for "activity_display_message.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: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="com.example.appno1.DisplayMessageActivity">
</RelativeLayout>

Add this to your MainActivity:
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
You might have missed this line, which can be found in the "Build an Intent" part.
Refer this for more, from the original page.

I think you might lose the last note in the tutorial.
so you just add android:id attribute to the activity_display_message.xml file and it will be ok!oh actually you should add into the layout element which is the valid location.

My advice is, try to clean the project and build it again. If you want to access newly added resources you need to build the project. Cleaning will help to remove all previously autogenerated resources from project and building will help to create them again + new ones.

You should add EXTRA_MESSAGE as static final, like:
private static final String EXTRA_MESSAGE = "message";

Regarding this error,
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
check that the file res > layout > activity_display_message.xml as the id specified as android:id="#+id/activity_display_message"

Related

How to fix this > attribute app:constraintBottom_toBottomOf not found

Android newbie here.
I am building and AR app and now I'm planning to add a User Interface. I am trying to add a constraint layout to app:layout_constraintBottom_toBottomOf="parent", however Im getting the error that this constraint was not found
These are my codes and error messages:
All the lines of code I add in my activity_ux.xml file that begin with "app"
e.g. app:layout_constraintBottom_toTopOf do not seem to work, I just showed you an example here with one of them just to make it easier to debug I think.
I tried searching online, one of the solutions was that I should install "ConstraintLayout for Android" and "Solver for ConstraintLayout" in my SDK Tools, but I already had this installed.
<FrameLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.google.ar.sceneform.Overlei.Overlei">
<fragment android:name="com.google.ar.sceneform.ux.ArFragment"
android:id="#+id/ux_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/gallery_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
>
</LinearLayout>
</FrameLayout>
I expected this to just work, I don't know where I can start looking if an in-built function(such as this) does not work.
Thanks for the suggestions guys. I have managed to deal with the error by adding this line
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
into my build.gradle(module.app) under dependencies
Again, thanks for everything
You should use app:layout_constraintBottom_toBottomOf only in ConstraintLayout. You can try to use inside of FrameLayout
then android:gravity = "bottom" in your LinearLayout.
Wrap you FrameLayout in a ConstraintLayout or change the FrameLayout to ConstraintLayout

Android app tutorial content.xml and activity.xml files have different code to tutorial

I'm working through the android tutorial at
http://developer.android.com/training/basics/firstapp/building-ui.html
I've created the project as stated in the tutorial however the code that it says is supposed to be in my activity.xml file is instead in my content.xml file its as if they have swapped?
Code in activity 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=".MyActivity">
<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" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_my" />
<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"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Code in content 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"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView android:text="Hello World!" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
The tutorial is telling me to change the relative layout block to linear layout (which it says should be in the activity xml, but for me its in content xml)
So when I replace the code in activity xml with what the tutorial has it gives me errors because I removed the code that was there previously.
OR
if I edit the code in content xml and run the app I can't see the changes because activity is not updated, and I cant move the linear layout code to activity xml because it gives me "multiple root tags" error.
Can someone show me a solution that will allow me to continue with the tutorial, because if I edit the java code or something like that I get problems with later stages in the tutorial.
Thanks
Instead of working on the activity.xml try editing the content.xml to reflect the LinearLayout change (do add the orientation value as shown).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView android:text="Hello World!" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Do also go through on the Android Getting Started Resources. They will come in handy
You just have to keep in mind that these .xml files represent the layout part, the UI.
To put it simply, a 'screen' in Android is composed of 2 files : the layout (the .xml file), and its associated .java file.
Here, we can see that your "content" is included in your "activity".
It's quite the same thing as putting directly the content of "content.xml" into "activity.xml".
What is in the "OnCreate()" method of you MyActivity.java ?
Have you tried replacing the "include" in "activity.xml" by the actual content of "content.xml", not forgetting to remove "root specific" values (xmlns:android, xmlns:tools, xmlns:app and tools:context)?
As fernaMuruthi said, try following the Android Getting Started Resources. Also, try not to use the graphic designer : it sometimes generate crappy code. instead, Type you UI pseudo-xml directly.
I was having the same issue. Never really worked with XML, and my Java is rusty. I ended up searching online a bunch and this is what I found out.
The .xml files are used to determine your layouts for your activity. They can be used in conjunction with one another or separately. This means using content.xml and main_activity.xml don't really matter. However because the tutorial is not very clear about this, it makes it hard for newcomers to figure out a work around.
The solution is to understand what the MainActivity.java file is doing with your .xml files. Mike James at i-programmer explains this fairly well...(http://www.i-programmer.info/programming/android/5914-android-adventures-activity-and-ui.html)
There is a method called onCreate() that controls which layout is being used by MainActivity.java. Here you will see the auto generated function setContentView() this is the main issue with determining what is being displayed in the app.
There is also a bunch of other generated code that is used to get more data from main_activity.xml. If you look at the code you will see what needs to be removed.
Hope this helps. Please up vote if it did :)
So, if you follow the tutorial to a tee, i.e. make your activity_my.xml file the exact same as what they are expecting, then you will see the edit text view and the button view as depicted.
It seems that quite a bit has been added to the blank activity template since the creation of this tutorial, making the tutorial quite obscure.
After looking around for a while and trying multiple solutions I finally realized that the toolbar created in activity_my.xml was obscuring the text/button created in content_my.xml.
My solution was to just pad the linear layout.
android:paddingTop="55dp"
But then again I'm on day #2 in the Android/Java world...

Attempting to Implement a Thread for a Game Loop

I am a novice learning Android development and the next step I must take is to be capable to understanding threads to be able to implement a game loop.
I have taken knowledge from google and StackOverflow and snippets from examples to come up with a lump of code that seems to not be working:
MainActivity.java
GameSetup.java
GameView.java
activity_main.xml
game_setup.xml
MainActivity.java is a simply title screen with a play button. This seems to work fine and will link to the next activity. However, I am getting a crash whenever I attempt to load up the next activity. My guestimations based on the error lead me to believe that threads have issues with RelativeActivity, but I'm not so sure on what the issue may be exactly, I am rather confused.
Here is the error I am getting thrown, I am having issues trying to decipher the issue with it.
Thanks.
R.id.layout is a RelativeLayout but you are trying to cast it to GameView, that's causing your crash.
Edit:
To add your GameView to the RelativeLayout you can do
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.your.package.GameView
android:id="#+id/my_game_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ship1" />
</RelativeLayout>
and retrive it with
GameView myGameView = (GameView) findViewById(R.id.my_game_view);
Hmm, from the code you have written so far, I can explain the reason for the ClassCastException.
You are declaring a RelativeLayout in both XML files.
Both RelativeLayouts use the same android:id "#+id/layout"
In GameView.java you try to find a view by specifying the id
m_game = (GameView) findViewById(R.id.layout);
IMO you are retrieving one of the RelativeLayouts and then try to cast it to a GameView. As this is not possible, Java throws the exception.

findViewById Error cant find id in XML

I cant find out why my findViewById is not linking to the XML file and it is driving me nuts. The is is in the XML file but it is still not Finding it in the java MainActivity file.
here is my XML
<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: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=".MainActivity" >
<EditText
android:id="#+id/editCentimeters"
android:layout_width="wrap_content"
android:layout_height="27sp"
android:layout_alignLeft="#+id/editInches"
android:layout_alignTop="#+id/textCentimeters"
android:ems="5" >
<Button
android:id="#+id/buttonConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editInches"
android:layout_centerHorizontal="true"
android:layout_marginTop="98dp"
android:text="#string/convert" />
And here is my Java
EditText etCentimeters = (EditText) findViewById(R.id.editCentimeters);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
I was facing the same issue. Just comment out the line which is throwing the error & BUILD the project once. Intellisense (ctrl + space) then will show you the IDs.
You need to take care of 2 things here:-
EditText etCentimeters = (EditText) findViewById(R.id.editCentimeters);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
Firstly, the above code should be present in the onCreate() method.
And next, you need to place that code snippet below this line in the onCreate() method, not before it.(I'm assuming that the setContentView is already present, if not, add that)
setContentView(R.layout.main);
use setContentView(R.layout.main); at the onCreate of your Activity
You can refresh project.
And also check ur setContentView(R.layout.YOURLAYOUT_NAME) in Your activity.
Make sure that u are using ur code in activity.
Just clean & build in order to find the ID.
Clean & build didn't work for me. I had to restart Android Studio.

problems with R - ANDROID

when i create a layout called " main.xml ":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/button" />
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
when i go to the main to create a button
" button = (Button) findViewById(R.id.button); "
i have an error , i cant find the button in R.id
and same for the spinner
the id dont exist in R.id
help me plz
You're probably importing the wrong version of R. There are two versions which exist: the static R class which exists in android.R and then there is also yourPackageName.R. If you import the former none of your Ids will be found.
Make sure you don't have any files, pictures, etc in your drawable or assets folder with capital letters, spaces, or dashes. Just use plane old lowercase. Also, try to clean your project. Go to project > clean. Then restart eclipse. Hope this helps. Also eclipse won't reconize any changes in XML files without saving first. Make sure to save after you add a button, ect.
Try to verify that there is no errors in your XML files,than try to clean the project.

Categories