Issue
This is the xml for my activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/showLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Location"
android:textSize="24sp" />
<Button
android:id="@+id/btnGetLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Location" />
</LinearLayout>
this is for the MainActivity
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_LOCATION = 1;
private static final int ACCESS_COARSE_LOCATION =2 ;
private static final int ACCESS_FINE_LOCATION =3 ;
private static final int INTERNET =4 ;
Button btnGetLocation;
TextView showLocation;
LocationManager locationManager;
String latitude, longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions( this,
new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
showLocation = findViewById(R.id.showLocation);
btnGetLocation = findViewById(R.id.btnGetLocation);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
btnGetLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// location nManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);//error come here
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
OnGPS();
} else {
getLocation();
}
}
});
}
private void OnGPS() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == ACCESS_COARSE_LOCATION) {
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "ACCESS Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "ACCESS Permission Denied", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == ACCESS_FINE_LOCATION) {
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "ACCESS Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "ACCESS Permission Denied", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == INTERNET) {
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "INTERNET Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "INTERNET Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
private void getLocation() {
if (ActivityCompat.checkSelfPermission(
MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (locationGPS != null) {
double lat = locationGPS.getLatitude();
double longi = locationGPS.getLongitude();
latitude = String.valueOf(lat);
longitude = String.valueOf(longi);
showLocation.setText("Your Location: " + "\n" + "Latitude: " + latitude + "\n" + "Longitude: " + longitude);
} else {
Toast.makeText(this, "Unable to find location.", Toast.LENGTH_SHORT).show();
}
}
}
}
when I run my app i got an error like this cannot find symbol class location( I searched the google for solution but not yet resolved my problem please help me tho figure out the problem...I give permission like INTERNET,FINE_LOCATION and COARSE_LOCATION in manifest
Solution
Its just a typo, channge
location nManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
to
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Also would be better to move this above the button click
Answered By - lyncx
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.