我构建了一个应用程序,该应用程序基本上显示3个不同的片段,底部具有“底部导航”菜单。我想使用包含Google Map的“浮动操作”按钮打开一个新的Activity,但是不幸的是,如果我尝试运行该应用,则会关闭该应用。
我的MainActivity XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/mapbutton">
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:srcCompat="@drawable/ic_public"
tools:ignore="VectorDrawableCompat" />
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation"
android:id="@+id/mapbutton"/>
我的Google Maps Activity XML文件:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TourActivity" />
这是我的MainActivity Java文件:
public class MainActivity extends AppCompatActivity {
private FloatingActionButton button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.mapbutton);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new BlogActivity()).commit();
}
button = findViewById(R.id.mapbutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openTourActivity();
}
});
}
public void openTourActivity(){
Intent intent = new Intent(this, TourActivity.class);
startActivity(intent);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_blog:
selectedFragment = new BlogActivity();
break;
case R.id.nav_explore:
selectedFragment = new ExporeActivity();
break;
case R.id.nav_user:
selectedFragment = new UserActivity();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
这是Google Map活动。 Tbh我什么都没改变。我只是复制并尝试运行它。
public class TourActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tour);
// 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}}
我的Logcat说:
Caused by: java.lang.ClassCastException: android.support.design.widget.FloatingActionButton cannot be cast to android.support.design.widget.BottomNavigationView
前段时间,我设法构建了类似的东西,并且奏效了,但这次我很挣扎。如果我将“地图活动”设置为启动器活动,则该应用程序启动,但是从“主要活动”开始,反之则需要它。
期待您的帮助和解答。
答案 0 :(得分:1)
此行是错误的:
application/json
用您的 BottomNavigationView ID替换BottomNavigationView bottomNav = findViewById(R.id.mapbutton);
....
....
button = findViewById(R.id.mapbutton);
。