通过经度和纬度意图另一类 [英] Pass longitude and Latitude with Intent to another class
问题描述
我试图通过从onLocationChanged的纬度和经度在MainActivity到另一个包 com.route.provider
类 DataPrivider
,但我得到这个错误我该怎么办呢?我怎么能接受他们的DataProvider?
双PLONG = location.getLongitude();
双PLAT = location.getLatitude(); 经纬度fromPostion =新的经纬度(开发平台,PLONG); 捆绑ARGS =新包();
args.putParcelable(longLat_dataPrivider,fromPostion);
在捆绑式的方法putParcelable(字符串,Parcelable)不适用的参数(字符串,经纬度)
块引用>解决方案看起来问题不在于你的code,但可能与进口或您的项目设置。
我通过传递的
经纬度
对象作为Parcelable在活动意图
两者之间有Android Studio中这方面的工作在我的build.gradle:
{相关性
编译文件树(导演:'库',包括:['的* .jar'])
编译com.android.support:appcompat-v7:22.0.0
编译com.google.android.gms:播放服务:6.5 +。
}在这两项活动中,使用以下导入:
进口com.google.android.gms.maps.model.LatLng;
下面是测试code为我工作:
MainActivity.java:
双PLONG = -121.345678;
双PLAT = 37.123456;经纬度fromPostion =新的经纬度(开发平台,PLONG);捆绑ARGS =新包();
args.putParcelable(longLat_dataPrivider,fromPostion);意图I =新意图(这一点,MainActivity2.class);
i.putExtras(参数);
startActivity(ⅰ);MainActivity2.java:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main_activity2); 意向I = getIntent(); 经纬度LL = i.getParcelableExtra(longLat_dataPrivider); 文字=(TextView的)findViewById(R.id.text); text.setText(位置:+ ll.latitude ++ ll.longitude);}I am trying to pass the latitude and Longitude from the onLocationChanged in the MainActivity to another package
com.route.provider
classDataPrivider
but I am getting this error how can I do that? and how can I receive them in the DataProvider?double pLong = location.getLongitude(); double pLat = location.getLatitude(); LatLng fromPostion = new LatLng(pLat,pLong ); Bundle args = new Bundle(); args.putParcelable("longLat_dataPrivider", fromPostion);
The method putParcelable(String, Parcelable) in the type Bundle is not applicable for the arguments (String, LatLng)
解决方案It looks like the problem is not with your code, but possibly with imports or your project setup.
I got this working in Android Studio by passing the the
LatLng
Object as Parcelable in anIntent
between two Activities.In my build.gradle:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' compile 'com.google.android.gms:play-services:6.5.+' }
In both Activities, use the following import:
import com.google.android.gms.maps.model.LatLng;
Here is the test code that worked for me:
MainActivity.java:
double pLong = -121.345678; double pLat = 37.123456; LatLng fromPostion = new LatLng(pLat,pLong ); Bundle args = new Bundle(); args.putParcelable("longLat_dataPrivider", fromPostion); Intent i = new Intent(this, MainActivity2.class); i.putExtras(args); startActivity(i);
MainActivity2.java:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity2); Intent i = getIntent(); LatLng ll = i.getParcelableExtra("longLat_dataPrivider"); text = (TextView) findViewById(R.id.text); text.setText("location: " + ll.latitude + " " + ll.longitude); }
这篇关于通过经度和纬度意图另一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!