При попытке отобразить долготу и широту приложение переводит его в настройки местоположения, что позволяет пользователю включить местоположение (что я и хочу). Однако, когда я пытаюсь нажать кнопку еще раз, он продолжает спрашивать, хочет ли пользователь включить GPS. В конце концов, он не отображает долготу и широту. При трассировке стека я получил это сообщение об ошибке. Кстати, я использую планшет Amazon Fire для запуска этого кода. Что я могу сделать, чтобы это исправить? Изменить: я читал кого-то, что это не ошибка, однако, все еще возникает вопрос: почему TextView не отображает местоположение?
10-10 21:11:45.295 19091-19091/com.example.calculatorandlocationfinderfinal W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
10-10 21:11:50.905 19091-19113/com.example.calculatorandlocationfinderfinal I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
10-10 21:11:50.905 19091-19113/com.example.calculatorandlocationfinderfinal I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
10-10 21:11:50.905 19091-19113/com.example.calculatorandlocationfinderfinal I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3
//below is my code
package com.example.calculatorandlocationfinderfinal;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_LOCATION=1;
Button getLocation;
TextView TextView;
LocationManager locationManager;
String latitude, longitude;
TextView result;
EditText number1, number2;
Button add, subtract, multiply, divide;
float result_num;
int num1, num2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//current location
ActivityCompat.requestPermissions(this,new String[]
{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_LOCATION);
TextView=findViewById(R.id.id_textView);
getLocation=findViewById(R.id.button);
getLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
//check gps is enable or not
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
onGPS();
}
else
{
//GPS is already on then
getLocation();
}
}
});
//math code below
result = (TextView) findViewById(R.id.result);
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
add = (Button) findViewById(R.id.add);
subtract = (Button) findViewById(R.id.subtract);
divide = (Button) findViewById(R.id.divide);
multiply = (Button) findViewById(R.id.multiply);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 + num2;
result.setText(String.valueOf(result_num));
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 - num2;
result.setText(String.valueOf(result_num));
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 * num2;
result.setText(String.valueOf(result_num));
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 / num2;
result.setText(String.valueOf(result_num));
}
});
}
private void getLocation() {
//Check permission again
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);
Location LocationNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location LocationPassive=locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (LocationGps != null)
{
double lat =LocationGps.getLatitude();
double longi=LocationGps.getLongitude();
latitude=String.valueOf(lat);
longitude=String.valueOf(longi);
TextView.setText("Your Location:"+"\n"+"Latitude= "+ latitude+ "\n"+"Longitude"+longitude);
}
else if (LocationNetwork != null)
{
double lat =LocationNetwork.getLatitude();
double longi=LocationNetwork.getLongitude();
latitude=String.valueOf(lat);
longitude=String.valueOf(longi);
TextView.setText("Your Location:"+"\n"+"Latitude= "+ latitude+ "\n"+"Longitude"+longitude);
}
else if (LocationPassive !=null)
{
double lat =LocationPassive.getLatitude();
double longi=LocationPassive.getLongitude();
latitude=String.valueOf(lat);
longitude=String.valueOf(longi);
TextView.setText("Your Location:"+"\n"+"Latitude= "+ latitude+ "\n"+"Longitude"+longitude);
}
else
{
Toast.makeText(this,"Can't get Your Location", Toast.LENGTH_SHORT).show();
}
}
}
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();
}
}
//Manifest Code: Just to show it asks permission
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calculatorandlocationfinderfinal">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>