我希望我的 MainActivity.java 有一个按钮,点击该按钮时应该执行用 MapsActivity.java 编写的代码
Posted
技术标签:
【中文标题】我希望我的 MainActivity.java 有一个按钮,点击该按钮时应该执行用 MapsActivity.java 编写的代码【英文标题】:I want my MainActivity.java to have a button which when tapped should execute the code written in MapsActivity.java 【发布时间】:2021-02-02 06:30:58 【问题描述】:我创建了一个 MapsActivity 和相应的 activity_maps.xml。我的 activity_main.xml 将有一个按钮,当点击该按钮时应显示标记指向的当前位置。
在 androidManifest.xml 中将 MapsActivity 作为 Launcher Activity 时代码工作正常,但希望我的 MainActivity 成为 Launcher Activity。
如何将我的 MainActivity 设为 Launcher Activity。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
public void onMapReady(GoogleMap googleMap)
Toast.makeText(this, "inside onMapReady()", Toast.LENGTH_SHORT).show();//doesn't appear
Log.i("hurray:","inside onMapReady()");//doesn't appear
mMap = googleMap;
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener()
@Override
public void onLocationChanged(@NonNull Location location)
if(location != null)
Toast.makeText(MapsActivity.this, "Location is not null", Toast.LENGTH_SHORT).show();//doesn't appear
LatLng userLatLngLocation = new LatLng(location.getLatitude(),location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(userLatLngLocation);
markerOptions.title("Current position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLatLngLocation));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
Toast.makeText(getApplicationContext(), location.toString(), Toast.LENGTH_SHORT).show();//does appears sometimes
else
Toast.makeText(getApplicationContext(), "location is null !", Toast.LENGTH_SHORT).show();
public void requestLocationPermission()
//if permission not granted ask for it
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, 1);
//else if location permission is already granted get location updates
else
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);//every 0secs & 0meters
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, int grantResults[])
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
MainActivity.java:
public class MainActivity extends AppCompatActivity
Button getPopLocationButton;//button to show the Maps
MapsActivity mapsActivity;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.map_screen);
mapsActivity = new MapsActivity();
getPopLocationButton = findViewById(R.id.buttonPopLocation);
getPopLocationButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
setContentView(R.layout.activity_maps);
);
【问题讨论】:
你能在旧问题中检查this solution @Ahmed 没用 :( 好的,我只是注意到,如果我在 AndroidManifest.xml 中将 MapsActivity 作为启动器活动,则代码可以正常工作,并且在当前用户位置可以看到一个标记,但我希望将 MainActivity 作为我的启动器活动和activity_main.xml 应该有一个名为“显示当前位置”的按钮,点击该按钮时应显示此标记。 【参考方案1】:好的,我得到了这个工作,
public void onClick(View view)
setContentView(R.layout.map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(MainActivity.this);
【讨论】:
找到了另一个简单的解决方案,我们可以简单地从 MainActivity 启动一个 Intent 到 MapsActivity以上是关于我希望我的 MainActivity.java 有一个按钮,点击该按钮时应该执行用 MapsActivity.java 编写的代码的主要内容,如果未能解决你的问题,请参考以下文章
在 Android Studio 中找不到 MainActivity.java [关闭]
如何将片段活动添加到 MainActivity.java 或 MainActivity.kt
如何在 MainActivity.java 中发出简单的 HTTP 请求? (安卓工作室)