尝试传输双精度时意图"无法解析构造函数"



我正在尝试使用在一个活动中获取的位置数据在另一个活动中进行计算,并传输我正在尝试使用Intent的数据。但是,当我尝试添加意图时,我不断收到错误

"无法解析构造函数"

我尝试从中传递数据的活动如下所示:

import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
public class CurrentLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location)
{
    if(location != null)
    {
        Double currentLat = location.getLatitude(); 
        Double currentLong = location.getLongitude(); 
        Log.e("Latitude :", "" + location.getLatitude());
        Log.e("Longitude :", "" + location.getLongitude());
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("Lat", currentLat);
        intent.putExtra("Long", currentLong);
        CurrentLocationListener.this.startActivity(intent);
    }
 }

这就是我尝试在其他活动中使用数据的地方:

public class MainActivity extends Activity implements SensorEventListener {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
parseButton.setOnClickListener(
                new Button.OnClickListener()
                {
                    public void onClick(View inputButtonView)
                        {
                            String latInputString = latFind.getText().toString();
                            String longInputString = longFind.getText().toString();
                            double latInputDouble = Double.parseDouble(latInputString);
                            double longInputDouble = Double.parseDouble(longInputString);
                            double dLon = (longInputDouble-currentLong);
                            double y = Math.sin(dLon) * Math.cos(latInputDouble);
                            double x = Math.cos(currentLat)
                                    *Math.sin(latInputDouble) - Math.sin(currentLat)
                                    *Math.cos(latInputDouble)*Math.cos(dLon);
                            double targetBearing = Math.toDegrees((Math.atan2(y, x)));
                            targetBearing = (360 - ((targetBearing + 360) % 360));
                        }
                });

所以我要问的是:如何正确地将纬度和经度的双精度数据从一个活动传输到另一个活动?

这是因为您需要将应用程序或活动作为第一个参数传递给意图构造函数。

您的 CurrentLocationListener 不会扩展活动类。这不是一个 Activity.So 你不能在意图构造函数中传递"this"

最新更新