【问题标题】:I don't know why my app don't stop crashing我不知道为什么我的应用程序不会停止崩溃
【发布时间】:2019-10-30 14:46:44
【问题描述】:

我正在编写一个需要该位置的应用程序。 Logcat 告诉我我的代码很清晰,没有错误。但是当我启动应用程序(模拟或在我的手机上)时,它会崩溃。

这是我的代码:

package com.example.a291019;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    TextView txtLocation = findViewById(R.id.txtLocation);

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

        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_DENIED || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_DENIED) {

            assert locationManager != null;
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 10, new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    Log.d("GPS", "Latitude" + location.getLatitude() + " et longitude " + location.getLongitude());
                    txtLocation.setText(Log.d("GPS", String.format("Latitude%s et longitude %s", location.getLatitude(), location.getLongitude())));
                }

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

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }

            });
        }


    }
}

【问题讨论】:

  • 相关部分是你的应用崩溃时显示的错误信息
  • 当您的应用崩溃时,请查看 Logcat 窗口中的堆栈跟踪以了解发生了什么
  • @Bruno ,当我的应用程序崩溃时,弹出一个窗口告诉我它会一直停止并建议我在启动仿真后关闭应用程序。没有错误信息
  • 假设你是一个开发者,这种回答在这里是不能接受的……使用Android Studio作为开发者,尤其是Logcat……

标签: java android geolocation location


【解决方案1】:

从我的角度来看,该位置的那条线的初始化是错误的:

public class MainActivity extends AppCompatActivity {
    TextView txtLocation = findViewById(R.id.txtLocation);

它必须放在这一行之后:

setContentView(R.layout.activity_main);

这是您为该活动定义负责布局的位置。

所以为班级成员调用findViewById 是行不通的。

【讨论】:

  • 真的。 Log.d() 也返回 Integer 并且不应该是 setText() 方法的参数。
  • @StefanBeike 当我移动这一行时,它不想模拟我的应用程序并且有这样的消息:“错误:从内部类中访问局部变量 txtLocation;需要声明为 final”进入“构建”部分。
  • @Ehsanmsz 那么如何设置我的文本的经纬度呢?
  • @Vayhuit txtLocation.setText(String.format("Latitude%s et longitude %s", location.getLatitude(), location.getLongitude()));
  • @Ehsanmsz 是的,但是当我添加这一行时,我的应用程序仍然崩溃
【解决方案2】:

1) 将此行移至onCreate 并改成final:

final TextView txtLocation = findViewById(R.id.txtLocation);

2) Log.d() 返回整数,不应该是 setText 方法的参数, 这样做而不是那样:

txtLocation.setText(String.format("Latitude%s et longitude %s", 
    location.getLatitude(), location.getLongitude()));

固定代码:

package com.example.a291019;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;


public class MainActivity extends AppCompatActivity {

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

    final TextView txtLocation = findViewById(R.id.txtLocation);

    LocationManager locationManager = (LocationManager) 
 getSystemService(Context.LOCATION_SERVICE);
    if (ContextCompat.checkSelfPermission(this, 
 Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_DENIED || 
 ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 
 != PackageManager.PERMISSION_DENIED) {

        assert locationManager != null;
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 10, 
          new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.d("GPS", "Latitude" + location.getLatitude() + " et longitude " + 
                    location.getLongitude());
                txtLocation.setText(String.format("Latitude%s et longitude %s", 
                    location.getLatitude(), location.getLongitude()));
            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }

        });
    }

  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2016-04-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多