Java 按钮未显示经度和纬度

Java 按钮未显示经度和纬度,java,android,android-studio,Java,Android,Android Studio,当尝试显示经度和纬度时,应用程序会将其转到位置设置,允许用户打开位置(这是我想要的)。但是,当我尝试再次按下按钮时,它会继续询问用户是否要启用GPS。最后,它不显示经度和纬度。在堆栈跟踪时,我收到了此错误消息。顺便说一下,我正在使用amazon fire平板电脑来运行此代码。我能做些什么来解决这个问题? 编辑:我读到有人说这不是一个错误,但是,问题仍然出现:为什么TextView不显示位置 10-10 21:11:45.295 19091-19091/com.example.calculator

当尝试显示经度和纬度时,应用程序会将其转到位置设置,允许用户打开位置(这是我想要的)。但是,当我尝试再次按下按钮时,它会继续询问用户是否要启用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"/>


10-10 21:11:45.295 19091-19091/com.example.calculator和LocationFinderFinal W/IIInputConnection包装器:在非活动InputConnection上显示状态图标
10-10 21:11:50.905 19091-19113/com.example.calculator和locationfinderfinal I/MaliEGL:[Mali]窗口类型=1,是帧缓冲区=0,错误数=0
10-10 21:11:50.905 19091-19113/com.example.calculator和locationfinderfinal I/MaliEGL:[Mali]表面->缓冲区数=4,表面->帧数=3,最小值=1
10-10 21:11:50.905 19091-19113/com.example.calculator和locationfinderfinal I/MaliEGL:[Mali]允许的最大缓冲区数\u出列缓冲区数=3
//下面是我的代码
包com.example.calculatorandlocationfinderfinal;
导入androidx.appcompat.app.AlertDialog;
导入androidx.appcompat.app.appcompat活动;
导入androidx.core.app.ActivityCompat;
导入android.Manifest;
导入android.app.Dialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.provider.Settings;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.w3c.dom.Text;
公共类MainActivity扩展了AppCompatActivity{
私有静态最终int请求_位置=1;
按钮位置;
文本视图文本视图;
地点经理地点经理;
弦纬度、经度;
文本视图结果;
编辑文本编号1,编号2;
按钮加、减、乘、除;
浮点数;
int num1,num2;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//当前位置
ActivityCompat.requestPermissions(此,新字符串[])
{Manifest.permission.ACCESS\u FINE\u LOCATION},REQUEST\u LOCATION);
TextView=findViewById(R.id.id\u TextView);
getLocation=findViewById(R.id.button);
getLocation.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
//检查gps是否启用
如果(!locationManager.isProviderEnabled(locationManager.GPS\U提供程序))
{
onGPS();
}
其他的
{
//GPS已经启动了
getLocation();
}
}
});
//下面是数学代码
结果=(TextView)findViewById(R.id.result);
number1=(EditText)findViewById(R.id.number1);
number2=(EditText)findViewById(R.id.number2);
add=(按钮)findViewById(R.id.add);
subtract=(按钮)findViewById(R.id.subtract);
divide=(按钮)findViewById(R.id.divide);
乘法=(按钮)findViewById(R.id.multiply);
add.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
num1=Integer.parseInt(number1.getText().toString());
num2=Integer.parseInt(number2.getText().toString());
结果_num=num1+num2;
result.setText(String.valueOf(result_num));
}
});
subtract.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
num1=Integer.parseInt(number1.getText().toString());
num2=Integer.parseInt(number2.getText().toString());
结果_num=num1-num2;
result.setText(String.valueOf(result_num));
}
});
multiply.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
num1=Integer.parseInt(number1.getText().toString());
num2=Integer.parseInt(number2.getText().toString());
结果_num=num1*num2;
result.setText(String.valueOf(result_num));
}
});
divide.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
num1=Integer.parseInt(number1.getText().toString());
num2=Integer.parseInt(number2.getText().toString());
结果_num=num1/num2;
result.setText(String.valueOf(result_num));
}
});
}
私有void getLocation(){
//再次检查权限
if(ActivityCompat.checkSelfPermission(MainActivity.this、Manifest.permission.ACCESS\u FINE\u位置)
!=已授予PackageManager.PERMISSION\u权限&&
ActivityCompat.checkSelfPermission(MainActivity.this、Manifest.permission.ACCESS\u\u位置)!=PackageManager.permission\u已授予)
{
ActivityCompat.requestPermissions(此,新字符串[])
{Manifest.permission.ACCESS\u FINE\u LOCATION},REQUEST\u LOCATION);
}
其他的
{
Location LocationGps=locationManager.getLastKnownLocation(locationManager.GPS\U提供程序);
Location LocationNetwork=locationManager.getLastKnownLocation(locationManager.NETWORK\u提供程序);
位置被动=l