【问题标题】:Sending double from one activity to another Android Studio从一个活动向另一个 Android Studio 发送双精度
【发布时间】:2018-05-18 13:14:01
【问题描述】:

你好, 我正在尝试使用此方法将数据从一个活动发送到另一个活动:how to retrieve a Double value from one activity to another? 然而,每次我打开我的应用程序时,它都会崩溃,因为我的代码在 onCreate 中:

double lat, longi;
Intent getLocation;
Bundle extras;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
getLocation = this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");

但是当我把它放在一个按钮中时它无法解决 this.getIntent();

public void getCoordinates(View view){
    Button coordinates = (Button) findViewById(R.id.getCoordinates);
    coordinates.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            getLocation = this.getIntent();
            extras = getLocation.getExtras();
            lat = extras.getDouble("lat");
            longi = extras.getDouble("longi");
        }
    });
}

我想自动或使用按钮接收数据。 我是移动计算领域的新手,所以请不要烤我。

【问题讨论】:

  • 嗨 Konradst - 我认为没有人会烤你:-)。在按钮事件中,this.getIntent() 由于词法作用域而无法解析(“this”嵌套在函数中,看不到 getIntent() 方法)。我不是 Android 开发方面的专家,但为了帮助获得有效的答案,您最好只使用 onCreate 函数编辑问题并提供您看到的错误的详细信息(例如,崩溃报告详细信息、错误消息等)。

标签: java android android-studio-2.3


【解决方案1】:

this 在您的代码中表示 isView.OnClickListener()

我们需要YourActivity.this 作为Context

你应该使用

public void getCoordinates(View view){
Button coordinates = (Button) findViewById(R.id.getCoordinates);
coordinates.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
        getLocation = YourActivity.this.getIntent();
        extras = getLocation.getExtras();
        lat = extras.getDouble("lat");
        longi = extras.getDouble("longi");
    }
});
}

【讨论】:

  • 祝你好运。享受编码吧。@Konradst
猜你喜欢
  • 2013-04-07
  • 1970-01-01
  • 2012-08-04
  • 2014-09-06
  • 2015-07-20
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多