android - 无法实例化 LocationListener 类型

标签 android android-activity

我正在为学校开发一个 Android 项目。此 Activity 应该获取 GPS 位置,并能够保存它。然后,稍后用户可以返回到该应用程序并获取从他们当前位置到该位置的路线。

对于 Android 开发人员来说还很陌生,下面的内容仍在进行中。我需要在退出应用程序后保存 pinLat 和 pinLong,以便在应用程序再次打开时它们可用。

这是当前的 Activity :

public class LocationActivity extends Activity implements LocationListener{

// declare variables
float locLat = 0;
float locLong = 0;
float pinLat = 0;
float pinLong = 0;

TextView txtLat;
TextView txtLong;
TextView txtPinLat;
TextView txtPinLong;
LocationManager locationManager;
LocationListener locationListener;
Location location;

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

    // get a handle on the text views
    txtLat = (TextView) findViewById(R.id.txtLat);
    txtLong = (TextView) findViewById(R.id.txtLong);
    txtPinLat = (TextView) findViewById(R.id.txtPinLat);
    txtPinLong = (TextView) findViewById(R.id.txtPinLong);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    //Why cant I instantiate the LocationListener?
    locationListener = new LocationListener();
}

public void onLocationChanged(Location location) {
    //if it finds the location
    if (location != null){
    // save the values to float variables
    locLat = (float) location.getLatitude();
    locLong = (float) location.getLongitude();
    // set the text views to the lat and long values
    txtLat.setText(String.valueOf(locLat));
    txtLong.setText(String.valueOf(locLong));
    }
}
//save the current location to be a pin for the map
public void pinLocation(View v) throws IOException {
    // when the button is pressed, save the current location lat and long
    pinLat = locLat;
    pinLong = locLong;

    // set the text views to the lat and long values
    txtPinLat.setText(String.valueOf(pinLat));
    txtPinLong.setText(String.valueOf(pinLong));
}

//launch google maps to navigate to the pin
public void navigate(View v) {
    //Toast.makeText(getApplicationContext(), "Navigate Called!", Toast.LENGTH_LONG).show();
    //build the google maps URL
    String uri = ("geo:"+ pinLat + "," + pinLong);
    //add the lat and long
    String query = pinLat + "," + pinLong;
    //encode the URI
    String encodedQuery = Uri.encode(query);
    //continue building the uri
    uri += "?q=" + encodedQuery;
    //start the intent with the Uri
    Intent navigate = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    //start the activity
    startActivity(navigate);
}

//when the app resumes, turn on the GPS locations
public void onResume(){
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            5,5, locationListener);
    super.onResume();
}

//on pause, stop the GPS
@Override
public void onPause() {
    locationManager.removeUpdates(locationListener);
    super.onPause();
}


//GENERATED CODE. Need to alert if disabled etc

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

在 onCreate 方法中,我在 locationListener = new LocationListener(); 上收到“无法实例化 LocationListener 类型”错误;线。我已经看到其他人在示例中使用了完全相同的行。我错过了什么?

我在我的 AndroidManifest 中有权限,布局应该不会造成任何问题..

谢谢!

最佳答案

由于您已经实现了 LocationListener,您的 Activity 已经是 LocationListener,因此您不必创建新的 LocationListener。

移除LocationListener locationListener; 周围的所有实例化

然后在您的 onResume 方法上尝试将请求更新行更改为:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

onLocationChanged 应该被触发,您可以从中获取位置。

关于android - 无法实例化 LocationListener 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26600267/

相关文章:

android - 当调用来自 PreferenceActivity 的 android 的 isValidFragment() 时?

android - 从 BroadcastReceiver 调用时 startActivity 不工作

java - Activity 结果错误

Android aChartEngine 条形图

android - Google Cloud Messaging 返回失败 : HTTP Status Code: 401

android - 在 Android 应用程序中重新启动后尝试打开文件夹时出现权限错误

java - Android 渲染问题无法解析资源@string/app_name

android - 如何在运行时在 Android 中获取高分辨率电池百分比级别?

android - 在Android中的逐帧动画中导致OutOfMemoryError

java - Realm 从 map 序列化数据