0

Ok, so I am creating an app that uses Google Services (Location Services) to find the user's current location. I am getting an error when I go from my main activity to my next activity and I don't really know why. I had it working a while ago, but I changed something (that I can't remember) and now it won't work. Basically I click the start button on the main screen and then my program shuts down. The activity that is started by the start button, is suppposed to obtain the user's current location, then proceed to find the distances between the user's current location and 5 set locations. Then it processed these distances.

Here is my logCat:(I am really sorry but I just joined and I can't add pictures)

E/MoreInfoHPW_ViewGroup(20140): Parent view is not a TextView E/MoreInfoHPW_ViewGroup(20140): Parent view is not a TextView E/AndroidRuntime(20140): FATAL EXCEPTION: main E/AndroidRuntime(20140): Process: com.emma.unsaltedsights, PID: 20140 E/AndroidRuntime(20140): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emma.unsaltedsights/com.emma.unsaltedsights.PickLocation}: java.lang.NullPointerException

E/AndroidRuntime(20140):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)

E/AndroidRuntime(20140):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)

E/AndroidRuntime(20140):at android.app.ActivityThread.access$800(ActivityThread.java:157) E/AndroidRuntime(20140):at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)

E/AndroidRuntime(20140):at android.os.Handler.dispatchMessage(Handler.java:102) E/AndroidRuntime(20140):at android.os.Looper.loop(Looper.java:157) E/AndroidRuntime(20140): at android.app.ActivityThread.main(ActivityThread.java:5293) E/AndroidRuntime(20140):at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(20140):at java.lang.reflect.Method.invoke(Method.java:515) E/AndroidRuntime(20140):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1259)

E/AndroidRuntime(20140):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) E/AndroidRuntime(20140):at dalvik.system.NativeStart.main(Native Method) E/AndroidRuntime(20140): Caused by: java.lang.NullPointerException E/AndroidRuntime(20140): at com.emma.unsaltedsights.PickLocation.findingDistance(PickLocation.java:921)

E/AndroidRuntime(20140):at com.emma.unsaltedsights.PickLocation.onCreate(PickLocation.java:59) E/AndroidRuntime(20140):at android.app.Activity.performCreate(Activity.java:5389) E/AndroidRuntime(20140):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)

E/AndroidRuntime(20140):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)

E/AndroidRuntime(20140): ... 11 more

Here is my MainActivity Class (start screen):

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
public void launchApp(View view){
    Intent intent = new Intent(this, PickLocation.class);
    startActivity(intent);
)

}

Here is my PickLocation Class (daughter activity):

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

        if(isGooglePlay()){
            setContentView(R.layout.pick_location);
            setUp();
            findingDistance();
            setButtonsText(distances);          
        }       
    }

private void setUp(){

        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        provider = lm.getBestProvider(new Criteria(), true);
        if(provider == null ){
            onProviderDisabled(provider);
        }
        //get current location
        currentloc = lm.getLastKnownLocation(provider);
        if(currentloc != null){
            onLocationChanged(currentloc);  
        }
}

public void findingDistance(){


        Location jis = createLocation("flp",-6.284172,106.789093); //double
        Location lippo = createLocation("flp",-6.260516,106.811823); 
        Location senayan = createLocation("flp",-6.227877,106.797661);
        Location gandaria = createLocation("flp",-6.245164, 106.782597);
        Location ranch = createLocation("flp",-6.255405,106.811500);

        disJ = currentloc.distanceTo(jis); //float

        disL = currentloc.distanceTo(lippo);

        disS = currentloc.distanceTo(senayan); 

        disG = currentloc.distanceTo(gandaria);

        disR = currentloc.distanceTo(ranch); 

        intdisJ = (int) disJ;
        intdisL = (int) disL;
        intdisS = (int) disS;
        intdisG = (int) disG;
        intdisR = (int) disR;

        distances = new Integer[5];
        distances[0] = intdisJ;
        distances[1] = intdisL;
        distances[2] = intdisS;
        distances[3] = intdisG;
        distances[4] = intdisR;

    }

private boolean isGooglePlay()  { // Services Connected
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if(status == ConnectionResult.SUCCESS){
            return(true);
        }else{
            ((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
        }
            return(false);
    }

@Override
    public void onLocationChanged(Location location) {//I think this is the error source
        currentloc = location;

    }

I am a total noob with android, this is actually my first app, so please be thorough with the answers.

1 Answers1

0

I had the same problem and just managed to fix it.

Mine started to work after I imported the google-play-services_lib folder, the video guy shows you how import it around the 10 min mark: https://www.youtube.com/watch?v=awX5T-EwLPc

  • Links can be helpful as supplemental information, but [link-only answers are strongly discouraged](http://meta.stackexchange.com/a/8259/228805). Please include a summary of the linked information that's relevant to the question, and explain how it resolves the issue. – Adi Inbar Apr 09 '14 at 23:20