【问题标题】:Android Application keeps stopping as I requested locationAndroid 应用程序在我请求位置时不断停止
【发布时间】:2018-08-28 09:46:48
【问题描述】:

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

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

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

在我的活动 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

【问题讨论】:

  • 您是否在运行时请求了权限?较新的 Android 版本需要此功能。有关详细信息,请参阅文档。
  • @Ninja - 如果您还有其他问题,请不要更新此问题。接受有帮助的答案,然后提出一个新问题

标签: java android android-location


【解决方案1】:

它是由这一行创建的:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

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

LocationManager locationManager = null;

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

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

【讨论】:

  • 这是正确答案(因为可以在 Logcat 中找到以下行:“Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()”)
  • 谢谢,我的应用程序不再崩溃。但是,它仍然无法显示我的位置。我在我的手机上测试它,而不是在虚拟设备上。它甚至在启动时都不会请求许可。我是否应该创建类似对话的内容,以便用户可以允许应用使用设备的位置。
  • @Ninja 我的建议?阅读如何正确使用定位服务。您需要大量更改代码。在这里阅读教程:developer.android.com/training/location/index.html 或这个:developer.android.com/guide/topics/location/strategies.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
相关资源
最近更新 更多