我正在编写一个需要位置的应用程序。 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) {
}
});
}
}
}
答案 0 :(得分:4)
从我的角度来看,该行在该位置的初始化是错误的:
public class MainActivity extends AppCompatActivity {
TextView txtLocation = findViewById(R.id.txtLocation);
它必须放在此行之后:
setContentView(R.layout.activity_main);
这是您定义该活动负责布局的位置。
因此,调用findViewById
的类成员是行不通的。
答案 1 :(得分:0)
1)将这一行移至onCreate
,并将其设置为final
:
final TextView txtLocation = findViewById(R.id.txtLocation);
2)Log.d()
返回Integer,并且不应该是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) {
}
});
}
}
}