Skip to content

Instantly share code, notes, and snippets.

@twhetzel
Forked from udacityandroid/MainActivity.java
Last active June 28, 2021 09:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save twhetzel/40d5a3307da03baf1fa37428cec73e5d to your computer and use it in GitHub Desktop.
Save twhetzel/40d5a3307da03baf1fa37428cec73e5d to your computer and use it in GitHub Desktop.
Copy and paste this java code into the the JustJava app's MainActivity.java file
// Add your package below
/* 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);
}
}
@MohammedOsama717
Copy link

MohammedOsama717 commented Jun 27, 2019 via email

@tmldoc
Copy link

tmldoc commented Jun 30, 2019

@Natarior
thank you! worked by changing it to ->
import androidx.appcompat.app.AppCompatActivity
didn't change anything else

only solution that worked for me

@aalabi
Copy link

aalabi commented Jul 25, 2019

@Natarior
thank you! worked by changing it to ->
import androidx.appcompat.app.AppCompatActivity
didn't change anything else

only solution that worked for me

Yes especially when you are using the latest version of Android Studio

@didodz114
Copy link

hello friends i think that i have the working code

first the XML code

<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" />

the MainActivity.java code

package com.example.android.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.
*/
public class MainActivity extends AppCompatActivity {

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(10);
}
/**

 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);
}

}

AndroidManifest.xml code

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

it showed work using these firends good luck :)

@theatosam
Copy link

Thanks @Natarior, I finally run the app the successfully by replacing android.support.v7.app.AppCompatActivity; with import androidx.appcompat.app.AppCompatActivity;

@arabindanarayandas
Copy link

USE THE BELOW FOR activity_main.xml

<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" />

NOW USE THE BELOW FOR MainActivity.java

package com.example.android.justjava.;

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

/**

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

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);
}

}

@shahzadansari
Copy link

I had the same error. I replaced the line of code, 'import android.support.v7.app.AppCompatActivity;' in line 4 or 5 with 'import androidx.appcompat.app.AppCompatActivity'. All the 6 errors i had eventually vanished including the one about "Cannot resolve v7". i'm still not totally certain that the changes are executable but it is worth a try!!

Thank you Natarior, It worked!

@ashwanisng
Copy link

error: cannot find symbol variable quantity_text_view
i have this error please help.

@twhetzel
Copy link
Author

@ashwanisng - two solutions I've seen for the error "error: cannot find symbol variable quantity_text_view" are to:

  • clean and rebuild the project
  • make sure the package name in the manifest.xml is the same as your package name and not an example package name

@shahzadansari
Copy link

shahzadansari commented Feb 15, 2020 via email

@shahzadansari
Copy link

shahzadansari commented Feb 15, 2020 via email

@jasneetsinghwahan
Copy link

I have an ERROR : android.support.v7.app.AppCompatActivity
It says Cannot Resolve V7
ERROR at Line 4
Compat

import android.support.v7.app.AppCompatActivity; should be replaced with
import androidx.appcompat.app.AppCompatActivity;

@PranayRudra-3107
Copy link

Thanks bro, Resolved already ,‘;’ semicolon was missing.

Appreciate your assist
On Thu, Jun 27, 2019 at 4:06 PM Natarior @.***> wrote:
Share your last error.I will be happy to help.

On Wed, 26 Jun 2019 at 06:42, MohammedOsama717 @.***>
wrote:

Thank you Natarior After I replace 'import
android.support.v7.app.AppCompatActivity;' in line 4 or 5 with 'import
androidx.appcompat.app.AppCompatActivity' the errors reduced from 6 to 1
error only.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<
https://gist.github.com/40d5a3307da03baf1fa37428cec73e5d?email_source=notifications&email_token=AMMP6IFTUHLONUQ3DUZE6OTP4L6VPA5CNFSM4HWRBMS2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFUJVC#gistcomment-2954065
,
or mute the thread
<
https://github.com/notifications/unsubscribe-auth/AMMP6IHD7DLGU6GGBUXCIQ3P4L6VPANCNFSM4HWRBMSQ

.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://gist.github.com/40d5a3307da03baf1fa37428cec73e5d?email_source=notifications&email_token=AMOPYIBDJLKTEHBGD2OBY53P4SUNXA5CNFSM4HWRBMS2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFUMRE#gistcomment-2955538,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AMOPYIDMIA56OVC3GWSMDV3P4SUNXANCNFSM4HWRBMSQ
.

--

Regards
Mohammed Osama
IT Engineer

Mobile No. *+*971 52 758 1781
WhatsApp: *+*971 56 178 9919


what did you do actually I didn't get you, how did you resolve this!!!!

@OseyereProg
Copy link

I had the same error. I replaced the line of code, 'import android.support.v7.app.AppCompatActivity;' in line 4 or 5 with 'import androidx.appcompat.app.AppCompatActivity'. All the 6 errors i had eventually vanished including the one about "Cannot resolve v7". i'm still not totally certain that the changes are executable but it is worth a try!!

Thanks Ikenna it worked like a gem

@mohammednamoora
Copy link

Hello
I have this problem
Please help, knowing that I have the latest version of Android Studio
pro

@shahzadansari
Copy link

shahzadansari commented May 24, 2020 via email

@zakaria3698
Copy link

thank you very much is work thes code 🥇 :-)

package com.example.android.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.
*/
public class MainActivity extends AppCompatActivity {

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(10);
}
/**

 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);
}

}

@Islam3li
Copy link

thank u so so so so much worked for me
just add ur package from AndroidManifest.xml but change from:

package="com.example.justjavabyjava">

it to be like that :

package com.example.justjavabyjava;

Untitled

then import the class :

image

and that is it fix the errors to me i hope that to help u out :)

@Psytarry
Copy link

This was what worked for me;

  1. under "activity_main.xml"
    Add
    android:id="@+id/quantity_text_view" to TextView 0
2. Ensure that the package name in "AndroidManifestxml" is the same with "MainActivity.java

3. Under MainAcitivity.java, change "import android.support.v7.app.AppCompatActivity;" to
"import android.appcompat.app.AppCompatActivity;"

@singhPankaj478
Copy link

@udacityandroid PLease update the errors in the Course ,it is too much time consuming ! OR don't mention that this course is free, it is just waste of time !

@m-kumar-10
Copy link

  • 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.>>>>Its important step
  • then in java >>import android.support.v7.app.AppCompatActivity;<< this import line of code has been updated to another one ,
  • so delete this.& drag your mouse to AppCompatActivity text of this line of code. >>public class MainActivity extends ### AppCompatActivity
  • now press alt+enter
  • and select import.
  • the issue will be fixed& error will be gone.
  • `/**
  • 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.justjava;
import android.os.Bundle;
//import android.support.v7.app.AppCompatActivity;
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.quantity_text_view);
      quantityTextView.setText("" + number);
      }
      }`

@mazx-hue
Copy link

image
more than 7 hours and i cannot solve this errors its really Get me tired

@mazx-hue
Copy link

how can i solve them any help
image
where are they

@mazx-hue
Copy link

image

@m-kumar-10
Copy link

image
more than 7 hours and i cannot solve this errors its really Get me tired

solvd

@priyankaaa532002
Copy link

I had the same error. I replaced the line of code, 'import android.support.v7.app.AppCompatActivity;' in line 4 or 5 with 'import androidx.appcompat.app.AppCompatActivity'. All the 6 errors i had eventually vanished including the one about "Cannot resolve v7". i'm still not totally certain that the changes are executable but it is worth a try!!

thankyou,
works wor me

@priyankaaa532002
Copy link

This was what worked for me;

  1. under "activity_main.xml"
    Add
    android:id="@+id/quantity_text_view" to TextView 0
2. Ensure that the package name in "AndroidManifestxml" is the same with "MainActivity.java

3. Under MainAcitivity.java, change "import android.support.v7.app.AppCompatActivity;" to
"import android.appcompat.app.AppCompatActivity;"

thanksss, works

@Hayatkd
Copy link

Hayatkd commented Mar 19, 2021

image

image

I tried correcting the code using previous comments but nothing worked for me. please help

@m-kumar-10
Copy link

image

image

I tried correcting the code using previous comments but nothing worked for me. please help

asd

@nitishmwn4
Copy link

nitishmwn4 commented Jun 28, 2021

2021-06-28
can't resolve activity_main
help

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