这是我的第一个片段代码,它将数据发送到Activity,它正在运行:
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Log.i(TAG,"Place:"+place.getLatLng());
LatLng latLng = place.getLatLng();
double latitude = latLng.latitude;
double longitude = latLng.longitude;
Intent intent= new Intent(getApplicationContext(),Activity_home.class);
intent.putExtra("latitude",latitude);
intent.putExtra("longitude",longitude);
startActivity(intent);
}
@Override
public void onError(Status status) {
Log.i(TAG,"An error occured:" + status);
}
});
这是从片段接收数据然后将其发送到其中的第二个片段的活动:
public class Activity_home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
Toolbar toolbar;
FloatingActionButton floatingActionButton;
DrawerLayout drawerLayout;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar)findViewById(R.id.toolbar);
//floatingActionButton= (FloatingActionButton)findViewById(R.id.fab);
navigationView = (NavigationView)findViewById(R.id.nav_view);
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My Test App");
/***************HEADER LAYOUT contents*****************/
View nav_view = navigationView.getHeaderView(0);
ImageView profile_img = (ImageView)nav_view.findViewById(R.id.imgProfile_drawer);
TextView title = (TextView)nav_view.findViewById(R.id.textView_navHeader_name);
TextView email = (TextView)nav_view.findViewById(R.id.textView_navHeader_email);
profile_img.setImageResource(R.drawable.ic_profile_reg);
title.setText("Moin Hashmi");
email.setText("moin.hashmi20@gmail.com");
/***************NAVIGATION DRAWER**********************/
/* floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "Snackbar", Snackbar.LENGTH_LONG).show();
}
});*/
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_open,R.string.navigation_close);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
//Double lonlat = (getIntent().getDoubleExtra("place",0));
if (getIntent().getExtras() != null){
double latitude = getIntent().getDoubleExtra("latitude",0.00);
double longitude = getIntent().getDoubleExtra("longitude",0.00);
Bundle bundle=new Bundle();
bundle.putDouble("lat",latitude);
bundle.putDouble("lon",longitude);
// bundle.putString("latitude", latitude+"");
// bundle.putString("longitude", longitude+"");
Fragment fragment = new Map();
fragment.setArguments(bundle);
navigationView.getMenu().getItem(0).setChecked(true);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.content_home,fragment).commit();
}
以下是从父Activity:
接收数据的片段代码 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(getArguments()!=null) {
longitude = this.getArguments().getDouble("lon");
latitude = this.getArguments().getDouble("lat");
}
View view = inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.gmaps);
mapFragment.getMapAsync(this);
btn_bookNow=(Button)view.findViewById(R.id.btn_Booknow);
btn_schedule=(Button)view.findViewById(R.id.btn_scheduleLater);
btn_bookNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(getActivity(),"clicked",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MapSearch.class);
startActivity(intent);
}
});
return view;
}
答案 0 :(得分:1)
你需要在两个地方使用相同的键名“lat”& “lon”或“纬度”& “经度”即可。
这就是你所拥有的:
bundle.putDouble("lat",latitude);
bundle.putDouble("lon",longitude);
再次检查
if(getArguments()!=null) {
longitude = this.getArguments().getDouble("longitude");
latitude = this.getArguments().getDouble("latitude");
}
我还建议不要将MapFragment放在另一个片段中,但这是一个单独的问题