views:

97

answers:

1

Hi guys,

I got this class, and nothing inside the onCreate is happening? Does it not automatically get called upon initialization?

Code is below, Thanks alot! - Micheal

package com.michealbach.livedrops;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;

public class AnyClass extends Activity {

double latitude = 0; 
double longitude = 0;

String addressString = "Address Initialized";
String postalString = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    postalString = "Why doesn't this String change happen at all?";

  }


public double getLongitude() {
    return longitude;
}

public double getLatitude() {
    return latitude;
}

public String getAddress() {
    return addressString;
}
public String getPostal() {
    return postalString;
}
}

Doing this results in a "application has stopped unexpectedly. Please try again". If I initialize the string to some set of characters instead of null, it'll just stay that way. Should it not do what is inside the onCreate() and make that change?

+1  A: 

onLocationChanged() is just a class method in your program, it's not conected to anything. You have to do


private final LocationListener locationListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {

             // Do stuff

            }


        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

    };


And then in your onCreate()


LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
cr = new Criteria();
cr.setPowerRequirement(Criteria.POWER_LOW);
cr.setAccuracy(Criteria.ACCURACY_FINE);
provider = lm.getBestProvider(cr,true);
lm.requestLocationUpdates(provider, minTIME, minDISTANCE, locationListener );


Falmarri
Thank you Falmarri, but it doesn't apply to my question. The "// Do stuff" that I'm doing is updateWithNewLocation(Location location) method. My question specifically refers to that problem that nothing at all is happening in the onCreate. The first line is a change to the String postalString - but that isn't reflected when I print out postalString. I assume nothing below that is happening either.
Micheal
My answer answers your question. Nothing is happening because your onLocationChanged method is a method of GetLocation, not LocationListener. In your code, why do you think anything should happen after onCreate()? What exactly do you think is calling any of your other methods?
Falmarri
Just replace `//Do stuff` with `updateWithNewLocation(location)`
Falmarri
Thank you Falmarri for taking a look at this. Let me repost some source to make my question clear.
Micheal
I'm concerned with why my onCreate is not called even a single time. Is there anything special I have to do to get it to happen?
Micheal
Oh, heh. You forgot to do `setContentView(R.layout.main)` or whatever your XML file is. You HAVE to call setContentView() before you can manipulate a view. You should really go through the Hello Android tutorial on http://developer.android.com/resources/tutorials/hello-world.html . Also, when you post a question that crashes, you really have to post the stacktrace in the logcat for us to have any idea what's going on
Falmarri
Looks like I need to start over. I'm not interested in showing this data specifically. I need it for another process (weather and another URL call). Looks like Activity shouldn't be extended here, but I'm getting all errors when I remove it. Gosh, all I want is the data - but there are a lot of hoops to jump through. haha.
Micheal
I'm trying to initialize a "GetLocation location = new GetLocation();" and then in my "void draw(Canvas c)" I've got c.drawText(location.getPostal(), 10, 200, paint);. But it'll never be anything but "Postal Initialized". Haha - I can't believe I'm stuck on this for so long.
Micheal