从这个问题主题可以看出,当在模拟器上运行我的程序时,它第一次完美运行,但是当我再次运行它而没有任何更改时,它显示"(项目名称)已停止工作。
我真的不知道如何解决这个问题,我尝试清理项目,再次构建APK,但仍然没有解决方案。
任何帮助请,将不胜感激。
这是我的代码:
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
TextView latitude;
TextView longitude;
TextView accuracy;
TextView altitude;
TextView address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitude = (TextView) findViewById(R.id.latitude);
longitude = (TextView) findViewById(R.id.longitude);
accuracy = (TextView) findViewById(R.id.accuracy);
altitude = (TextView) findViewById(R.id.altitude);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Check if we already have a location access permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// this mean we don't have permissions, so we need to ask for it.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); // 1 here is some sort of id to identify this request, its called request code
} else {
// we do have permission
// 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latitude.setText("Latitude: " + location.getLatitude());
longitude.setText("Longitude: " + location.getLongitude());
accuracy.setText("Accuracy: " + location.getAccuracy());
altitude.setText("Alitiude: " + location.getAltitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
}
@Override // this controls the dialog that asks user for location access permission
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// check if user clicked on the dialog and choose allow.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// if we have permission, then update user location. we need this check to make the code run!
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
}
即使我复制此代码并将其放入一个新项目中,它仍然可以工作,但是当我再次运行它同样的问题它不起作用。
我收到了这个错误
09-10 21:35:37.936 2588-2588 / com.example.qubayel.test2 E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.qubayel.test2,PID:2588 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.qubayel.test2 / com.example.qubayel.test2.MainActivity}:java.lang.IllegalArgumentException:无效的侦听器:null 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
我找到了解决方案,问题是我在定义之前使用了locationListener。在这行代码中:
// 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
这个定义应该在我的locationListener调用之上:
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latitude.setText("Latitude: " + location.getLatitude());
longitude.setText("Longitude: " + location.getLongitude());
accuracy.setText("Accuracy: " + location.getAccuracy());
altitude.setText("Alitiude: " + location.getAltitude());
}
@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 {
LocationManager locationManager;
LocationListener locationListener;
TextView latitude;
TextView longitude;
TextView accuracy;
TextView altitude;
TextView address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitude = (TextView) findViewById(R.id.latitude);
longitude = (TextView) findViewById(R.id.longitude);
accuracy = (TextView) findViewById(R.id.accuracy);
altitude = (TextView) findViewById(R.id.altitude);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latitude.setText("Latitude: " + location.getLatitude());
longitude.setText("Longitude: " + location.getLongitude());
accuracy.setText("Accuracy: " + location.getAccuracy());
altitude.setText("Alitiude: " + location.getAltitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
// Check if we already have a location access permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// this mean we don't have permissions, so we need to ask for it.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); // 1 here is some sort of id to identify this request, its called request code
} else {
// we do have permission
// 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
@Override // this controls the dialog that asks user for location access permission
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// check if user clicked on the dialog and choose allow.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// if we have permission, then update user location. we need this check to make the code run!
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
}