how to fix this cannot resovle this symbol? - java

I'm new to android and i have been learning android form udacity.
in one of their lesson they asked students to copy code from git hub , i did so but when I pasted the code an error popped saying cannot symbol number format.please help me to resolve this and please give answer in detail it's a request. thank you.
..........................................................................................................................................................
link of github :- https://gist.github.com/anonymous/fa134c55a4a43e8d6004
...............................................................................java code which i've written:-
package com.example.android.justjava;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.;
/**
* 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(18 * 3 + 4 / (2+2) -1);
displayPrice(2*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);
}
/**
* 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().format(number));
}
}
.............................................................................

Try add import java.text.NumberFormat;
In such way build system imports class you required.
Documentation describes it in details

Related

Java - Web Scraping In Android Studio [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 2 years ago.
I'm trying to web scrape from a website https://www.worldometers.info/coronavirus/ and turns that data to form an app but the data is not actually printing I don't the reason but whenever I click the button in an android emulator it just crashes instantly !!
I have 3 textView in the app and a button so whenever I click the button it should show up the data in the textView !!
textView have ids of TotalCases / TotalDeaths / TotalRecovered
button has an id of the button
Here is what I have I have done
package com.example.coronaupdate;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Button btn;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
compare();
}
});
}
public void compare()
{
final TextView totalCases;
final TextView totalDeaths;
final TextView totalRecovered;
totalCases = (TextView) findViewById(R.id.TotalCases);
totalDeaths = (TextView) findViewById(R.id.TotalDeaths);
totalRecovered = (TextView) findViewById(R.id.TotalRecovered);
try {
Document doc = Jsoup.connect("https://www.worldometers.info/coronavirus/").userAgent("mozilla/17.0").get();
Elements temp = doc.select("div.maincounter-number");
Element totalcase = temp.get(0);
String cases = totalcase.select("div.maincounter-number span").text();
totalCases.setText(cases);
Element totaldeaths = temp.get(1);
String deaths = totaldeaths.select("div.maincounter-number span").text();
totalDeaths.setText(deaths);
Element totalrecovered = temp.get(2);
String recovered = totalrecovered.select("div.maincounter-number span").text();
totalRecovered.setText(recovered);
/* for(Element totalCase:temp)
{
String cases = totalCase.select("div.maincounter-number span").text();
System.out.println("" + cases);
*//*i++;
System.out.println(i + "" + totalCase.getElementsByTag("span"));*//*
}*/
}
catch (IOException e){
e.printStackTrace();
}
}
}
image of the app view
Image of the app view
If you could post the Error you're getting it would be easier to see what's going on, but I have two assumptions of what's wrong.
First you should check if yout Manifest is asking for INTERNET access permission. If it isn't, you should include it.
Second: Always when making requests to the internet in Android you should use either AsyncTasks or at least open a separate thread manually. That's because internet calls are asynchronous by definition, and if you block the main execution of your App to wait for a response the UI is gonna freeze or crash. So perhaps you should extract the logic of your compare() function to a separate class that inherits from AsyncTask and place it into the doInBackground() method.

method Activity.setContentView(int) is not applicable

i m programming an android program of a simple hello world in netbeans but it returns me this error...what should i do in order to rid of this error ...i m new and exited to jump into this .
here is my code...
enter code here
package com.google.haha;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
/**
*
* #author abc
*/
public class NewActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
TextView text=new TextView(this);
text.setText("hello");
setContentView(this);
}
}
When you do
setContentView(this);
this refers to the current class instance, not the TextView you just created.
Try:
setContentView(text);
Like "metter" said, the setContentView(VIEW) method expects an object of the View class (TextView, ListView, etc). So you have to pass a View element in there...

Asking name and storing it?

I have huge problem. Have searching answer from many places but I do not find answer to question.
I have 2 classes in java. One is "main" and other is "menu"
On main, there is editText where person can type name and button ok.
When you press ok, I want to specific thing happen. That is where I need help. I am newcomer in Android.
I want that in other class, where will be main application and stuff, the entered name would be displayed. For example "Welcome " + name
I have tried many ways but I do not get it to work. So I want it to get one of 2 possible ways.
Set string in class 1 and then when it goes class 2, then it imports the string from class 1 to class 2 so I can use it.
I set string in class 2 and then in Class 1 I change the string in class 2, so the main 'data' string is actually in class 2, where I will continue using it if needed!
I have searched it from many places, used google and this database, but haven't found answer.
My codes are really nothing much, so no point pasting them here :).
Thanks for the help!
edit:// Ok, here are some codes then :)
package viimane.voimalus.incule;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ViimaneVoimalusActivity extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button oknupp = (Button) findViewById(R.id.nimiOK);
oknupp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("viimane.voimalus.in" +cule.REALMENU"));
}
});
}
}
package viimane.voimalus.incule;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class RealMenu extends Activity {
EditText nimesisestus;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.peamenu);
}
}
You didn't show any code at all, so the answer won't be too detailed.
You can pass a String in the intent used to start the second activity, using putExtra method. In the second activity you may get the string by calling getStringExtra(). If you don't know much about starting activity and intents, there are many resources on the web, for example - this one.
To receive text input use EditText and to put text on the screen (that the user can't cange) use TextView.
Those are starting points. Next time I hope the question will be more focused.
Pass the string as parameter to the constructor of the menu class like this:
menu m = new menu(mystring);
then in the constructor save the atribute to your class
private String mystring;
menu(String mystring)
{
this.mystring = mystring;
}

why wont my Gyroscope app in android fire off any events?

alright so I'm new to programming for android, and I think I did something wrong, but I don't know what. I've looked at 3 different tutorials and my code seems to look like theirs. Can anyone tell me what I'm doing wrong? here is my src (altered from a android nehe tutorial).
package cypri.games;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
/**
* The initial Android Activity, setting and initiating
* the OpenGL ES Renderer Class #see Lesson02.java
*
* #author Savas Ziplies (nea/INsanityDesign)
*/
public class DGearActivity extends Activity {
/** The OpenGL View */
private GLSurfaceView glSurface;
DGear dGear = new DGear();
private static final String TAG = "DEBUG";
SensorManager sensorManager;
private final SensorEventListener sensorListener = new SensorEventListener(){
public void onSensorChanged(SensorEvent se) {
if (se.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
dGear.playerX = se.values[0];
Log.v(TAG, "se.values[0] =" + se.values[0]);
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
/**
* Initiate the OpenGL View and set our own
* Renderer (#see Lesson02.java)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
//Create an Instance with this Activity
glSurface = new GLSurfaceView(this);
//Set our own Renderer
glSurface.setRenderer(dGear);
//Set the GLSurface as View to this Activity
setContentView(glSurface);
}
/**
* Remember to resume the glSurface
*/
#Override
protected void onResume() {
Log.v(TAG, "or");
super.onResume();
glSurface.onResume();
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
}
/**
* Also pause the glSurface
*/
#Override
protected void onPause() {
super.onPause();
glSurface.onPause();
Log.v(TAG, "op");
sensorManager.unregisterListener(sensorListener);
}
}
Are you running this in the emulator or on an actual device?
If you're running it on an actual device are you sure it has a gyroscope? There are lots of different sensor types and the gyroscope is only one of them. It could very well be one of the others.
Instead of only writing to the log if it's a gyroscope type, try writing the name of the se.sensor when that event fires. That way you'll at least know the event is firing.

Android: What does this warning mean?

I have a button with a few sound effects. When I click the button, nothing happens. No sound is played, but no force close either. The Debugger is giving me this warning:
"SoundPool - Sample 1 not READY"
"SoundPool - Sample 2 not READY" etc...
Le Code:
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Soundboard extends Activity{
private SoundManager mSoundManager;
private int next = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(0,R.raw.finn_algebraic);
mSoundManager.addSound(1,R.raw.finn_allaboardthekuckletrain);
mSoundManager.addSound(2,R.raw.finn_iminmyelement);
mSoundManager.addSound(3,R.raw.finn_shmowzow);
Button SoundButton=(Button)findViewById(R.id.Button1);
SoundButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
mSoundManager.playSound(next);
next++;
if(next>3)
next=0;
}
});
}
}
Sounds take a while to load. You can use a SoundPool.OnLoadCompleteListener to find out when they're ready. (Android 2.2+ only)

Categories