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
content_main.xml
androidmanifest.xml
log cat
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: