[英]Android Application keeps stopping as I requested location
I am new to Android Studio, I know that, it cannot be an excuse but I need your help 我是Android Studio的新手,我知道这不能成为借口,但需要您的帮助
I added two request statements in 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"/>
On my activity java file I have location manager, I am trying to get Latitude and Longitude and pass them to TextView. 在我的活动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) {
}
}
However, Application keeps stopping. 但是,应用程序不断停止。
Updated LogCat , after crashing was fixed, however still cannot display location 修复了崩溃后更新了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
It is created by this line: 它是由以下行创建的:
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if you call a function like getSystemService
which needs Context
, you cannot new them as class member like this. 如果调用需要
Context
类似getSystemService
的函数,则不能像这样的类成员将其新建。 You must do it when Context
is created, for example in onCreate()
. 创建
Context
时必须执行此操作,例如在onCreate()
。 So your code need to be like this: 因此,您的代码需要像这样:
LocationManager locationManager = null;
protected void onCreate(Bundle savedInstanceState) {
...
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
...
}
I have not checked all of your code and you may find other bugs too, but this part is obviously incorrect. 我没有检查所有代码,您可能还会发现其他错误,但是这部分显然是不正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.