Você está na página 1de 3

Stack Overflow

Questions

Tags

Users

sign up
Badges

Unanswered

log in

Ask

Read this post in our app!

Trying to display longitude and latitude in textViews


0

android

android-location

I'm trying to display both the longitude and latitude in seperate textviews after the user presses
the get location button. All code seems ok to me but am getting null pointer exceptions. I do not
want to do anything when the location is changed only when the user presses the button.
package com.seanhannon.fyp;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FYPActivity extends Activity {
public Button getLocation;
public TextView LongCoord;
public TextView LatCoord;
public double longitude;
public double latitude;

The XML file is the usual with permissions for fine and coarse location etc.
Here is the Logcat output

FATAL EXCEPTION: main


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seanhannon.fyp/com.seanhann
on.fyp.FYPActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.seanhannon.fyp.FYPActivity.onCreate(FYPActivity.java:33)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more

share

improve this question


Droid_Interceptor
220 1 6 20

order by votes

1 Answer

Asked
Dec 28 '11 at 17:21

This is your peace of code


getLocation = (Button)getLocation.findViewById(R.id.GetLocation);
LongCoord = (TextView)LongCoord.findViewById(R.id.longCoord);
LatCoord = (TextView)LatCoord.findViewById(R.id.LatCoord);

change it with below code


getLocation = (Button)findViewById(R.id.GetLocation);
LongCoord = (TextView)findViewById(R.id.longCoord);
LatCoord = (TextView)findViewById(R.id.LatCoord);

it will remove null pointer exception...

share

improve this answer


AAnkit
13.5k

5 33 46

Answered
Dec 28 '11 at 17:29

Thanks a mill, been few months since did any android so basics are gone totally Droid_Interceptor Dec 28
'11 at 17:39
most welcome,if it solved your problem, accept the answer. AAnkit Dec 28 '11 at 17:54

add a comment

Your Answer

log in

or
Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Post Your Answer

Você também pode gostar