Reputation: 485
I am trying to make an application that takes the location and sends that data, everything was fine till 10 min ago. Now I got an error and I couldn't find what it is. Any help, thank you...
Some of my codes below...
...
...
Location mCurrentLocation;
private final static int
CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
LocationClient mLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
/*
* Create a new location client, using the enclosing class to
* handle callbacks.
*/
mLocationClient = new LocationClient(this, this, this);
}
...
...
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
mCurrentLocation = mLocationClient.getLastLocation();
tv.setText("slm"+mCurrentLocation.getLatitude());
}
And the error I get is ...
09-19 10:58:10.320: E/AndroidRuntime(1542): FATAL EXCEPTION: main
09-19 10:58:10.320: E/AndroidRuntime(1542): java.lang.NullPointerException
09-19 10:58:10.320: E/AndroidRuntime(1542): at com.example.testapplication.MainActivity.onClick(MainActivity.java:186)
09-19 10:58:10.320: E/AndroidRuntime(1542): at android.view.View.performClick(View.java:2538)
09-19 10:58:10.320: E/AndroidRuntime(1542): at android.view.View$PerformClick.run(View.java:9152)
09-19 10:58:10.320: E/AndroidRuntime(1542): at android.os.Handler.handleCallback(Handler.java:587)
09-19 10:58:10.320: E/AndroidRuntime(1542): at android.os.Handler.dispatchMessage(Handler.java:92)
09-19 10:58:10.320: E/AndroidRuntime(1542): at android.os.Looper.loop(Looper.java:130)
09-19 10:58:10.320: E/AndroidRuntime(1542): at android.app.ActivityThread.main(ActivityThread.java:3693)
09-19 10:58:10.320: E/AndroidRuntime(1542): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 10:58:10.320: E/AndroidRuntime(1542): at java.lang.reflect.Method.invoke(Method.java:507)
09-19 10:58:10.320: E/AndroidRuntime(1542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
09-19 10:58:10.320: E/AndroidRuntime(1542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
09-19 10:58:10.320: E/AndroidRuntime(1542): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 1221
Reputation: 485
Thank you guys for your help activating Google's location service solved my problem.
Upvotes: 1
Reputation: 12943
Check for null before you set the locations latitude in the TextView. You can't be sure that getLastLocation()
returns a valid location Object immediatly because it's a non-blocking call.
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
mCurrentLocation = mLocationClient.getLastLocation();
if(mCurrentLocation != null)
tv.setText("slm"+mCurrentLocation.getLatitude());
}
Upvotes: 1