我不知道为什么我的应用程序没有停止崩溃



我正在编写一个需要位置的应用程序。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) {
}
});
}

}
}

在我看来,在该位置初始化该行是错误的:

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

它必须放在这一行之后:

setContentView(R.layout.activity_main);

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

因此,为类成员调用findViewById不起作用。

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) {
}
});
}
}
}

最新更新