Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created May 25, 2015 22:21
Show Gist options
  • Select an option

  • Save udacityandroid/a0b6ecf92ea1c3849ed4 to your computer and use it in GitHub Desktop.

Select an option

Save udacityandroid/a0b6ecf92ea1c3849ed4 to your computer and use it in GitHub Desktop.
Android Development for Beginners : Starter Gist for Java
package com.example.android.practiceset2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// PASTE CODE YOU WANT TO TEST HERE
}
/**
* Display methods that allow the text to appear on the screen. Don't worry if you don't know
* how these work yet. We'll be covering them in lesson 3.
*/
public void display(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text);
}
public void display(int text) {
TextView t = (TextView) findViewById(R.id.display_text_view);
t.setText(text + "");
}
public void display1(String text) {
display(text);
}
public void display2(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view_2);
t.setText(text);
}
public void display3(String text) {
TextView t = (TextView) findViewById(R.id.display_text_view_3);
t.setText(text);
}
}
@MBasuony

Copy link
Copy Markdown

On my java Mainactivity the TextVier is red!!

@rahulkhatri19

Copy link
Copy Markdown

display3(""And look! Box 3!"");
to look like: "And look! Box 3!"

@rahulkhatri19

Copy link
Copy Markdown

put this line in : import android.widget.TextView;

@Htewech7

Copy link
Copy Markdown

No error Good

@mohamedsaleh1984

Copy link
Copy Markdown

it works after changing the package name.

@Alihani0almamary

Copy link
Copy Markdown

This is how the code is written

3

@Soufiane-Aou

Copy link
Copy Markdown

thank you it work for me ``

@skrd-18

skrd-18 commented Dec 23, 2018

Copy link
Copy Markdown

Do not delete your first line of code !

@rijkerd

rijkerd commented Jan 12, 2019

Copy link
Copy Markdown

Fixed after sync with gradle

@xSurpassionx

xSurpassionx commented Jan 24, 2019

Copy link
Copy Markdown

I was able to get it work by changing the a few things such as tools:context, and changing the layout from constraint to Linear Vertical. Here is the code .

image

@kabosaif

Copy link
Copy Markdown

I did as Pommereh said and the errors have been solved

@xMagicXs

xMagicXs commented Jul 2, 2019

Copy link
Copy Markdown

for those with android newer version ignore this line import android.support.v7.app.AppCompatActivity;
and copy from line 12 also paste it there and copy import android.widget.TextView; as well then all should be fine
( the TextView will be red but that because you did not create them in xml yet also this is RelativeLayout and new android is going to generate constraint is why to copy/paste from and to line 12 then you just adapt your xml and you are back in game :) )

@Kelly5978

Copy link
Copy Markdown

Okay, why in the world are these examples still listed in the course if they do not work with the newer version? I've now spent way too much time trying to fix this problem....when this is SUPPOSED TO BE A BEGINNER COURSE! I'm guessing no one checks these messages anymore, so I suppose I'm on my own. Very disappointed.

@eelberg

eelberg commented Oct 12, 2019

Copy link
Copy Markdown

import android.widget.TextView

Can you please post the code as it should be? I was not able to understand the instructions "copy from line 12 also paste it there and copy import android.widget.TextView; as well "

Thanks

@kabosaif

kabosaif commented Oct 12, 2019 via email

Copy link
Copy Markdown

@sravary

sravary commented Oct 31, 2019

Copy link
Copy Markdown

This is what I did to make this work....
In MainActivity - line 4 - replace: import android.support.v7.app.AppCompatActivity;
with the following code: import androidx.appcompat.app.AppCompatActivity;

Hopefully that should do the trick!

@LaszloLajosT

LaszloLajosT commented Nov 10, 2019

Copy link
Copy Markdown

Yes, that's right.
We use never version, so we need updated AppCombatActivity.

If you want to fix these kind of errors in the future then do the next steps(without copy and paste process):
For example you have error (red line here):

public class MainActivity extends AppCompatActivity {

Select AppCompatActivity from the line and use shortcut: (in windows) ALT + ENTER.
A little window shows where you can use "Import..." . Select it and voila, it will import the right package.

I already post two pictures of my result where I copied the XML file. Here is a link to my comment.
https://gist.github.com/udacityandroid/332432b53a13e5c55c1c#gistcomment-3079156

I thought you will appreciate:)

@jasonallenh

Copy link
Copy Markdown

Sravary's answer is the best one. This also works for the Just Java example.

@afarisamuel6

Copy link
Copy Markdown

If you are using android studio currently in 2020 kindly take note of this to help resolve the AppCompatActivity issue

import android.support.v7.app.AppCompatActivity;
kindly replace this line of code with
import androidx.appcompat.app.AppCompatActivity;

and you can ran your program successfully without any challenge

Screenshot 2020-06-02 at 6 21 26 AM

@humandroid01

Copy link
Copy Markdown

It would be nice, if they update those codes from time to time.

@SagaMB

SagaMB commented Sep 20, 2020

Copy link
Copy Markdown

Guys don't worry, just try a solution.

In activity_main.xml file

Replace this: tools:context="com.example.android.practiceset2.MainActivity">

To this: tools:context="com.example.practiceset2.MainActivity">

and in MainActivity.java file

Replace this: package com.example.android.practiceset2;

To this: package com.example.practiceset2;

@chetan081

Copy link
Copy Markdown

@afarisamuel6 your solution was very nice thanks

ghost commented Oct 9, 2020

Copy link
Copy Markdown

nice learning is fun

@Harshkumar77

Copy link
Copy Markdown

@Andstddev

Copy link
Copy Markdown

Hi everyone,
I tried running the code but it keeps stopping. It keeps stopping and I tried to debug but I can't really understand whats going on. Thanks in advance.

`package com.example.practiceset2;

import android.os.Bundle;
//import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.android.practiceset2.R;

//import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // PASTE CODE YOU WANT TO TEST HERE

    }
}

/**
 * Display methods that allow the text to appear on the screen. Don't worry if you don't know
 * how these work yet. We'll be covering them in lesson 3.
 */

public void display(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text);
}

public void display(int text) {
    TextView t = (TextView) findViewById(R.id.display_text_view);
    t.setText(text + "");
}

public void display1(String text) {
    display(text);
}

public void display2(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view_2);
    t.setText(text);
}

public void display3(String text) {
    TextView t = (TextView) findViewById(R.id.display_text_view_3);
    t.setText(text);
}

}`

@LaszloLajosT

Copy link
Copy Markdown

Hi @Andstddev

It's hard to figure out what stops your app because you share just one piece of the code(XML file needed), but I try to guess:

  1. You didn't integrate AndroidX properly but you use AndroidX imports.
  2. TextView ID doesn't match from XML to Java file.

I shared a link below where you can learn how to share your whole project on Github.( So I can have a look and tell you where was the problem and why.)

How to submit your Android Studio project to Github

@Vaibhavi-13

Copy link
Copy Markdown

Sometimes you have to change the name from MainActivity.kt to MainActivity.java and then it works .
Also in newer versions
import android.support.v7.app.AppCompatActivity;
this line is not needed so erase it.

@Prospyl

Prospyl commented Dec 27, 2020

Copy link
Copy Markdown

Ensure your package name is having the correct name "package com.example.practiceset2;" (On MainActivity.java) and
tools:context="com.example.practiceset2.MainActivity" > in the activity_main.xml file.
Delete "import android.support.v7.app.AppCompatActivity;" if you are getting a whole list of error in your MainActivity.java

@JAYANTOCHOUDHURY

Copy link
Copy Markdown

In the first line the code sis not work for me-

package com.example.android.practiceset2;

I tried this instead-

package com.androidexample.practiceset2;

You have to write your project name which u are given instead of practiceset2

@Samdzaka

Samdzaka commented Jul 8, 2022

Copy link
Copy Markdown

Thank you!

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