• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Google Location Api Not working in My App

Sarmakin

Lurker
Nov 9, 2015
6
1
Hello Everyone

I am an Android Beginner.I am developing an app based on distributed location estimation algorithm using Fused Location API for Android.The program compiles without any errors but when I run it I get the App Name Unfortunately LocationApi has stopped working. I tried figuring it out but it's been over 10 days not able to figure it out yet.I am here with attaching three files : MainActivity.java manifest.xml and content_main.xml along with the logcat

MainActivity.java
Code:
package com.example.sxn8837.locationapi;



import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

importstatic com.example.sxn8837.locationapi.R.*;
importstatic com.example.sxn8837.locationapi.R.layout.activity_main;
importstatic com.google.android.gms.location.LocationServices.*;

publicclassMainActivityextendsActivityimplements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListener{

privatestaticfinalString TAG =MainActivity.class.getSimpleName();
privatestaticfinalint PLAY_SERVICES_RESOLUTION_REQUEST =1000;
privateLocation mLastLocation;
privateGoogleApiClient mGoogleApiClient;
privateboolean mRequestLocationUpdates =false;
privateLocationRequest mLocationRequest;
privatestaticint UPDATE_INTERVAL =10000;
privatestaticint FASTEST_INTERVAL =5000;
privatestaticint DISPLACEMENT =10;




privateTextView lbLocation;
privateButton btnStartLocationUpdates =(Button) findViewById(id.buttonLocationUpdates);
privateButton btnShowLocation =(Button) findViewById(id.buttonShowLocation);

[USER=1021285]@override[/USER]
protectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(activity_main);
lbLocation =(TextView) findViewById(id.lblLocation);
if(checkPlayServices()){
buildGoogleApiClient();
createLocationRequest();
}

btnShowLocation.setOnClickListener(newView.OnClickListener(){

publicvoid onClick(View v){

displayLocation();

}
});

btnStartLocationUpdates.setOnClickListener(newView.OnClickListener(){

publicvoid onClick(View v){

togglePeriodLocationUpdates();
}

});
}

protectedvoid onStart(){
super.onStart();
if(mGoogleApiClient !=null){
mGoogleApiClient.connect();
}
}

protectedvoid onResume(){
super.onResume();
checkPlayServices();
if(mGoogleApiClient.isConnected()&& mRequestLocationUpdates ){
startLocationUpdates();
}
}

protectedvoid onStop(){
super.onStop();
if(mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}

}

protectedvoid onPause(){
super.onPause();
stopLocationUpdates();
}

privatevoid displayLocation(){

mLastLocation =FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation !=null){
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
lbLocation.setText(latitude +","+longitude);
}
else{
lbLocation.setText("Couldn't get Location Updates");
}
}

privatevoid togglePeriodLocationUpdates(){
if(!mRequestLocationUpdates){
btnStartLocationUpdates.setText(getString(string.btn_stop_location_update));

mRequestLocationUpdates =true;

startLocationUpdates();
}

else{

btnStartLocationUpdates.setText(getString(string.btn_stop_location_update));

mRequestLocationUpdates =false;

stopLocationUpdates();


}
}

protectedsynchronizedvoid buildGoogleApiClient(){

mGoogleApiClient =newGoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.addApi(API).build();
}

protectedvoid createLocationRequest(){
mLocationRequest =newLocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}

privateboolean checkPlayServices(){
int resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

if(resultCode !=ConnectionResult.SUCCESS){

if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)){
GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICES_RESOLUTION_REQUEST).show();

}else{

Toast.makeText(getApplicationContext(),"This Device is not supported",Toast.LENGTH_LONG).show();
finish();
}
returnfalse;
}

returntrue;
}

protectedvoid startLocationUpdates(){
FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,(com.google.android.gms.location.LocationListener)this);
}

protectedvoid stopLocationUpdates (){
finalPendingResult<Status> statusPendingResult =FusedLocationApi.removeLocationUpdates(mGoogleApiClient,(com.google.android.gms.location.LocationListener)this);


}
[USER=1021285]@override[/USER]
publicboolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
returntrue;
}

[USER=1021285]@override[/USER]
publicboolean onOptionsItemSelected(MenuItem item){
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if(id == R.id.action_settings){
returntrue;
}

returnsuper.onOptionsItemSelected(item);
}

[USER=1021285]@override[/USER]
publicvoid onConnected(Bundle bundle){

displayLocation();

if(mRequestLocationUpdates){
startLocationUpdates();
}
}

[USER=1021285]@override[/USER]
publicvoid onConnectionSuspended(int i){
mGoogleApiClient.connect();
}

[USER=1021285]@override[/USER]
publicvoid onLocationChanged(Location location){

mLastLocation = location ;

Toast.makeText(getApplicationContext(),"Location Changed",Toast.LENGTH_SHORT).show();
displayLocation();

}

[USER=1021285]@override[/USER]
publicvoid onStatusChanged(String provider,int status,Bundle extras){

}

[USER=1021285]@override[/USER]
publicvoid onProviderEnabled(String provider){

}

[USER=1021285]@override[/USER]
publicvoid onProviderDisabled(String provider){

}

[USER=1021285]@override[/USER]
publicvoid onConnectionFailed(ConnectionResult connectionResult){
Log.i(TAG,"Connection Failed:"+ connectionResult.getErrorCode());
}
}

content_main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"
android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:eek:rientation="vertical"
android:layout_gravity="center_horizontal"
app:layout_behavior="[USER=696546]@String[/USER]/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"tools:context=".MainActivity">

<TextViewandroid:text="[USER=696546]@String[/USER]/lbl_you_are_at"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"/>

<TextView
android:id="@+id/lblLocation"
android:padding="15dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textStyle="bold"/>

<Button
android:id="@+id/buttonShowLocation"
android:padding="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="40dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="[USER=696546]@String[/USER]/btn_get_location"
android:textSize="16sp"
android:textStyle="bold"/>
>

<Button
android:id="@+id/buttonLocationUpdates"
android:padding="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="60dp"
android:background="@color/common_signin_btn_default_background"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="[USER=696546]@String[/USER]/btn_start_location_update"
android:textSize="16sp"
android:textStyle="bold"/>
>
</LinearLayout>

androidmanifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sxn8837.locationapi">
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
android:allowBackup="true"
android:icon="[USER=22138]@Mipmap[/USER]/ic_launcher"
android:label="[USER=696546]@String[/USER]/app_name"
android:supportsRtl="true"
android:theme="[USER=19691]@Style[/USER]/AppTheme">
<activity
android:name=".MainActivity"
android:label="[USER=696546]@String[/USER]/app_name"
android:theme="[USER=19691]@Style[/USER]/AppTheme.NoActionBar">

<meta-data
android:name="com.google.android.gms.version"
android:value="[USER=1111536]@integer[/USER]/google_play_services_version"
/>
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>

log cat
Code:
11-0116:21:03.4547779-7779/? D/dalvikvm:Not late-enabling CheckJNI(already on)
11-0116:21:04.7047779-7779/com.example.sxn8837.locationapi D/AndroidRuntime:Shutting down VM
11-0116:21:04.7047779-7779/com.example.sxn8837.locationapi W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb1a3eba8)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: FATAL EXCEPTION: main
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime:process: com.example.sxn8837.locationapi, PID:7779
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: java.lang.RuntimeException:Unable to instantiate activity ComponentInfo{com.example.sxn8837.locationapi/com.example.sxn8837.locationapi.MainActivity}: java.lang.NullPointerException
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(NativeMethod)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at dalvik.system.NativeStart.main(NativeMethod)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime:Causedby: java.lang.NullPointerException
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.Activity.findViewById(Activity.java:1884)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at com.example.sxn8837.locationapi.MainActivity.<init>(MainActivity.java:46)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.Class.newInstanceImpl(NativeMethod)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1208)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(NativeMethod)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at dalvik.system.NativeStart.main(NativeMethod)
 
Last edited:
Welcome to AF @Sarmakin :)
I would edit your post to put your code in code tags. It will make it easier for developers to read it and offer help and make it less likely you get nabbed by our filters. ;)


[code]
line 1...
line 2...
line x...
[/code]

Code:
line 1...
line 2...
line x...

Sorry about that.Have edited it now.Thanks for the suggestion. :) . I should have known better.
 
  • Like
Reactions: Unforgiven
Upvote 0
There's no indentation in the code. It's very difficult to read, but your stack trace tells you what's wrong. It's at line 46 in MainActivity, although the code you posted doesn't match up with the stack trace. What code is at line 46?

Code:
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime:Causedby: java.lang.NullPointerException
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.Activity.findViewById(Activity.java:1884)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at com.example.sxn8837.locationapi.MainActivity.<init>(MainActivity.java:46)
 
  • Like
Reactions: Unforgiven
Upvote 0
There's no indentation in the code. It's very difficult to read, but your stack trace tells you what's wrong. It's at line 46 in MainActivity, although the code you posted doesn't match up with the stack trace. What code is at line 46?

Code:
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime:Causedby: java.lang.NullPointerException
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at android.app.Activity.findViewById(Activity.java:1884)
11-0116:21:04.7347779-7779/com.example.sxn8837.locationapi E/AndroidRuntime: at com.example.sxn8837.locationapi.MainActivity.<init>(MainActivity.java:46)

Thanks for the reply and the suggestion.Sorry about the indentation.Will look into line 46 and post the correctly indented code.I had to copy it to word doc before posting here.Hence the problem.Will do it in a while and keep you posted.
 
Upvote 0
Thanks. I think I see your problem. This is wrong

Code:
privateButton btnStartLocationUpdates =(Button) findViewById(id.buttonLocationUpdates);
privateButton btnShowLocation =(Button) findViewById(id.buttonShowLocation);

Put this stuff in your onCreate method after calling setContentView

Thanks for the suggestion I tried it I am getting the same error in the emulator saying that Unfortunately LocationApi has stopped.

logcat
Code:
11-09 20:18:13.352 3645-3645/? D/dalvikvm: Not late-enabling CheckJNI (already on)
11-09 20:18:15.012 3645-3645/com.example.sxn8837.locationapi I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-09 20:18:15.022 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: VFY: unable to resolve virtual method 511: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-09 20:18:15.022 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-09 20:18:15.032 3645-3645/com.example.sxn8837.locationapi I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-09 20:18:15.032 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: VFY: unable to resolve virtual method 533: Landroid/content/res/TypedArray;.getType (I)I
11-09 20:18:15.032 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-09 20:18:15.312 3645-3645/com.example.sxn8837.locationapi I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
11-09 20:18:15.312 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: VFY: unable to resolve virtual method 438: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
11-09 20:18:15.312 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b
11-09 20:18:15.332 3645-3645/com.example.sxn8837.locationapi W/GooglePlayServicesUtil: Google Play services is missing.
11-09 20:18:15.502 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: GC_FOR_ALLOC freed 158K, 11% free 3301K/3692K, paused 83ms, total 86ms
11-09 20:18:16.002 3645-3645/com.example.sxn8837.locationapi D/dalvikvm: GC_FOR_ALLOC freed 3K, 11% free 3451K/3848K, paused 23ms, total 23ms
11-09 20:18:16.022 3645-3645/com.example.sxn8837.locationapi I/dalvikvm-heap: Grow heap (frag case) to 5.278MB for 1764376-byte allocation
11-09 20:18:16.052 3645-3654/com.example.sxn8837.locationapi D/dalvikvm: GC_FOR_ALLOC freed <1K, 8% free 5174K/5572K, paused 29ms, total 29ms
11-09 20:18:16.282 3645-3645/com.example.sxn8837.locationapi W/GooglePlayServicesUtil: Google Play services is missing.
11-09 20:18:16.332 3645-3645/com.example.sxn8837.locationapi D/AndroidRuntime: Shutting down VM
11-09 20:18:16.332 3645-3645/com.example.sxn8837.locationapi W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb1adbba8)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: FATAL EXCEPTION: main
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: Process: com.example.sxn8837.locationapi, PID: 3645
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {com.example.sxn8837.locationapi/com.example.sxn8837.locationapi.MainActivity}: java.lang.NullPointerException
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:  Caused by: java.lang.NullPointerException
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.example.sxn8837.locationapi.MainActivity.onResume(MainActivity.java:86)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.Activity.performResume(Activity.java:5310)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-09 20:18:16.342 3645-3645/com.example.sxn8837.locationapi E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

the formatted code of MainActivity.java is here

Code:
package com.example.sxn8837.locationapi;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton ;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import static com.example.sxn8837.locationapi.R.*;
import static com.example.sxn8837.locationapi.R.layout.activity_main;
import static com.google.android.gms.location.LocationServices.*;

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,LocationListener {

private static final String TAG = MainActivity.class.getSimpleName();
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
private Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private boolean mRequestLocationUpdates = false ;
private LocationRequest mLocationRequest;
private static int UPDATE_INTERVAL = 10000;
private static int FASTEST_INTERVAL = 5000;
private static int DISPLACEMENT = 10;

private TextView lbLocation;
private Button btnStartLocationUpdates;
private Button btnShowLocation;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
btnStartLocationUpdates = (Button) findViewById(id.buttonLocationUpdates);
btnShowLocation = (Button) findViewById(id.buttonShowLocation);
lbLocation = (TextView) findViewById(id.lblLocation);
if(checkPlayServices()){
buildGoogleApiClient();
createLocationRequest();
}

btnShowLocation.setOnClickListener(new View.OnClickListener (){

public void onClick(View v){

displayLocation();

}
});

btnStartLocationUpdates.setOnClickListener(new View.OnClickListener(){

public void onClick( View v){

togglePeriodLocationUpdates();
}

});
}

protected void onStart(){
super.onStart();
if(mGoogleApiClient != null){
mGoogleApiClient.connect();
}
}

protected void onResume(){
super.onResume();
checkPlayServices();
if(mGoogleApiClient.isConnected() && mRequestLocationUpdates ){
startLocationUpdates();
}
}

protected void onStop(){
super.onStop();
if(mGoogleApiClient.isConnected() ){
mGoogleApiClient.disconnect();
}

}

protected void onPause(){
super.onPause();
stopLocationUpdates();
}

private void displayLocation(){

mLastLocation = FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation != null){
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
lbLocation.setText(latitude + "," +longitude);
}
else {
lbLocation.setText("Couldn't get Location Updates");
}
}

private void togglePeriodLocationUpdates(){
if(!mRequestLocationUpdates) {
btnStartLocationUpdates.setText(getString(string.btn_stop_location_update));

mRequestLocationUpdates = true;

startLocationUpdates();
}

else {

btnStartLocationUpdates.setText(getString(string.btn_stop_location_update));

mRequestLocationUpdates = false ;

stopLocationUpdates();


}
}

protected synchronized void buildGoogleApiClient(){

mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.addApi(API).build();
}

protected void createLocationRequest(){
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}

private boolean checkPlayServices(){
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

if(resultCode != ConnectionResult.SUCCESS){

if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)){
GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICES_RESOLUTION_REQUEST).show();

}else{

Toast.makeText(getApplicationContext(),"This Device is not supported", Toast.LENGTH_LONG).show();
finish();
}
return false ;
}

return true;
}

protected void startLocationUpdates(){
FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, (com.google.android.gms.location.LocationListener) this);
}

protected void stopLocationUpdates (){
final PendingResult<Status> statusPendingResult = FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onConnected(Bundle bundle) {

displayLocation();

if(mRequestLocationUpdates){
startLocationUpdates();
}
}

@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {

mLastLocation = location ;

Toast.makeText(getApplicationContext(),"Location Changed",Toast.LENGTH_SHORT).show();
displayLocation();

}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Connection Failed:" + connectionResult.getErrorCode());
}
}
 
Last edited:
Upvote 0
The previous problem was fixed."The application has stopped" is a blanket error given by the system. You then have to look at your stack trace to find the real error.

You have another (different) null pointer exception. Can you see that in the stack trace? You should become familiar with reading the information, as it tells you exactly what went wrong in your code, and gives you a line number at which the problem occurred.

You should also learn how to run your application in debug mode, and set breakpoints in the code. This way you can stop the execution of the program, and examine the values of variables.

More fundamentally, do you know what a 'null pointer exception' actually is? If not, then that's your first lesson. If you don't understand this, then your life as an Android app developer will be very difficult.
 
Last edited by a moderator:
  • Like
Reactions: Sarmakin
Upvote 0
The previous problem was fixed."The application has stopped" is a blanket error given by the system. You then have to look at your stack trace to find the real error.

You have another (different) null pointer exception. Can you see that in the stack trace? You should become familiar with reading the information, as it tells you exactly what went wrong in your code, and gives you a line number at which the problem occurred.

You should also learn how to run your application in debug mode, and set breakpoints in the code. This way you can stop the execution of the program, and examine the values of variables.

More fundamentally, do you know what a 'null pointer exception' actually is? If not, then that's your first lesson. If you don't understand this, then your life as an Android app developer will be very difficult.

Thanks for the suggestion.Yes I will learn the application in debug mode . This is the reference I am looking at http://developer.android.com/tools/debugging/debugging-studio.html . Sorry for the delayed reply.Because of different time zones I replied just now.Yes I am aware of the fact of what null pointer exception is In my opinion my application is trying to use an object reference which has a null value.I have to figure which object is that I guess.Hope I am on the right path.Let me know if I am wrong.:).I also noticed that google play services util is missing.I installed the package from Android SDK Manager.Do I have a key that I need to get in order to use the methods.I am a beginner with Android APIs.Hence the basic Qs.Sorry about that.
 
Last edited:
Upvote 0
No that's great what you're doing. You'll learn more if you try to work through these things and understand what's going on. If you're really stuck, just ask questions here, or StackOverflow is a great programmer's resource.
Actually I found that page you mentioned quite interesting because it turns out that Android Studio can do object allocation tracking. That's useful for working out if your app is chewing up memory by creating too many objects. Didn't know it could do that!
 
Upvote 0
No that's great what you're doing. You'll learn more if you try to work through these things and understand what's going on. If you're really stuck, just ask questions here, or StackOverflow is a great programmer's resource.
Actually I found that page you mentioned quite interesting because it turns out that Android Studio can do object allocation tracking. That's useful for working out if your app is chewing up memory by creating too many objects. Didn't know it could do that!
.
Thanks will do so.Yeah I have posted in stackoverflow too.I am trying to seek opinion from people who have been developing in android over a period of time.I did get some suggestions over there too.But I just noticed the error in the stack that google play services is missing.I have installed it via the sdk manager.I just googled the error and got to know maybe I should set it to a lower version . Can you shed more light on this ? :)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones