Android 应用程序在我请求位置时一直停止

Android Application keeps stopping as I requested location

我是Android工作室的新手,我知道,这不能成为借口,但我需要你的帮助

我在清单中添加了两个请求语句

<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"/>

在我的 activity java 文件中,我有位置管理器,我正在尝试获取纬度和经度并将它们传递给 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) {

    }
}

但是,应用程序一直停止。

已更新LogCat,修复崩溃后,仍然无法显示位置

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

它是由这行创建的:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

如果你调用像 getSystemService 这样需要 Context 的函数,你不能像这样将它们作为 class 成员新建。您 必须 在创建 Context 时执行此操作,例如在 onCreate() 中。所以你的代码需要像这样:

LocationManager locationManager = null;

protected void onCreate(Bundle savedInstanceState) {
    ...
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    ...
}

我没有检查你所有的代码,你可能还会发现其他错误,但这部分显然是不正确的。