Dee Jott
Dee Jott

Reputation: 125

My app won´t open after Google Maps integration

i´ve built an app that is basically showing 3 different fragments with a Bottom Navigation menu at the bottom. I want to open a new Activity with a Floating Action Button that contains a Google Map but unfortunately, the app gets shut down if i try to run it.

My MainActivity XML file:

<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"/>

My Google Maps Activity XML file:

<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" />

This is my MainActivity Java file:

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;
            }
        };
}

This is the Google Map Activity. Tbh i didnt change anything significant at all. I just copied and try to run it.

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));
}}

My Logcat says:

 Caused by: java.lang.ClassCastException: android.support.design.widget.FloatingActionButton cannot be cast to android.support.design.widget.BottomNavigationView

I managed to build something similar a while ago and it worked but this time i am struggling. The app starts if i set the Maps Activity as launcher Activity but i need it the other way around with the Main Activity starting.

Looking forward to your help and answers.

Upvotes: 0

Views: 41

Answers (1)

Pratik Butani
Pratik Butani

Reputation: 62411

This line is wrong:

BottomNavigationView bottomNav = findViewById(R.id.mapbutton);
....
....
button = findViewById(R.id.mapbutton);

Replace mapbutton with your BottomNavigationView Id.

Upvotes: 1

Related Questions