Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IMPORTANT: Remember to add your package name before you copy and paste this java code into the JustJava app's MainActivity.java file.
/**
* IMPORTANT: Make sure you are using the correct package name.
* This example uses the package name:
* package com.example.android.justjava
* If you get an error when copying this code into Android studio, update it to match teh package name found
* in the project's AndroidManifest.xml file.
**/
package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
display(1);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
}
@DatYardieQ

This comment has been minimized.

Copy link

DatYardieQ commented Jul 29, 2016

Hello i have copied and pasted this code and i am getting 6 errors come up on my screen

@tian-chaiyaporn

This comment has been minimized.

Copy link

tian-chaiyaporn commented Jul 30, 2016

I think in line 4, you need to use: import android.support.v7.app.AppCompatActivity;

not: import android.support.v7.app.ActionBarActivity;

@Basem-Hany

This comment has been minimized.

Copy link

Basem-Hany commented Jul 30, 2016

i also have these errors
screenshot 553
screenshot 554

@Basem-Hany

This comment has been minimized.

Copy link

Basem-Hany commented Jul 30, 2016

screenshot 555

@luckyman20

This comment has been minimized.

Copy link

luckyman20 commented Jul 30, 2016

what to do i havealso these errors

@ledi001

This comment has been minimized.

Copy link

ledi001 commented Jul 30, 2016

Hello,
i also have problems with this code. I don't get an error, but if i run the example on my phone, it don't show me the 1 when i press the button. It looks like the method is not executed by a button press.

/** * This method is called when the order button is clicked. */ public void submitOrder(View view) { display(1); }

@ledi001

This comment has been minimized.

Copy link

ledi001 commented Jul 30, 2016

Now, i startet my cellphone new and it works with this code:

`
package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    display(1);
}

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

}
`

@paololmanzini

This comment has been minimized.

Copy link

paololmanzini commented Jul 30, 2016

This (at the end) worked!

package HERE PUT YOUR FOLDER NAME (THE NAME OF THE FIRST FOLDER IN JAVA DIRECTORY);
/*ex: package it.web_mi.paolojustjava.justjava; */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(
      R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }
@garrettpen98

This comment has been minimized.

Copy link

garrettpen98 commented Aug 1, 2016

Hey guys, I think you forgot to label your text view in the XML file. try adding this line to the quantity text view. android:id="@+id/quantity_text_view" Good luck!

@Tarbo

This comment has been minimized.

Copy link

Tarbo commented Aug 1, 2016

For it to work, ensure that you add this in the Button XML attribute:
android:onClick="submitOrder"

@SaurabhK24

This comment has been minimized.

Copy link

SaurabhK24 commented Aug 1, 2016

After using all the suggestions given it still says R package does not exist. What do I do???

@jiajie313

This comment has been minimized.

Copy link

jiajie313 commented Aug 2, 2016

make sure edit the package name to <com.example.android.justjava> while initializing your project

@crypteya

This comment has been minimized.

Copy link

crypteya commented Aug 2, 2016

I think in line 4, you need to use: import android.support.v7.app.AppCompatActivity;
not: import android.support.v7.app.ActionBarActivity;

As mentioned in the second comment in this gist, there is a typo in line 4. After that is edited (as above) and the id/onclick changes have been made, everything works.

@WDM1

This comment has been minimized.

Copy link

WDM1 commented Aug 2, 2016

A lot of great suggestions here but none corrected the R. error I continued to receive.
Finally found this https://www.youtube.com/watch?v=kLHB-40y72s which helped.
The generated code was not using the correct package name. Simply went in to the manifests folder to correct the issue.
Now everything seems to be working well.

@websus

This comment has been minimized.

Copy link

websus commented Aug 3, 2016

WORKED
Added these:
"For it to work, ensure that you add this in the Button XML attribute:
android:onClick="submitOrder""

"adding this line to the quantity text view. android:id="@+id/quantity_text_view" Good luck!"

AND

Change this:
public class MainActivity extends AppCompatActivity {
TO:
public class MainActivity extends ActionBarActivity {

@diegomenfer

This comment has been minimized.

Copy link

diegomenfer commented Aug 4, 2016

Well, after reading all the answers and reading all the errors on Android Studio, i found the solution, see it attached and explained a little further.

Before copying the "MainActivity.java", make sure you have add all the "TextViews" and "Buttons" and all the attributes on your "activity_main".

It would look like this:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="Quantity"
        android:textAllCaps="true" />

    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textColor="#000000"
        android:textSize="16sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Order"
        android:textAllCaps="true"
        android:onClick="submitOrder" />

After this is time to copy the provided code by Udacity on your "MainActivity.java", but before that lets take a look what is the problem on the original code provide.

First of All

The "package name" could be tricking.

On AndroidStudio (Windows) the Default "package name" adds your "Windows" UserName to the "package name"

Example:

If your Windows User Name is "UdacityTrollUs", the default "package name" will be, "com.example.udacitytrollus.justjava" and THIS make all the things going crazy when you copy paste the code.

What to do?

Make sure to read the "app/manifests/AndroidManifest.xml" and look for those lines

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.udacitytrollus.justjava">

As you can see my "package name" is "com.example.udacitytrollus.justjava" just make sure to copy your package name over the one provided by Udacity.

Now just 1 more thing.
In the provided code by Udacity, as already said, there's an typo, the line 4 says:
"import android.support.v7.app.ActionBarActivity;" and the line 11 says:
public class MainActivity extends AppCompatActivity {

As you can see "ActionBarActivity" is different of "AppCompactActivity"

To fix this, look your "MainActivity.java", copy line 11 before you paste the code given by Udacity, after this, delete line 11 and paste your line 11, and now all thins will work.

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        display(1);
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(
                R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }
}
@sahilmittal1104

This comment has been minimized.

Copy link

sahilmittal1104 commented Aug 4, 2016

can anybody just explain why are we using
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);

just new with java cant understand

@shreyadhatwalia

This comment has been minimized.

Copy link

shreyadhatwalia commented Aug 4, 2016

error:package R does not exist..is shown. what do i do?

@vijayreddie86

This comment has been minimized.

Copy link

vijayreddie86 commented Aug 5, 2016

don't do anything just check the folder manifests in studio and observe the line package="com.example.username.justjava" replace it in the java code with package com.example.Android.justjava; which is the first line in MainActivity.java.
AND

Change this:
public class MainActivity extends AppCompatActivity {
TO:
public class MainActivity extends ActionBarActivity {

@ghost

This comment has been minimized.

Copy link

ghost commented Aug 5, 2016

As pointed out by the guys, the error is in the git code provided, just replace the bad code line in your java file by the good one (of course after you wrote the xml code described on the tutorial)

Bad:
import android.support.v7.app.ActionBarActivity
Good:
import android.support.v7.app.AppCompatActivity;

@joelc1225

This comment has been minimized.

Copy link

joelc1225 commented Aug 7, 2016

After changing "public class MainActivity extends AppCompatActivity"
TO
"public class MainActivity extends ActionBarActivity", everything worked for me.

hope this helps

@Basem-Hany

This comment has been minimized.

Copy link

Basem-Hany commented Aug 8, 2016

ya me to
ActionBarActivity instead of AppCompatActivity

@kittipatkampa

This comment has been minimized.

Copy link

kittipatkampa commented Aug 8, 2016

I follow these steps and it works:

  1. change import android.support.v7.app.ActionBarActivity; to import android.support.v7.app.AppCompatActivity; and keep import android.support.v7.app.ActionBarActivity; the same as it is.
  2. The error at quantity_text_view can be resolved by adding android:id="@+id/quantity_text_view" in the quantity (order number) TextView in the activity_main.xml.

Then I test it by plugging in the real phone to my computer and click the green play button and it works.

@PeddiManiShankar

This comment has been minimized.

Copy link

PeddiManiShankar commented Aug 8, 2016

Here is the code which works (Note: SubmitOrder should be in camel case)
java snap

this is screenshot for below code

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void SubmitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(
      R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }
@twhetzel

This comment has been minimized.

Copy link

twhetzel commented Aug 9, 2016

If you're getting the issue with R not being found, trying cleaning the project by going to Build -> Clean Project If that doesn't work, also try closing and re-opening Android Studio. If you copy-paste the code snippet and are not using the same package name in the code above, things get wonky.

A correct file is here:
https://gist.github.com/twhetzel/40d5a3307da03baf1fa37428cec73e5d

@udacityandroid

This comment has been minimized.

Copy link
Owner Author

udacityandroid commented Aug 11, 2016

Thanks to all the students who helped with relentless troubleshooting and apologies for the errors in the code.

I've updated the file above with these corrections:

  1. Line 4 changed from
    import android.support.v7.app. ActionBarActivity to
    import android.support.v7.app.AppCompatActivity;
  2. Line 30 and 31 consolidated into one line:
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
  3. Added a reminder to add the package name specific to the user's project.
    The package name can be found in the AndroidManifest.xml of your project:
    screen shot 2016-08-11 at 11 21 22 am
@ghost

This comment has been minimized.

Copy link

ghost commented Aug 12, 2016

I tried to run and got the following error message;

screen shot 2016-08-12 at 15 06 23

See fatal exception below from Android Monitor;

08-12 10:17:33.388 3320-3320/com.example.android.justjava E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.justjava, PID: 3320
java.lang.IllegalStateException: Could not find method submitOrder(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'

@kseeley44

This comment has been minimized.

Copy link

kseeley44 commented Aug 13, 2016

This new code still does not seem to be working for me. Anyone else?

@kseeley44

This comment has been minimized.

Copy link

kseeley44 commented Aug 13, 2016

image

image

@jonbrinkley

This comment has been minimized.

Copy link

jonbrinkley commented Aug 14, 2016

I was getting the red R error and realized that I needed to paste code below this line of code in the default program:

package app.com.example.android.justjava;

Then it worked.

@davidsony33

This comment has been minimized.

Copy link

davidsony33 commented Aug 14, 2016

I'm still getting the errors.

@abhi472

This comment has been minimized.

Copy link

abhi472 commented Aug 15, 2016

@kseeley44
you're using wrong package name
you need to add your own package name there which will be available in manifest file
for eg
screen shot 2016-08-15 at 9 43 42 am
screen shot 2016-08-15 at 9 44 06 am
screen shot 2016-08-15 at 9 46 27 am

@abhi472

This comment has been minimized.

Copy link

abhi472 commented Aug 15, 2016

your package name will be available in the manifest files as this line
screen shot 2016-08-15 at 9 49 06 am

after you've changed the package name from appropriate places go to Build->Rebuild Project

this will solve the problem!!

@elmaystro

This comment has been minimized.

Copy link

elmaystro commented Aug 16, 2016

For others (new comers) who have an error : usually it is because of two things :

1- in activity_main.xml :
Naming wrong whether in TextView : android:id="@+id/quantity_text_view"
or in button : android:onClick="submitOrder"

2- MainActivity.java :
press cntrl+enter so that the ide resolves the error for you after pasting the code of course (the error is just because of the package name so the android ide will resolve it automatically for you :) )

@ssanator

This comment has been minimized.

Copy link

ssanator commented Aug 16, 2016

x sign
PLEASE CAN ANYONE TELL WHY THE ANDROID SIGN IS MARKED X

@fokusdev

This comment has been minimized.

Copy link

fokusdev commented Aug 17, 2016

@abhi472 -thank You for screenshots, and that last comment about "Rebuilding". Now everything works fine!

@g2ummo

This comment has been minimized.

Copy link

g2ummo commented Aug 19, 2016

Make sure you paste the code underneath the first line

@tejasvx

This comment has been minimized.

Copy link

tejasvx commented Aug 20, 2016

package com.example.android.justjava; /**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
    *
  • package com.example.android.justjava;
    */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.android.justjava.R;

import static com.example.android.justjava.R.id.textView2;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(textView2);
      quantityTextView.setText("" + number);
      }
      }

/**

  • copy the above code
  • just remember that you change
  • textView2
  • in
  • TextView quantityTextView = (TextView) findViewById(textView2);
  • to
  • quantity_text_view.
  • hope this will help
    */
@ghost

This comment has been minimized.

Copy link

ghost commented Aug 20, 2016

Following all of the help above, things still don't work. Holy shit - if it's this bad this early on, what kind of BS are we going to be dealing with deeper in?

@davidamacylee

This comment has been minimized.

Copy link

davidamacylee commented Aug 20, 2016

To find the fix for this code jump to the end of the comment. To read my long and arduous journey of finding the solution to this problem, you can read my rant below.

It took me a while to figure this one out, but the code as of the recent revision doesn't work because it's missing the package. This is because the recent update reads:

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • package com.example.android.justjava;
    */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

... etc.

In the above snippet of the gist the package name "package com.example.android.justjava;" is preceded by an asterisk* which turns it from code into a comment and therefore is not read by Android Studio as valid Java code. I found that by copying and pasting the package name "package com.example.android.justjava;" to the line above "import android.os.Bundle;" it fixed the issue by supplying the MainActivity.java file with a valid package name code. What I mean is that I modified the gist snippet above to read like this:

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • package com.example.android.justjava;
    */

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

... etc.

Look closely for the differences between the two snippets; the first one merely has the package name "package com.example.android.justjava" in the comments preceded by an asterisk* whereas the second snippet then subsequently specifies the package as "com.example.android.justjava" by including the line of code which reads "package com.example.android.justjava;" after the comments section(as denoted by asterisks***) and before the four lines of code which begin with "import".

... which is really stupid, because if you followed the very explicit directions of the instructor in the video, you would have already set up your project with the specifications which would give it the default package name "com.example.android.justjava" and if they had included that package name in the code gist then we as students could just copy and paste the gist over, no problem. To make it work I had to compare the default java code supplied by Android Studio with the gist supplied by the instructor, which, to be honest, taught me a useful bit about programming and be-asterisked comments in Java, but confused and frustrated the gosh darn heck out of me. (Are we allowed to swear at the things which make us angry in this forum?) Granted, if you actually read the comments denoted by the asterisks***, it says something about inserting your package below, and then lists "package com.example.android.justjava;" as an example of what they used, but like I said if you followed the step by step instructions in the video this would have been the case for you anyway and the fix for any issue you might have encountered then would have been to be a good student and just go back through the video to miss that bit you glazed over while you were picking your nose or fetching a screaming teapot off of the stove or closing your eyes for a two-second micronap. As it is we haven't learned anything about packages or Java code packages or in general except what we were forced to learn for ourselves by illogical instructor edits.

Long story short, add the line "package com.example.android.justjava;" after the comment section denoted by asterisks*** and before the four lines of code which begin with "import".

@YrboD

This comment has been minimized.

Copy link

YrboD commented Aug 22, 2016

edit:
Made it to work with these steps:

  1. I created a new project from scratch with the same names as in the video.
  2. I copied over the code from top of this site into MainActivity.java
  3. At the beggining of the MainActivity.java file I have added following code:
    package com.example.android.justjava;

and it worked from first try.

When earlier I tried with project named differently I did not manage to make it work. must be something a little deeper.

@Ksancha

This comment has been minimized.

Copy link

Ksancha commented Aug 23, 2016

Problem with shutting down SOLVED
Line 27 Change your code from submitOrder to SubmitOrder If you made OnClick comand="SubmitOrder"

Check that you use the ID of the element that you assigned before (i.e quantity_text_view)

Also check that you have the right name-path on the line 5 (Its should be the first folder in your java folder)

@parasjain-12

This comment has been minimized.

Copy link

parasjain-12 commented Aug 24, 2016

GUYS when i run this code it shows "Could not identify launch activity: Default Activity not found
Error while Launching activity" plz help me # ASAP

@Anel-89

This comment has been minimized.

Copy link

Anel-89 commented Aug 24, 2016

CalveinX, hi, Thanks so much for your post and detailed instruction, it did help in my case with that "non-existing R package"!

@arjava

This comment has been minimized.

Copy link

arjava commented Aug 25, 2016

the result is

genyjav

@divyanshujhawar

This comment has been minimized.

Copy link

divyanshujhawar commented Aug 25, 2016

While copying the code just remember to enter the correct .xml file name in the java code
setContentView(R.layout.activity_main);
instead of main write the file name which you have chosen.

@eriverz

This comment has been minimized.

Copy link

eriverz commented Aug 27, 2016

I keep getting that same R. and MainActivity error, Already tried every single recommendation posted here, and even started the project from scratch twice. Does anybody still have the same issue? any more suggestions??? Appreciate your help!

@SidDas20

This comment has been minimized.

Copy link

SidDas20 commented Aug 27, 2016

image
I'm getting this error. Can somebody help?
My activity class does exist. I am able to access the app from the emulator by clicking the icon but it is not working from android studio itself which it used to do before.

@sarahfriedli

This comment has been minimized.

Copy link

sarahfriedli commented Aug 28, 2016

ok guys, i had the same R problem like everyone else, unfortunately, i did note take the instruction from the java file when i saw it which explains why we all have that probelm:

so here is the solution:

in the original file this is what was written: ( notice how i only take the first few lines?) and also notice the comment that says add you r package name, that is what you are suppose to do. package name can be found from manifest file.

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
    *
  • package com.example.android.justjava;
    */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

solution: NOTICE HOW I HAVE NOW ADDED THE PACKAGE BEFORE THE IMPORT ANDOIRD...

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.

  • This is the package name our example uses:
    *

  • package com.example.android.justjava;
    */
    package com.example.android.justjava;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;

    now it works perfectly.

@ahmedmujtaba

This comment has been minimized.

Copy link

ahmedmujtaba commented Aug 29, 2016

Paste the given code after "package com.example.android.justjava;" line.
It will fix the "Package R does not exist error"

@willsonSp

This comment has been minimized.

Copy link

willsonSp commented Aug 29, 2016

The only mistake is that the line "package com.example.android.justjava;" It is commented. So, to correct you only need to remove the comment from this line.

@w4rlock999

This comment has been minimized.

Copy link

w4rlock999 commented Sep 1, 2016

remove the comment on package com.example.android.justjava; guys

@karsoumik27

This comment has been minimized.

Copy link

karsoumik27 commented Sep 4, 2016

posted on 04-Sep-2016

If you are getting the red R error then just add this line at the top below the comment

package com.example.android.justjava;

or replace the "com.example.android.justjava;" with your packagename
image

@Sparsh10

This comment has been minimized.

Copy link

Sparsh10 commented Sep 10, 2016

Just Copy Paste The Lines Blow As the Code They Gave Is A Little Wrong.

Also,make you have put

android:onClick="submitOrder"

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
    *
  • package com.example.android.justjava;
    */

package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }
@Her0Zer0

This comment has been minimized.

Copy link

Her0Zer0 commented Sep 12, 2016

package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

The package text should be at the top like so ^^. This should get the script working. If you are like me, I was getting carried away with the syntax, but the above is correctly written enough for copy/paste.

@AANDERQUIKK

This comment has been minimized.

Copy link

AANDERQUIKK commented Sep 12, 2016

Ow! Thanks a lot @karsoumik27!

@Puxley

This comment has been minimized.

Copy link

Puxley commented Sep 16, 2016

After reading the comment at the beginning of the file, the solution that worked for me was as simple as copying the text in line 5 to a new line at the beginning of the file.

As shown below:

image

P.S. Assuming that the tutorial has been followed exactly (I'm surprised this wasn't included in the first place).

@ghost

This comment has been minimized.

Copy link

ghost commented Sep 17, 2016

just remove the comments line and it will work , edit your folder name if you changed it to something else

@Sanclub

This comment has been minimized.

Copy link

Sanclub commented Sep 20, 2016

For these error codes, follow the instructions below!

Error while executing: am start -n "com.example.android.justjava/MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.android.justjava/MainActivity }
Error type 3
Error: Activity class {com.example.android.justjava/MainActivity} does not exist.

Error while Launching activity

Here is the solution worked for me... (Most probably it will work for you)

  1. Add this line on top of all codes in the MainActivity.java tab:

package com.example.android.justjava;

  1. Then, see the Tab called "MainActivity" in the picture? Change it to "App"

toolbar

Whola!

@kamdroid3

This comment has been minimized.

Copy link

kamdroid3 commented Sep 20, 2016

So after all of this finally worked for me,
added the package com.example.android.justjava; just before import android.os.Bundle;
and modified the R.layout.activity_main TO BE com.example.android.justjava.R.layout.activity_main or as you wrote your app name.

here is the full MainActivity.java:

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
    *
  • package com.example.android.justjava;
    */
    package com.example.android.justjava;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.example.android.justjava.R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(com.example.android.justjava.R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }

and here is the full activity.main.xml:-

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="quantity"
    android:textAllCaps="true"
    android:layout_marginBottom="16dp"/>

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:textColor="#000000"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Order"
    android:layout_marginTop="16dp"
    android:onClick="submitOrder"
    />

it worked with me and here is the screenshot
screenshot_2016-09-20-16-19-51

thanks all of you, actually I am very disappointed about Udacity, this is my first course in android an I am supposed to follow the instructions to get learned. but I spent 4 hours searching for the solution they supposed to provide as experts, anyway this helped me alot.

@manmeet-22

This comment has been minimized.

Copy link

manmeet-22 commented Sep 21, 2016

The code given by udacity is correct all you need to do is-

  1. Click on manifest in the left tab. Inside it is "AndroidManifest.xml". Open It!!
  2. In the top of manifest you will something like-

package="com.androidexample.justjava"

  1. Copy that line and then paste it in the top of "MainActivity,java".
  2. Then Edit that line according to this -

package com.androidexample.justjava;

@vicky14

This comment has been minimized.

Copy link

vicky14 commented Sep 23, 2016

Just add the below code in MainActivity.java

package com.example.android.justjava;

@suresh72

This comment has been minimized.

Copy link

suresh72 commented Sep 23, 2016

i do commit to same error please do resolve over here

Uploading Screenshot (112).png…

please do solve for this problem

@raghavjadia

This comment has been minimized.

Copy link

raghavjadia commented Sep 24, 2016

I have copied and pasted the code properly,added the proper package name and also typed submitOrder correctly.
But when i run the app the value of the second TextView does not get incremented to 1, is there some problem with the code itself?

@madhavgovind

This comment has been minimized.

Copy link

madhavgovind commented Sep 24, 2016

i really tried a lot, applied all d suggestions, but this error still exist. completely fed up

Help----- help ---- help---- helpppppppppppppppppppppppppppppppppppppppp

:app:compileDebugJavaWithJavac
:app:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
C:\Users\shyam\AndroidStudioProjects\JustJava\app\src\main\java\com\example\android\justjava\MainActivity.java
Error:(14, 25) error: package R does not exist
Error:(28, 62) error: package R does not exist
:app:compileDebugJavaWithJavac FAILED
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

image

@madhavgovind

This comment has been minimized.

Copy link

madhavgovind commented Sep 24, 2016

image

@suresh72

This comment has been minimized.

Copy link

suresh72 commented Sep 24, 2016

screenshot 113
please do some correction the error is solving

@suresh72

This comment has been minimized.

Copy link

suresh72 commented Sep 24, 2016

image
please do help sir my course is incomplete please do the solution sir

@ikkev

This comment has been minimized.

Copy link

ikkev commented Sep 25, 2016

@suresh72:
Add the following line at the top of your code:
package com.example.android.justjava;

Also you seem to have forgotten the give your TextView the id '@+id/quantity_text_view'. Add the following attribute to your TextView that is displaying the 0: android:id="@+id/quantity_text_view"

@AlexanderErassery

This comment has been minimized.

Copy link

AlexanderErassery commented Sep 29, 2016

Guys uncomment the following line in the code to fix the R error (Remove it from the comments section),

package com.example.android.justjava;

@vgujevski

This comment has been minimized.

Copy link

vgujevski commented Sep 29, 2016

Hi, i can run the app all fine, but in the Java code R is still highlighted red in both places, and says "cant resolve R sybol"

@JHTWTPI

This comment has been minimized.

Copy link

JHTWTPI commented Oct 2, 2016

This one works!
feel free to copy and paste.

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

/**

  • This method is called when the order button is clicked.
    */
    public void submitOrder(View view) {
    display(1);
    }

/**

  • This method displays the given quantity value on the screen.
    */
    private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
    R.id.quantity_text_view);
    quantityTextView.setText("" + number);
    }
    }
@SamuelIdakwo

This comment has been minimized.

Copy link

SamuelIdakwo commented Oct 4, 2016

Please help me!!! the code seems to work now but when I run it on my emulator, it is always crashing. Please what do I do?
github

@ahmeda37

This comment has been minimized.

Copy link

ahmeda37 commented Oct 6, 2016

package com.example.android.justjava; is not meant to be commented out. To fix the code simply close the comment before it like so.

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • */
    package com.example.android.justjava;
@manivannanragavan

This comment has been minimized.

Copy link

manivannanragavan commented Oct 10, 2016

image

How to solve this error

@Fazoli

This comment has been minimized.

Copy link

Fazoli commented Oct 10, 2016

I have the same problem above. Could any good soul past here the right code?

@KaQing

This comment has been minimized.

Copy link

KaQing commented Oct 14, 2016

has anyone an idea? If I try to run the app on my huawei I get a black screen which disappears immediatly... tried to google the problem (run as command, see below) but didn't find anything.
run as

@KaQing

This comment has been minimized.

Copy link

KaQing commented Oct 14, 2016

@Fazoli and manninvannanragavan

If you delete The R and play a bit around (type R back in and so on, play a bit with the text) you can select R with an autolink to the project. Just delete R and type R back in. you will get two R options.

@KaQing

This comment has been minimized.

Copy link

KaQing commented Oct 14, 2016

run as

@ghost

This comment has been minimized.

Copy link

ghost commented Oct 18, 2016

If you're getting the "Unfortunately Just Java has stopped" error. Then you may have miss spelled a function. In my activity_main.xml file I had put "submitOrders" instead of "submitOrder" for my button. Once I removed the extra letter 's' it ran perfectly. Check that your code had no such errors.

@albertoRojasLefort

This comment has been minimized.

Copy link

albertoRojasLefort commented Oct 19, 2016

Just added "package com.example.android.justjava;" before the code listed here and everything worked as a charm, hope this helps!!

@jibranejaz

This comment has been minimized.

Copy link

jibranejaz commented Oct 21, 2016

theres nothing wrong with the code provided... read the comments.. it says to add the package name on top of our code which in our(by the steps we have followed through out the course) case is package com.example.android.justjava;

@dosmode

This comment has been minimized.

Copy link

dosmode commented Oct 23, 2016

I followed albertoRojasLefort's advice and it worked as well!

Thank you

@00amaranth00

This comment has been minimized.

Copy link

00amaranth00 commented Oct 23, 2016

Thanks for all the help guys ..

I was getting error: package R does not exist and tried @jonbrinkley's and @abhi472's recommendation and it worked for me ..

Unfortunately, Just Java has stopped though when i tapped on the order button ..

** Screenshot after replacing the default MainActivity.java with the one provided by Udacity ..

screenshot from 2016-10-23 13 24 08

** Inserting package [package name]; , package name listed on your AndroidManifest.xml file ..
screenshot from 2016-10-23 13 23 40

@ziterz

This comment has been minimized.

Copy link

ziterz commented Oct 24, 2016

image
very nice!

@gcleitom

This comment has been minimized.

Copy link

gcleitom commented Oct 24, 2016

If you are having problem with R.
Look that in the original code the line 7 doesn't exist so the package wasn't set in the original link.
Copy the code below.

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
    *
  • package com.example.android.justjava;
    */

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }
@ML0126

This comment has been minimized.

Copy link

ML0126 commented Oct 29, 2016

The Android studio can't resolve the alphabet R...

@AlisonOliveira

This comment has been minimized.

Copy link

AlisonOliveira commented Oct 30, 2016

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
    *
  • Não pode esquecer de adicionar essa PKG package com.example.android.justjava;
    */
    package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(2);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }
@ajayanku

This comment has been minimized.

Copy link

ajayanku commented Oct 31, 2016

screenshot 34
screenshot 35
screenshot 36
plz help me.

@sinamotamedi

This comment has been minimized.

Copy link

sinamotamedi commented Nov 7, 2016

I had some errors after copying/pasting but I just add to put the following code in after the initial commented section.

package com.example.android.justjava;

It looks like they changed the code a little bit since the video was made so Katherine doesn't mention that. But if you have been following their instructions and using the same names, you can just copy/paste that line of code and put it above the import code lines and under the comment block at the top.

@shubh120

This comment has been minimized.

Copy link

shubh120 commented Nov 9, 2016

hey i have the Solution from the problems related to ###R and the what is quantity_text_view something like that
for the quantity view make sure you had added that line android:id="@+id/quantity_text_view"
and make sure to add android:onClick="submitOrder" in the button and the rest of the code is

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    display(1);
}

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

}

happy to help:)

@shuvo3000

This comment has been minimized.

Copy link

shuvo3000 commented Nov 13, 2016

All of you who r getting the R error the provided code is missing the line

package com.example.android.justjava;

add this line to the beginning of your code and everything should run fine.

@asadhamzu

This comment has been minimized.

Copy link

asadhamzu commented Nov 17, 2016

<style name="Base.Widget.AppCompat.Spinner.Underlined" parent="android:Widget.Material.Spinner.Underlined"/> when i run forn device thi show nup why!
@BalaPrasad346

This comment has been minimized.

Copy link

BalaPrasad346 commented Nov 26, 2016

screenshot 21
screenshot 22
i am getting this errors. plz explain me how to remove them

@JaeHeyK

This comment has been minimized.

Copy link

JaeHeyK commented Nov 29, 2016

solution
Don't forget to add your OWN package name at top of "MainActivity.java"

@mikito1987

This comment has been minimized.

Copy link

mikito1987 commented Nov 29, 2016

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • package com.example.android.justjava;
    */
    package com.example.android.justjava;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.View;
    import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends ActionBarActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.zero_text_view);
      quantityTextView.setText("" + number);
      }
      }

I've changed and this code is worked :)

@anolletti

This comment has been minimized.

Copy link

anolletti commented Dec 4, 2016

I am having problems with this very code. I have added a package name, but now I receive this error:
MainActivity is not an Activity subclass or alias

Here is my manifest file:

<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"
        android:label="@string/app_name"
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

Below is my Java File:
package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(
      R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }
@PraneethYerra

This comment has been minimized.

Copy link

PraneethYerra commented Dec 11, 2016

pressing alt+enter will import the necessary packages to the code so just go to an error and press alt+enter

@JesseMurias

This comment has been minimized.

Copy link

JesseMurias commented Dec 12, 2016

fix the code by adding the missing line:

package com.example.android.justjava; above import android.os.Bundle;

@tgships

This comment has been minimized.

Copy link

tgships commented Dec 16, 2016

Guys, for those of you who shared your thoughts and the right codes, a BIG BIG THANK YOU!!! And for those of you who haven't got it right, just read through all the comments, I mean those who shared the right codes. They are all there. Just patiently read it through.

@jflig001

This comment has been minimized.

Copy link

jflig001 commented Dec 18, 2016

I don't cut and paste active_main. It's faster to type the code code InteliJ imports everything you need.

@tijnar

This comment has been minimized.

Copy link

tijnar commented Dec 18, 2016

Guys!! I have tried all the methods .. Saw all the questions and solutions. But having problem on R(can not resolve symbol R ). I have listed my all codes (activity_main.xml, MainActivity.java , AndroidManifest.xml) respectively. Please give me some solution, so that I can go ahead.
1
2
3

@orlandoweise

This comment has been minimized.

Copy link

orlandoweise commented Dec 19, 2016

For everyone having the "R" error. Just make sure you add your package. There is a comment that states to add your package in the MainActivity file. Just add your package for example "com.example.android.justjava;" (without quotes) and your project should execute.

Hope this helps.

@iAshishVerma

This comment has been minimized.

Copy link

iAshishVerma commented Dec 21, 2016

Add your package name

@pranavchandra27

This comment has been minimized.

Copy link

pranavchandra27 commented Dec 21, 2016

please help meeeee!

i'm facing this problem again and again

@erocurim

This comment has been minimized.

Copy link

erocurim commented Dec 24, 2016

After a while I get rid of the warnings, but now the quantity does not change when I click the "ORDER" botton. Any toughts?
Please help!

@irfansattar

This comment has been minimized.

Copy link

irfansattar commented Dec 26, 2016

Hey guys, I have no idea why there are errors with the provided code when I copy/paste it. I've done everything we were required to (I added @+id/quantity_text_view, I have the submitOrder line, I added the package name) but Android Studio still gives the various errors people have been discussing throughout this thread. Any help is appreciated.

image

@jadduT

This comment has been minimized.

Copy link

jadduT commented Dec 26, 2016

iam getting an error as default activity not found
error

@jadduT

This comment has been minimized.

Copy link

jadduT commented Dec 26, 2016

guys i found the solution of the problem thwe first code provided is correct but a bit modification is need so as we know that package name must be added but in the provided code the package name package com.example.android.justjava; is written inside comment so just copy it out and paste outside the comment braces and it works

@ecomaikgolf

This comment has been minimized.

Copy link

ecomaikgolf commented Dec 27, 2016

Remember to put the package name! And it must be outside the comment section

@LiorA1

This comment has been minimized.

Copy link

LiorA1 commented Dec 30, 2016

now i getting:
"Error running MainActivity: MainActivity is not an Activity subclass or alias"

love some help..

@TheClassicalGod

This comment has been minimized.

Copy link

TheClassicalGod commented Jan 1, 2017

I can confirm that, the code on this page(as it is today, at least) has one small issue. The package name is listed in the comment section of the code toward the top of the file.

package com.example.android.justjava:

Add that line above the import lines in your MainActivity.java file and the errors about R. not referencing the XML properly went away for me. I was getting the app to load but immediately force closing until I realized that line was missing. I kinda would have preferred this tutorial go through each line step by step so we understand what is needed and why, but I understand that they just kinda wanted us to slap things together now and dissect later. It's also entirely possible they did this deliberately so we looked for answers ourselves. lol Just saying.

@LiorA1

This comment has been minimized.

Copy link

LiorA1 commented Jan 1, 2017

^ I done this already..
Maybe I ruined something..
Try it again..

@TheClassicalGod

This comment has been minimized.

Copy link

TheClassicalGod commented Jan 2, 2017

@LiorA1 - You might have also done something that I messed up early they started talking about removing unneeded stuff and I didn't see a line in the video that I saw in my XML, so I got rid of it. lol Specifically the tools:context="MainActivity" and/or the android:id=@+id/activity_main" lines. Those are the two lines making sure the XML and Java are linked together properly.

If that isn't the problem and you figure it out, let us know. the XML stuff is easy, it's the java that i'm still trying to figure out. lol I know how frustrating it is to feel like you've run into an issue that everyone else didn't and nobody seems to have the answer and everyone just kinda shrugs when you ask for help. It sucks. I'll keep checking back to see if you got past here. Good luck.

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:orientation="vertical"
    tools:context="MainActivity" >```
@DanyoBEO

This comment has been minimized.

Copy link

DanyoBEO commented Jan 2, 2017

Hey Guys, I get a Process finished with exit code 1 Class not found: "com.example.android.justjava.ExampleInstrumentedTest"Empty test suite.
and I followed every post on this thread. Here what I got.

` package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }`

`

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="Quantity"
    android:textAllCaps="true" />

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:textColor="#000000"
    android:textSize="16sp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="Order"
    android:textAllCaps="true"
    android:onClick="submitOrder" />
` Please let me know if you need any more info. I followed to the best I can.
@TheClassicalGod

This comment has been minimized.

Copy link

TheClassicalGod commented Jan 2, 2017

1 Class not found: "com.example.android.justjava.ExampleInstrumentedTest"

Did you name the project something a bit different than what was said in the tutorial maybe? If so, the package line in you java code needs to reflect what you named it. It could just be the way it was pasted and i'm seeing that wrong. I don't see much else that could be the problem at this point though.

Edit: I think I see what might have happened. You edited or even possibly deleted the wrong java file. I haven't tried to do this myself to see if it broke mine the same way, but if that is the case, i'm not sure how to fix it. Best bet would be to copy your XML and start fresh with newly generated java files and paste your XML back where it was in the new project. Then copy the java from here again. Just make sure you copy the java into the MainActivity.java instead of ExampleInstrumentedTest.java like it looks you may have the first time. Hopefully this gets you going again.

@makhloufabdelkader

This comment has been minimized.

Copy link

makhloufabdelkader commented Jan 3, 2017

What i did and worked well for me is uncommenting the fifth line:
package com.example.android.justjava;

@szklimek

This comment has been minimized.

Copy link

szklimek commented Jan 4, 2017

Hello. I had very similar problems as yours. But finally I found out what was wrong and now it's working as expected.
I have Android Studio 2.2.3 and during new project creation there is option - Backwards Compatibility (AppCompat) which must be checked.
justjavasolution

At first I left this unchecked and had problems with AppCompat. I tried to add compile 'com.android.support:appcompat-v7:25.1.0' to build.gradle file, apps compiled but always stops after running it on phone.

So I suggest (if you are not sure what you chose) making new JustJava app and check option Backwards Compatibility (AppCompat).

@shekharmayank

This comment has been minimized.

Copy link

shekharmayank commented Jan 4, 2017

Just add package com.example.android.justjava; below first comment in MainActivity.java, all the errors will be gone.

@amalaugustinejose

This comment has been minimized.

Copy link

amalaugustinejose commented Jan 4, 2017

`package com.example.android.justjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void submitOrder(View view) {
    display(1);
}

private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

}

`
Finally fixed all the errors. This worked for me

@srikanthmaneti

This comment has been minimized.

Copy link

srikanthmaneti commented Jan 6, 2017

Here is the Working Code

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Quantity"
    android:textAllCaps="true"
    android:id="@+id/textView" />

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:layout_margin="16dp"
    android:text="0" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/quantity_text_view"
    android:text="Order"
    android:onClick="submitOrder"
    android:id="@+id/button" />

MainActivity.Java Code..

package com.example.android.justjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
/*

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /*

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /*

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }

}

@ch359

This comment has been minimized.

Copy link

ch359 commented Jan 7, 2017

If you're getting the AppCompatActivity errors, the simplest way to fix them seems to be:

File -> Project Structure -> Dependencies tab -> Click the +, Select 'library dependencies' -> double click com.android.support:appcompat-v7:25.1.0

Once gradle had finished doing its thing, all of my red errors vanished.

Edit: Also had to change AndroidManifest.xml to show '@style/Theme.AppCompat' instead of what was there before to avoid a crash on runtime.

@netmorgan

This comment has been minimized.

Copy link

netmorgan commented Jan 9, 2017

package com.example.android.justjava is hidden in the comments to the code
just open it up

@AmirErnest

This comment has been minimized.

Copy link

AmirErnest commented Jan 9, 2017

The R error can be solved by adding the package name which can be found in the manifest.xml file...
the r error

@HumbsBR

This comment has been minimized.

Copy link

HumbsBR commented Jan 12, 2017

Thanks!

@LiorA1

This comment has been minimized.

Copy link

LiorA1 commented Jan 13, 2017

@TheClassicalGod -

Thanks !
Solve it!

@Manoj0718

This comment has been minimized.

Copy link

Manoj0718 commented Jan 14, 2017

@sarahfriedli your suggestion totally worked. I was stuck already 2 days with that

@8vasa8

This comment has been minimized.

Copy link

8vasa8 commented Jan 15, 2017

@AmirErnest Finally working thanks

@zutamacka

This comment has been minimized.

Copy link

zutamacka commented Jan 16, 2017

I had errors too, I figured out I needed to leave in the line
package net.huskydesigns.justjava;
or, if you used default settings,
package com.example.android.justjava;
which is the top line in your code and includes your package. I'm leaving the comment here for all those who didn't use the default settings.

@fragargon

This comment has been minimized.

Copy link

fragargon commented Jan 16, 2017

just had the package name, it is located in AndroidManifest.xml
it looks like:

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • package com.example.android.justjava;
    */
    package com.example.kiko.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

@lil90

This comment has been minimized.

Copy link

lil90 commented Jan 17, 2017

just copy all of the code from line 8 to 38 and paste it over your code, leaving your package name at the top, also, you may need to change the variable at line 35:
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
to whatever you used as the id for the quantity text view in the xml, in my case, I used
android:id="@+id/quantity"
rather than
android:id="@+id/quantity_text_view"

@filiphanek

This comment has been minimized.

Copy link

filiphanek commented Jan 17, 2017

Thanks for the package hint! Works for me.

@leonardocossio

This comment has been minimized.

Copy link

leonardocossio commented Jan 18, 2017

I spent 5 hours taking all the steps proposed above and still doesn't work.

@gobbletsit

This comment has been minimized.

Copy link

gobbletsit commented Jan 18, 2017

Hey guys,

After i did all the mentioned above i'm still having this:

error: class MainActivity is public, should be declared in a file named MainActivity,java

This is how my code in java looks like:

package com.example.android.justjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
/*

This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/*

This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
    display(1);
}
/*

This method displays the given quantity value on the screen.
*/
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

}

Does anyone know any other solution to this problem?

I've tried restarting Android Studio, going thru the depandancies tab just as @ragnar321 commented and all the basic copy/paste AppCompat and still no luck......

@danserb

This comment has been minimized.

Copy link

danserb commented Jan 18, 2017

If you just read the comments in the code they provide us. It clearly says "ADD YOUR PACKAGE BELOW". That means, that you need to write your package before the other packages imported so after the "comments" code in the one you need to copy over to your code, you need to write this: package com.example.android.justjava; .. Hope it helps! It's pretty simple if You read the comments that are in the code

@danserb

This comment has been minimized.

Copy link

danserb commented Jan 18, 2017

gobbletsit have you checked your "submitOrder" call on your xml button ? I had a similar error because I wrote by mistake SubmitOrder instead of submitOrder

@r4ul0

This comment has been minimized.

Copy link

r4ul0 commented Jan 19, 2017

If you get into too many troubles, i just recommend you to go back and restart from beginning deleting the current and buggy project.

Actually this code runs pretty well if you try to just write down correctly everything as teached on the Udacity lesson. Pay attention to the package and apps names, and on the xml file take care on the text view id and the onClick method called.

The tricky part is while you have to change the MainActivity java file. You just have to copy the package declaration on the top of your running code BEFORE trying to run it. I found that tryng to solve it by the methods proposed by AndroidStudio could take you to those problems that bring you here at forums.

(Your Java Main Activity code should be like this)

package com.example.android.justjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

@Graphmaster

This comment has been minimized.

Copy link

Graphmaster commented Jan 19, 2017

I feel like a lot you (including me) may have deleted this:

package com.example.adroid.justjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • package com.example.android.justjava;
    */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

We should not have done that.
Instead delete everything after that in the manifest then paste the code. Worked that way for me.

@ChronicFusion

This comment has been minimized.

Copy link

ChronicFusion commented Jan 20, 2017

I went through the code and found what was wrong and changed it up a little. This all works perfectly fine on my PC. Additionally, I have not changed any settings in android studio.
I suggest backing up what you have, then completely removing the java and xml code and replacing it with what I have. This SHOULD resolve all issues.
Note: Android Studio v2.2.3

-----MainActivity.java code-----

package com.example.android.justjava;

 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

}



/*

 * This method is called when the order button is clicked.

 */

public void submitOrder(View view) {

    display(1);

}



/*

 * This method displays the given quantity value on the screen.

 */

private void display(int number) {

    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);

    quantityTextView.setText("" + number);

}

}

-----activity_main.xml code-----

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="QUANTITY"/>

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="0"
    android:textColor="#000000"
    android:textSize="16sp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="submitOrder"
    android:text="ORDER"/>
@didishas

This comment has been minimized.

Copy link

didishas commented Jan 20, 2017

This code from JHTWTPI works for me

`package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

/**

This method is called when the order button is clicked.
/
public void submitOrder(View view) {
display(1);
}
/
*

This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
}`

@muhammadsalehahmad

This comment has been minimized.

Copy link

muhammadsalehahmad commented Jan 21, 2017

In my case Its error happen due to it ( the MainActivity.java) cant recognize the xml file or in other words ''WRONG PATH'' so
to fix this just make some copy of your original code in MainActivity.java to some notes, or editor ( in case you dont remember the path ) then copy all of example java code above to your MainActivity.java afterward just copy the line in your original code ( that you copied to notes/editor) which is starting with package then paste to the top of your new code. as you can see in my code mus itself is my directory where the android studio create the MainActivity.java for this project ..hope it helps :)

image

image

@muhammadsalehahmad

This comment has been minimized.

Copy link

muhammadsalehahmad commented Jan 21, 2017

In my case Its error happen due to it ( the MainActivity.java) cant recognize the xml file or in other words ''WRONG PATH'' so
to fix this just make some copy of your original code in MainActivity.java to some notes, or editor ( in case you dont remember the path ) then copy all of example java code above to your MainActivity.java afterward just copy the line in your original code ( that you copied to notes/editor) which is starting with package then paste to the top of your new code. as you can see in my code mus itself is my directory where the android studio create the MainActivity.java for this project ..hope it helps :)

image

image

@CostasSmylianis

This comment has been minimized.

Copy link

CostasSmylianis commented Jan 22, 2017

I made two changes to the code to make it work

First:
in line 4 replace:
import android.support.v7.app.ActionBarActivity;
with:
import android.support.v7.app.AppCompatActivity;

Second:
in the MainActivity.java file that you copied and pasted you have to add the line describing wehte the package is:
package com.example.android.justjava;

I just copied it from the comment.
If your application id is different you can find it from:
menu file\project structure\app\stucture

projectid

@ragalyig

This comment has been minimized.

Copy link

ragalyig commented Jan 22, 2017

The first line is missing from the code above:
package com.example.android.justjava;

@metasonic

This comment has been minimized.

Copy link

metasonic commented Jan 22, 2017

Hey guys,
If you have issues displaying the $ (Dolar) sign, this works for me

/**
     * This method displays the given price on the screen.
     */
    private void displayPrice(int number) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance(Locale.US).format(number));
    }
@milonsultan

This comment has been minimized.

Copy link

milonsultan commented Jan 23, 2017

Just add these line "package com.example.android.justjava;" before "import android.os.Bundle;".

@MeganStefaniak

This comment has been minimized.

Copy link

MeganStefaniak commented Jan 24, 2017

I have a problem guys...
zrzut ekranu 2017-01-24 o 22 41 53

and I can't go to the next step :( Is there someone, who can try to help me out?
Thanks in advice.

@nicjaws

This comment has been minimized.

Copy link

nicjaws commented Jan 25, 2017

I got errors as well:

package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void submitOrder(View view) {
    display(1);
}

private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.zero_text_view);
    quantityTextView.setText("" + number);

screen shot 2017-01-25 at 14 26 45

@iz4atkal

This comment has been minimized.

Copy link

iz4atkal commented Jan 25, 2017

Followed the instructions from the first comment and it worked for me.

@Arunav100

This comment has been minimized.

Copy link

Arunav100 commented Jan 25, 2017

Ok just wanted to share my experience if anyone is facing troubles with "R" and are using the exact file name given by the instructors then the below should fix it.

/**
 * Add your package below. Package name can be found in the project's AndroidManifest.xml file.
 * This is the package name our example uses:
 * 
 * package com.example.android.justjava; 
 */

The above is a comment and is not a part of the code. But there is one line that should be a part of the code and is in there in the comments area i.e. "package com.example.android.justjava; " add this package name to your MainActivity without quotes and it should work.
Hope this helps others

@rudhman

This comment has been minimized.

Copy link

rudhman commented Jan 25, 2017

Don't delete the first line of the existing code present while pasting the new code.

@piotrsierpinski

This comment has been minimized.

Copy link

piotrsierpinski commented Jan 26, 2017

[To Udacity coursants] -> If you follow the naming from the video, than you should ONLY copy the form package com.example.android.justjava; (with colon!) ABOVE import android.os.Bundle; (that means copying to line 7). Worked for me.

@TerraMaficco

This comment has been minimized.

Copy link

TerraMaficco commented Jan 26, 2017

Adding to @piotrsierpinski
If the line "package com.example.android.justjava" is not added above the import-statements, then the error with R occures.

@spaingirl

This comment has been minimized.

Copy link

spaingirl commented Jan 26, 2017

@TerraMaficco is right, adding package "com.example.android.justjava; " out of comments "*" and Build > build APK, it works

@ybAlfie

This comment has been minimized.

Copy link

ybAlfie commented Jan 29, 2017

For anyone still having issues, here is my finished code that finally got it all working..

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.android.justjava.R;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }
@bettinat

This comment has been minimized.

Copy link

bettinat commented Jan 29, 2017

Everyone who has a problem about the letter "R": there is nothing to worry, you only need to add one line to the code in MainActivity.java:

Your first line should be this:
package com.example.android.justjava;

!!You only have to add this line, and everything will be working!!

@UpsilonB

This comment has been minimized.

Copy link

UpsilonB commented Jan 30, 2017

Hi all fellow EU Scholarship participants

You might get errors because of the AppCompatActivity. This activity is selected while creating the project. If you did not select it, you will get this error. Selecting this option is not mentioned during the lessons, so that is why I did not select it.

The code used in the video with ActionBarActivity is also incorrect. The ActionBarActivity is deprecated code. It is replaced by AppCompatActivity.

At this point, I can not guarantee that the code below will not get you (or me) in trouble in the next lessons. But the code works and gives no errors IF you did not select AppCompatActivity.

package com.example.android.justjava;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        display(1);
    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }
}

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 7, 2017

Hi guys!
I've also had the "R problem". I know that it's pretty basic but check once again if your project name is correct. In my case I had project named "JustJave" and not "JustJava". After I've had created new project with correct name everything was fine.

@xxyzz

This comment has been minimized.

Copy link

xxyzz commented Feb 7, 2017

Thanks!

@plmiso

This comment has been minimized.

Copy link

plmiso commented Feb 7, 2017

I have to admit, great excersise - learned a lot about how java files operates between each other!

@matteolecca

This comment has been minimized.

Copy link

matteolecca commented Feb 8, 2017

The code works you have only to move the line 5 ( package com.example.android.justjava;) from the comment to the code.

`
package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void submitOrder(View view) {
    display(1);
}


private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

}`

@btariq18

This comment has been minimized.

Copy link

btariq18 commented Feb 8, 2017

uncomment package com.example.android.justjava;

@daxiaokongyi

This comment has been minimized.

Copy link

daxiaokongyi commented Feb 10, 2017

It's easy to fix if you have an error with "R problem". you only need to add one sentence at very beginning:

package com.example.android.justjava;

hope it works : )

@babacus

This comment has been minimized.

Copy link

babacus commented Feb 11, 2017

It worked for me. just uncommented " package com.example.android.justjava;" and it worked. thanks.

@wonbinbk

This comment has been minimized.

Copy link

wonbinbk commented Feb 13, 2017

I got errors regarding the R layout too.
And yes, it worked after I uncommented "package com.example.android.justjava;".
Thanks

@ChandraniChatterjeeMolly

This comment has been minimized.

Copy link

ChandraniChatterjeeMolly commented Feb 13, 2017

@wonbinbk
for the error regarding R, just add the package name at the start of the code.
package com.example.android.justjava;

@pavanpucha

This comment has been minimized.

Copy link

pavanpucha commented Feb 14, 2017

I feel like a complete idiot. I haven't read the comments in the program and was wondering why my app was crashing. I had to add my package., however explicit instructions in the course were, copy the file and run,
they could have told us to read a little bit of code,
Patience +1

@moacirsis

This comment has been minimized.

Copy link

moacirsis commented Feb 14, 2017

Olá alguém pode me ajudar

quantity

@moacirsis

This comment has been minimized.

Copy link

moacirsis commented Feb 14, 2017

já resolvi tem que chamar o id.quantity_text_view na activity_main :)

@almondion

This comment has been minimized.

Copy link

almondion commented Feb 15, 2017

Problem with the R is also solved by pasting the code after the first line of the original code. :-)

@diacristian

This comment has been minimized.

Copy link

diacristian commented Feb 16, 2017

simple fix. package code is omitted:

package com.example.android.justjava;

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 16, 2017

Hey fellow students, I can't solve this error:
screenshot 1

Feeling like an idiot. I've been trying to solve the error for hours now. =/ What am I missing?

Edit: This worked for me

Hi @danimerk,
The R class is generated when the project is built, so you will get this error any time there is a problem in your app that stops it from building. If you go to Build -> Clean Project and then `Build -> Make Project does the error resolve?
If not there is likely an error in your XML or java code somewhere, but let's try the clean and build first :slight_smile:

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 18, 2017

wow !! it finally worked.
if you are getting "R class does not exist " error, then you just have to add the package name at the start of the code. If you face any trouble in finding the package, simply follow the comments at the start of your code.
Good luck have fun!!!

@SujitAmbore

This comment has been minimized.

Copy link

SujitAmbore commented Feb 18, 2017

Hi, Dont worry the code will run just check your "activity_main.xml" page and check the submitOrder spelling and compare that to public void submitOrder(View view) method in java. Both should be same or else error will be thrown

@JonDC

This comment has been minimized.

Copy link

JonDC commented Feb 19, 2017

Hi there,

Here's what I did just in case there might be any additional late comers:

  1. I added a dot next to MainActivity just because it felt like that might have been the file extension. Soon as I did that, the little red light bulb appeared on the left of my screen
    capture

  2. Then if you hover over the red light bulb a downward facing arrow will appear and you'll get a suggestion of what to do. Press that and it connects it to your initial file. It also gives you a list of activities it performs if you do press it. But I haven't gotten around to figuring what they mean just yet
    image

  3. Don't forget to delete the dot next to MainActivity. So from .MainActivity go back to just Main Activity

It just adds the package name but it's shorter to just follow the suggestions the software gives you.
Hope this helps guys! Luck!

@sabinaioana25

This comment has been minimized.

Copy link

sabinaioana25 commented Feb 20, 2017

Hi all.

I've tried running the app. after copying and pasting the code given by Udacity, but I have the following list of errors due to which the app. won't run on the emulator / phone. 😞

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.justjava, PID: 3175
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.justjava/com.example.android.justjava.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.android.justjava.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.android.justjava-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.android.justjava-2/lib/x86, /system/lib, /vendor/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.android.justjava.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.android.justjava-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.android.justjava-2/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

This is what appears as being the main issue:
screenshot

Any ideas what's causing this? Thanks!

@sabinaioana25

This comment has been minimized.

Copy link

sabinaioana25 commented Feb 20, 2017

Update: it seems it was the same thing everyone here had. 😊
But is it normal that it only adds up to the value of 1, regardless of how many times you press the button? 🤓

@jomuziek

This comment has been minimized.

Copy link

jomuziek commented Feb 22, 2017

package com.example.android.justjava

don't forget to add this code in the java file

@dillichalise

This comment has been minimized.

Copy link

dillichalise commented Feb 24, 2017

I have a problem while running app. It says "unfortunately, just java has stopped working." How can i resolve this problem?
Activity_main.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Quantity"
    android:textAllCaps="true"
    android:layout_marginBottom="16dp"/>

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:text="0"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Price"
    android:layout_marginTop="16dp"
    android:textSize="16sp" />
<TextView
    android:id="@+id/price_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$0"
    android:layout_marginTop="16dp"
    android:textSize="16sp" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Order"
    android:layout_marginTop="16dp"
    android:onClick="submitOrder"
    />

MainActivity.java

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • package com.example.android.justjava;
    */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
import com.example.dillichalise.justjava.R;

/**

  • This app disays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      int nc=2;
      display(nc);
      display(nc * 5);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      private void displayPrice(int number) {
      TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
      priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
      }

}

@heyrohitjain

This comment has been minimized.

Copy link

heyrohitjain commented Feb 27, 2017

capture

I am facing this Problem.
Please Help.

@ChandanAkhilReddy

This comment has been minimized.

Copy link

ChandanAkhilReddy commented Feb 27, 2017

@heyrohitjain
use this code..

package com.example.android.justjava; /**
Add your package below. Package name can be found in the project's AndroidManifest.xml file.
This is the package name our example uses:
*
package com.example.android.justjava;
/
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.example.android.justjava.R;
import static com.example.android.justjava.R.id.quantity_text_view;
/
*
This app displays an order form to order coffee.
/
public class MainActivity extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/
*
This method is called when the order button is clicked.
/
public void submitOrder(View view) {
display(1);
}
/
*
This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(quantity_text_view);
quantityTextView.setText("" + number);
}
}

@abhineshchandra

This comment has been minimized.

Copy link

abhineshchandra commented Mar 1, 2017

i am facing some problem in the code plzz help me
screenshot 62

@Valayah

This comment has been minimized.

Copy link

Valayah commented Mar 3, 2017

I GOT IT GUYS!

First: DO NOT delete all java code for your Activity main. Instead, leave the first line (PACKAGE).
Second: Paste the code they gave you;
Third: Make sure it refers to the right text view on the code: TextView quantityTextView = (TextView) findViewById(R.id.quantify_text_view);

It worked for me!

The whole code become like this:

package com.example.android.justjava;

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • package com.example.android.justjava;
    */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */
      public void submitOrder(View view) {
      display(1);
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantify_text_view);
      quantityTextView.setText("" + number);
      }
      }
@thanhhocse96

This comment has been minimized.

Copy link

thanhhocse96 commented Mar 4, 2017

At the begin you have that line:
package com.example.android.justjava;

Change it to your package name, the red box in this pic
screenshot 1

@dineshpahuja

This comment has been minimized.

Copy link

dineshpahuja commented Mar 4, 2017

hi smilc,
Thanx for the solution it worked for me...

@KedCole

This comment has been minimized.

Copy link

KedCole commented Mar 5, 2017

Hi - does anyone get error that "JustJava stopped working" and it closes app when you try to push ORDER button?

I have the following code:

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**

 This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    display(1);
}
/**

 This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

}

and this XML

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="16dp"
    android:text="Quantity"
    android:textAllCaps="true"
    android:textSize="20sp"/>

<TextView
    android:id="@+id/quanitiy_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="16dp"
    android:text="0"
    android:textColor="#000000"
    android:textSize="20sp"/>

<Button
    android:id="@+id/order_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="order"
    android:textSize="20sp"
    android:onClick="submitOrder"/>

ANY SUGGESTIONS??

@InderSethi

This comment has been minimized.

Copy link

InderSethi commented Mar 5, 2017

Well the problem I found is in us that we are not reading the comments :

The probelm is that we are not including our package in it and it is mentioned in it, I'm using the com.example.android.justjava may be you're using the different one so please be sure what you wrote up the name of the file to Just Java, If yes than it'll work.

Just Paste this code in your java file :

/**

  • Add your package below. Package name can be found in the project's AndroidManifest.xml file.
  • This is the package name our example uses:
  • package com.example.android.justjava;
    */
    package com.example.android.justjava;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;

import com.example.android.justjava.R;

/**

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    /**

    • This method is called when the order button is clicked.
      */

    public void submitOrder(View view) {
    display(1);
    }

    /**

    • This method displays the given quantity value on the screen.
      */
      private void display(int number) {
      TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }
@KedCole

This comment has been minimized.

Copy link

KedCole commented Mar 5, 2017

Thanks InderSethi for your comment very much, as you can see in my previous content, the package was clearly added as well.

The app displays on the emulator when I run the build, but it is not executing after you click ORDER...you are suggesting this is related to package, correct? But it must be another error as app crashes when I click ORDER.

Any other suggestions?

@singhmj

This comment has been minimized.

Copy link

singhmj commented Mar 12, 2017

@KedCole
The mistake is actually with the ID you assigned to your Quantity TextView in XML file.
There is a typo here :
android:id="@+id/quanitiy_text_view"
rewrite it as
android:id="@+id/quantity_text_view and it will most likely solve the problem.

@Ridwanabdusalam

This comment has been minimized.

Copy link

Ridwanabdusalam commented Mar 13, 2017

Let anyone try this:

package com.example.android.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**

 This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    display(2);
}
/**

 This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(
            R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

}

And for the XML file:

<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.android.justjava.MainActivity">

<LinearLayout
    android:layout_width="368dp"
    android:layout_height="495dp"
    android:orientation="vertical"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:text="Quantity"
        android:textAllCaps="true"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:text="0"
        android:textColor="#000000"
        android:textSize="20sp"/>

    <Button
        android:id="@+id/order_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="order"
        android:textSize="20sp"
        android:onClick="submitOrder"/>
</LinearLayout>

</android.support.constraint.ConstraintLayout>

---------I hope it works perfectly------------------