Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Last active December 14, 2024 16:53
Show Gist options
  • Save udacityandroid/83242daf8a43b743d05e98733a35b19f to your computer and use it in GitHub Desktop.
Save udacityandroid/83242daf8a43b743d05e98733a35b19f to your computer and use it in GitHub Desktop.
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);
}
}
@ChetanSinghNegiCSE
Copy link

ChetanSinghNegiCSE commented Jan 4, 2022 via email

@nitish9065
Copy link

there is not a single line of error in my code. However i am not able to run my app on my phone .

@nitish9065
Copy link

oo
see i'm getting thsi error while building my app

@nitish9065
Copy link

please someone help me to figure thsi out!!

@raunakkolle
Copy link

If you use androidX instead of android, you need change

import android.support.v7.app.AppCompatActivity;
to

import androidx.appcompat.app.AppCompatActivity;

to check for androidx, go to Gradle scripts->build.gradle(Module:<name_of_the_project>.app)

@MrpRasid
Copy link

use this import androidx.appcompat.app.AppCompatActivity;
instead of
import android.support.v7.app.AppCompatActivity;

@iwisedevil
Copy link

when i start to run the code the app crashes, anyone meet this problem before?

@kongkl
Copy link

kongkl commented May 6, 2022

when i start to run the code the app crashes, anyone meet this problem before?

Same issue
I think mine has to do with the quantity_text_view

@saurav-ux
Copy link

Try this code

package com.anand.intro; //write your package

import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView num = findViewById(R.id.num);
    Button order1 = findViewById(R.id.order);
    /**
     * This method is called when the order button is clicked.
     */
order1.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
         display1(1);
     }
    /**
     * This method displays the given quantity value on the screen.
     */
     private void display1(int number)
     {
         //  TextView quantity = (TextView) findViewById(R.id.quantity);
         num.setText("" + number);
     }


 });
}

}

In XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"

android:orientation="vertical"


tools:context=".MainActivity">

<TextView
    android:id="@+id/quantity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="7dp"
    android:padding="7dp"
    android:text="QUANTITY"
    android:textSize="24sp" />

<TextView
    android:id="@+id/num"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="7dp"
    android:padding="7dp"
    android:text="0"
    android:textSize="24sp" />

<Button
    android:id="@+id/order"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="7dp"
    android:padding="7dp"
    android:text="ORDER"

    />

Copy link

ghost commented Jun 20, 2022

You need to change import android.support.v7.app.AppCompatActivity; to **import

I AM GETTING THIS ERROR WHEN I USED THIS CODE PLEASE HELP ME TO RESOLVE IT

    package com.example.justjava;

import androidx.appcompat.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);
}

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

}

THIS IS THE ERROR PLEASE HELP ME TO RESOLVE
androidx.appcompat.app.AppCompatActivity;**
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.android.build.gradle.internal.res.ParseLibraryResourcesTask$ParseResourcesRunnable

Copy link

ghost commented Jun 20, 2022

You need to change import android.support.v7.app.AppCompatActivity; to import androidx.appcompat.app.AppCompatActivity;

You need to change import android.support.v7.app.AppCompatActivity; to **import

I AM GETTING THIS ERROR WHEN I USED THIS CODE PLEASE HELP ME TO RESOLVE IT

    package com.example.justjava;

import androidx.appcompat.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);
}

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

}

THIS IS THE ERROR PLEASE HELP ME TO RESOLVE
androidx.appcompat.app.AppCompatActivity;**
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.android.build.gradle.internal.res.ParseLibraryResourcesTask$ParseResourcesRunnable

Copy link

ghost commented Jun 20, 2022

This Code worked for me :)

package com.example.justjava;

import androidx.appcompat.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);
}

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

}

You need to change import android.support.v7.app.AppCompatActivity; to **import

I AM GETTING THIS ERROR WHEN I USED THIS CODE PLEASE HELP ME TO RESOLVE IT

    package com.example.justjava;

import androidx.appcompat.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);
}

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

}

THIS IS THE ERROR PLEASE HELP ME TO RESOLVE
androidx.appcompat.app.AppCompatActivity;**
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.android.build.gradle.internal.res.ParseLibraryResourcesTask$ParseResourcesRunnable

Copy link

ghost commented Jun 20, 2022

This is the solution that worked for me. I was getting AppCompactActivity, onCreate, findViewById errors. Solution - Replacing the import android.support.v7.AppCompatActivity; with import androidx.appcompat.app.AppCompatActivity; This was the Error -> Screenshot (39) This is after implementing the solution -> Screenshot (40)

You need to change import android.support.v7.app.AppCompatActivity; to **import

I AM GETTING THIS ERROR WHEN I USED THIS CODE PLEASE HELP ME TO RESOLVE IT

    package com.example.justjava;

import androidx.appcompat.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);
}

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

}

THIS IS THE ERROR PLEASE HELP ME TO RESOLVE
androidx.appcompat.app.AppCompatActivity;**
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.android.build.gradle.internal.res.ParseLibraryResourcesTask$ParseResourcesRunnable

@Saphiriela
Copy link

Hi there,

if you have the latest version od Android studio, you won't have the .java document, but it will be in Kotlin. You need to translate Java into the Kotlin (it is done automatically) and than you'll have only 2 issues with mainActivity and quantity_text_view. This is how it is solved:

`package com.example.justjava;

import android.R
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

/**

This app displays an order form to order coffee.
*/
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(com.example.justjava.R.layout.activity_main)
}

/**
 * This method is called when the order button is clicked.
 */
fun submitOrder(view: View?) {
    display(1)
}

/**
 * This method displays the given quantity value on the screen.
 */
private fun display(number: Int) {
    val quantityTextView = findViewById<View>(com.example.justjava.R.id.quantity_text_view) as TextView
    quantityTextView.text = "" + number
}

}`

@madasmsm
Copy link

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-light"
    android:padding="15dp"
    android:text="Quantity"
    android:textAllCaps="true"
    android:textSize="22sp" />

<TextView
    android:id="@+id/txt_num"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="15dp"
    android:text="0"
    android:textColor="@color/black"
    android:textSize="22sp" />

<Button
    android:id="@+id/btn_order"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:text="order" />

package com.example.utest05;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

/*

  • This app displays an order form to order coffee.
    */
    public class MainActivity extends AppCompatActivity {
    TextView quantityTextView;
    Button order;
    @OverRide
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    quantityTextView = findViewById(R.id.txt_num);
    order = findViewById(R.id.btn_order);
    order.setOnClickListener(new View.OnClickListener() {
    @OverRide
    public void onClick(View v) {
    String txt = quantityTextView.getText().toString();
    int num = Integer.parseInt(txt);
    num += 1;
    quantityTextView.setText(String.valueOf(num));
    }
    });
    }
    }

@madasmsm
Copy link

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-light"
    android:padding="15dp"
    android:text="Quantity"
    android:textAllCaps="true"
    android:textSize="22sp" />

<TextView
    android:id="@+id/txt_num"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="15dp"
    android:text="0"
    android:textColor="@color/black"
    android:textSize="22sp" />

<Button
    android:id="@+id/btn_order"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:onClick="submitOrder"
    android:text="order" />

/*

  • IMPORTANT: Make sure you are using the correct package name.
  • This example uses the package name:
  • 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.utest05;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

/*

  • 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.txt_num);
      quantityTextView.setText("" + number);
      }
      }

@Hustler004
Copy link

Hi there,

if you have the latest version od Android studio, you won't have the .java document, but it will be in Kotlin. You need to translate Java into the Kotlin (it is done automatically) and than you'll have only 2 issues with mainActivity and quantity_text_view. This is how it is solved:

`package com.example.justjava;

import android.R import android.os.Bundle import android.view.View import android.widget.TextView import androidx.appcompat.app.AppCompatActivity

/**

This app displays an order form to order coffee. */ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(com.example.justjava.R.layout.activity_main) }

/**
 * This method is called when the order button is clicked.
 */
fun submitOrder(view: View?) {
    display(1)
}

/**
 * This method displays the given quantity value on the screen.
 */
private fun display(number: Int) {
    val quantityTextView = findViewById<View>(com.example.justjava.R.id.quantity_text_view) as TextView
    quantityTextView.text = "" + number
}

}`

Thank you so much totally worked for me and was looking for it since so long

@lmbrtm
Copy link

lmbrtm commented Jun 30, 2023

This is what worked for me:

/**

  • 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 android.example.justjava

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

/**

  • This app displays an order form to order coffee.
    */
    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    }

    /**

    • This method is called when the order button is clicked.
      */
      fun submitOrder(view: View?) {
      display(1)
      }

    /**

    • This method displays the given quantity value on the screen.
      */
      private fun display(number: Int) {
      val quantityTextView = findViewById(R.id.quantity_text_view) as TextView
      quantityTextView.text = "" +number
      }
      }

@wingmike
Copy link

wingmike commented Aug 5, 2024

This is the solution that worked for me. I was getting AppCompactActivity, onCreate, findViewById errors. Solution - Replacing the import android.support.v7.AppCompatActivity; with import androidx.appcompat.app.AppCompatActivity; This was the Error -> Screenshot (39) This is after implementing the solution -> Screenshot (40)

that worked for me as well

@hassenye
Copy link

Cannot resolve method 'createOrderSummary' in 'MainActivity' problem
Cannot resolve method 'displayMessage' in 'MainActivity' problem
Cannot resolve symbol 'quantity_text_view'
Method 'submitOrder(android.view.View)' is never used
Private method 'display(int)' is never used
Casting 'findViewById(...)' to 'TextView' is redundant
Do not concatenate text displayed with setText. Use resource string with placeholders. problem

please help on these issues

@firezer
Copy link

firezer commented Aug 27, 2024

Actually, I used the recent version Android studio and the "MainActivity" is kotlin which is MainActivity.kt and the following code is auto converted to it and error free

package com.example.justjava

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

/**
*

  • This app displays an order form to order coffee.
    */
    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    }

    /**

    • This method is called when the order button is clicked.
      */
      fun submitOrder(view: View?) {
      display(1)
      }

    private fun display(number: Int) {
    val quantityTextView = findViewById(R.id.quantity_text_view)
    quantityTextView.text = "" + number
    }
    }

@somo01-et
Copy link

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

I would love to rectify those errors, could you tell me how did you remove all those errors and now have one only ?

hi i fix tahe error but when i thche the button no thing haapn

@Destsh45
Copy link

Destsh45 commented Oct 6, 2024

finally i did it after hard works
try this code
package _"com.example.android.justjava" -(make sure to write you package name)

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView num = findViewById(R.id.num);
    Button order1 = findViewById(R.id.order);
    /**
     * This method is called when the order button is clicked.
     */
    order1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            display1(1);
        }
        /**
         * This method displays the given quantity value on the screen.
         */
        private void display1(int number)
        {
            //  TextView quantity = (TextView) findViewById(R.id.quantity);
            num.setText("" + number);
        }


    });
}

}

@SilasHakuzwimana
Copy link

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

I would love to rectify those errors, could you tell me how did you remove all those errors and now have one only ?

Hy, Which version of Android Studio are you using?

@FaizTeleha
Copy link

its my last project of the Android Developers final project for Congratulations to Nani

@FaizTeleha
Copy link


@FaizTeleha
Copy link

BUTTON CLICK
Modify activity_main.xml to add this Button XML attribute.
android:onClick = "submitOrder"
Replace entire MainActivity.java file with the file provided in the link to the code.
Run app on device. what can i do? pls tell me the answer of these Question
What happens when you click on the Button?

@FaizTeleha
Copy link

Modify activity_main.xml to add this Button XML attribute.
android:onClick = "submitOrder"
Replace entire MainActivity.java file with the file provided in the link to the code.
Run app on device.
What happens when you click on the Button?
Solution

  1. I modify activity_main.xml to onclick attribute
  2. I copy the MainActivity.java by new version ,then I click to run,it display nicely
  3. it update the quantity value one
    I still didn't get the answer of the question ? Help Help

@FaizTeleha
Copy link

fun main()
{
mood == "happy" && weather == "Sunny" -> "go for a walk"
else -> "Stay home and read."
if else->
whatShouldIDoToday("sad")
}

@FaizTeleha
Copy link

Create a program that suggests an activity based on various parameters.

  1. Start in a new file with a main function.
  2. From main(), create a function, whatShouldIDoToday().
  3. Let the function have three parameters.
    mood: a required string parameter
    weather: a string parameter that defaults to "sunny"
    temperature: an Integer parameter that defaults to 24 (Celsius).
  4. Use a when construct to return some activities based on combinations of conditions. For example:
    mood == "happy" && weather == "Sunny" -> "go for a walk"
    else -> "Stay home and read."
  5. Copy/paste your finished function into REPL, and call it with combinations of arguments. For example:
    whatShouldIDoToday("sad")

Stay home and read.

fun describe(obj: Any): String =when(mood == "happy" && weather == "Sunny" -> "go for a walk")
{
mood -> "happy"
"sunny" -> "Go for a walk"
weather -> "Stay home and read."
else -> whatShouldIDoToday("Sad")
}
fun main() {
println(describe(happy))
println(describe("sunny"))
println(describe(weather))
println(describe("WhatShouldIDoToday"))

}

//The output is //
Go for a walk
Stay home and read.
Sad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment