-
-
Save udacityandroid/a0b6ecf92ea1c3849ed4 to your computer and use it in GitHub Desktop.
| 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); | |
| } | |
| } |
display3(""And look! Box 3!"");
to look like: "And look! Box 3!"
put this line in : import android.widget.TextView;
No error Good
it works after changing the package name.
thank you it work for me ``
Do not delete your first line of code !
Fixed after sync with gradle
I did as Pommereh said and the errors have been solved
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 :) )
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.
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
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!
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:)
Sravary's answer is the best one. This also works for the Just Java example.
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
It would be nice, if they update those codes from time to time.
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;
@afarisamuel6 your solution was very nice thanks
nice learning is fun
if u are using android in 2020 kindly turn on auto import
https://stackoverflow.com/questions/16615038/what-is-the-shortcut-to-auto-import-all-in-android-studio
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);
}
}`
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:
- You didn't integrate AndroidX properly but you use AndroidX imports.
- 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.)
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.
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
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
Thank you!


On my java Mainactivity the TextVier is red!!