Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用意图传递变量-Android_Android_Android Studio - Fatal编程技术网

使用意图传递变量-Android

使用意图传递变量-Android,android,android-studio,Android,Android Studio,我目前正在开发一个android应用程序,它使用GooglePlacesAPI 我有两个活动,我想在它们之间传递一个变量。我尝试过使用putExtras和getExtras通过Intent 我一直与getExtras有问题。我已经调试了我的代码,使用了断点并将变量输出到日志中 以下是我要从中传递变量的活动的代码: Intent mapIntent = new Intent(getBaseContext(), MapsActivity.class); Spinner distance

我目前正在开发一个android应用程序,它使用GooglePlacesAPI

我有两个活动,我想在它们之间传递一个变量。我尝试过使用putExtras和getExtras通过Intent

我一直与getExtras有问题。我已经调试了我的代码,使用了断点并将变量输出到日志中

以下是我要从中传递变量的活动的代码:

   Intent mapIntent = new Intent(getBaseContext(), MapsActivity.class);

   Spinner distanceSpinner = (Spinner) findViewById(R.id.spinner_distance);

   String radius = distanceSpinner.getSelectedItem().toString();

   mapIntent.putExtra("radius", radius);

   Log.e("Passer", String.valueOf(radius)); //Shows that value is not = null.

   startActivity(mapIntent);
此类的注销输出是正确的,并显示变量

下面是我要将变量传递给的类的代码:

Intent filterIntent = new Intent(getBaseContext(), LocateFilterActivity.class);

Bundle bd = filterIntent.getExtras();
String radius= (String) bd.get("radius");

Log.e("TESTER", String.valueOf(radius));

startActivity(placeIntent);
日志实际上没有为此类输出任何内容。我知道它可以通过调试达到并执行代码行

我尝试过按照前面的SO问题/答案以几种不同的方式编写代码,但运气不太好


任何帮助都将不胜感激:)

您可以使用getIntent()访问传递给活动的数据。getExtras()方法可以在使用startActivity(intent)调用创建的新创建的活动中调用该方法。:)

您试图访问错误的意图数据try
getIntent().getStringExtra(“radius”)的可能重复项LocateFilterActivity
activity
onCreate()
方法中的code>。