I am new to Android Studio, I know that, it cannot be an excuse but I need your help
I added two request statements in Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.escaper.lehome">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
On my activity java file I have location manager, I am trying to get Latitude and Longitude and pass them to TextView.
package com.example.escaper.lehome;
import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.location.LocationServices;
import org.w3c.dom.Text;
public class Main2Activity extends AppCompatActivity {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
if (location != null) {
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
// We have location data
TextView tvLat = (TextView) findViewById(R.id.tvLat);
tvLat.setText(latitude.toString());
TextView tvLong = (TextView) findViewById(R.id.tvLong);
tvLong.setText(longitude.toString());
} else {
// We don't have location data
}
}
public void onClick(View view) {
}
}
However, Application keeps stopping.
Updated LogCat , after crashing was fixed, however still cannot display location
03-19 23:17:16.325 9261-9261/? I/art: Not late-enabling -Xcheck:jni (already on)
03-19 23:17:16.325 9261-9261/? W/art: Unexpected CPU variant for X86 using defaults: x86
03-19 23:17:16.574 9261-9261/com.example.escaper.lehome W/System: ClassLoader referenced unknown path: /data/app/com.example.escaper.lehome-1/lib/x86
03-19 23:17:16.673 9261-9291/com.example.escaper.lehome I/GMPM: App measurement is starting up
03-19 23:17:16.674 9261-9261/com.example.escaper.lehome I/InstantRun: starting instant run server: is main process
03-19 23:17:16.684 9261-9291/com.example.escaper.lehome E/GMPM: getGoogleAppId failed with status: 10
03-19 23:17:16.685 9261-9291/com.example.escaper.lehome E/GMPM: Uploading is not possible. App measurement disabled
03-19 23:17:16.752 9261-9261/com.example.escaper.lehome W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-19 23:17:16.947 9261-9300/com.example.escaper.lehome I/OpenGLRenderer: Initialized EGL, version 1.4
03-19 23:17:16.947 9261-9300/com.example.escaper.lehome D/OpenGLRenderer: Swap behavior 1
03-19 23:17:16.947 9261-9300/com.example.escaper.lehome W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-19 23:17:16.947 9261-9300/com.example.escaper.lehome D/OpenGLRenderer: Swap behavior 0
03-19 23:17:17.019 9261-9261/com.example.escaper.lehome W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
It is created by this line:
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if you call a function like getSystemService
which needs Context
, you cannot new them as class member like this. You must do it when Context
is created, for example in onCreate()
. So your code need to be like this:
LocationManager locationManager = null;
protected void onCreate(Bundle savedInstanceState) {
...
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
...
}
I have not checked all of your code and you may find other bugs too, but this part is obviously incorrect.