Java Android应用程序没有';不显示位置

Java Android应用程序没有';不显示位置,java,android,android-gps,Java,Android,Android Gps,我一整天都在寻找解决问题的办法,但我找不到答案。我对java/androidstudio/app编程完全是新手,可能这只是我没有看到的一件很小的事情 我只想让我的应用程序显示设备的当前位置。没别的了 在emulator上运行应用程序时,不会显示任何错误,应用程序将启动 然后我打开扩展控件并按下“位置”中的“发送”按钮。 现在我希望我的应用程序显示当前位置,但什么也没发生。TextView“textLat”、“textLong”和“textAlt”保持为空 我完全不知道我的错是什么。我在不同教程的

我一整天都在寻找解决问题的办法,但我找不到答案。我对java/androidstudio/app编程完全是新手,可能这只是我没有看到的一件很小的事情

我只想让我的应用程序显示设备的当前位置。没别的了

在emulator上运行应用程序时,不会显示任何错误,应用程序将启动

然后我打开扩展控件并按下“位置”中的“发送”按钮。 现在我希望我的应用程序显示当前位置,但什么也没发生。TextView“textLat”、“textLong”和“textAlt”保持为空

我完全不知道我的错是什么。我在不同教程的帮助下编写了这些代码。我也在我真正的设备上试用过

我将非常感谢任何帮助!非常感谢

    package com.example.findlocation_test2;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView textLat;
    TextView textLong;
    TextView textAlt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textLat = (TextView) findViewById(R.id.textLat);
        textLong = (TextView) findViewById(R.id.textLong);
        textAlt = (TextView) findViewById(R.id.textAlt);

        LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                if(location != null) {
                    double dLat = location.getLatitude();
                    double dLong = location.getLongitude();
                    double dAlt = location.getAltitude();

                    textLat.setText(Double.toString(dLat));
                    textLong.setText(Double.toString(dLong));
                    textAlt.setText(Double.toString(dAlt));
                }
                else {
                    textLat.setText("Fehler");
                    textLong.setText("Fehler");
                    textAlt.setText("Fehler");
                }
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

                Toast.makeText(getBaseContext(), "GPS wurde akiviert",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProviderDisabled(String provider) {

                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
                Toast.makeText(getBaseContext(), "GPS wurde deaktiviert",
                        Toast.LENGTH_SHORT).show();

            }
        };

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, ll);

    }

}
在清单中,我添加了权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.findlocation_test2">

    <uses-permission android:name="android.permission.ACCESS_INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> /**Erlaubnis, um auf die GPS Daten zuzugreifen*/
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

/**埃劳布尼斯,嗯,我要去祖祖格里芬*/

查看其中的结果:它在答案中解释了如何执行此操作。如果你的代码是相同的,你可能没有正确地创建你的按钮/事件。嘿,我已经尝试过了,像大多数人一样,我只得到了0.0的坐标。另外,我不需要这么复杂的代码。实际上我只是想从系统中找出坐标,这样我就可以使用它们了。我认为这将是一项简单的任务。。。