【问题标题】:Intent 'Cannot resolve constructor' when trying to transfer double尝试传输双精度时意图“无法解析构造函数”
【发布时间】:2016-05-19 18:08:44
【问题描述】:

我正在尝试使用我在一项活动中获得的位置数据在另一项活动中进行计算并传输我正在尝试使用 Intent 的数据。但是,当我尝试添加 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));
                        }
                });

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

【问题讨论】:

  • 我也在绞尽脑汁思考如何正确回调捆绑的数据

标签: android android-intent android-activity navigation double


【解决方案1】:

这是因为您需要将 Application 或 activity 作为第一个参数传递给 Intent 构造函数。

您的 CurrentLocationListener 没有扩展 Activity 类。它不是 Activity。因此您不能在 Intent 构造函数中传递“this”

【讨论】:

  • 是否有其他方法可以将 currentLat 和 currentLong 变量从一个活动传递到另一个活动?
  • 您在哪里以及如何调用 CurrentLocationListener?
  • 我试图用它来存储用户的当前位置,然后在 mainActivity 的计算中使用该数据。但我现在才意识到我可以让他们参与同一个活动而不需要这个活动。然后我只需要弄清楚如何从那里使用它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 2017-03-10
  • 2020-07-01
  • 2015-05-05
相关资源
最近更新 更多