0

I just started to learn about Android App Development, and I am about to create a simple application for starter right now.

I followed a tutorial on YouTube, but I don't know why I get some errors when I'm trying to run my app. It says they expect a ;, ), and more. I tried to look into my code, and i've made sure that every ( have ), and every line has ;

But the list expands and I don't know what's actually wrong.

package org.kosmik.infokosmik;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

private BottomNavigationView mMenuUtama;
private FrameLayout mFrameUtama;

private berandaFragment berandaFragment;
private beritaFragment beritaFragment;
private radioFragment radioFragment;
private kegiatanFragment kegiatanFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mFrameUtama = (FrameLayout) findViewById(R.id.frame_utama);
    mMenuUtama = (BottomNavigationView) findViewById((R.id.menu));

    berandaFragment = new berandaFragment();
    beritaFragment = new beritaFragment();
    radioFragment = new radioFragment();
    kegiatanFragment = new kegiatanFragment();

    mMenuUtama.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId())) {

                    case R.id.menu_beranda :
                        setFragment(berandaFragment);
                        return true;

                    case R.id.menu_berita :
                        setFragment(beritaFragment);
                        return true;

                    case R.id.menu_radio :
                        setFragment(radioFragment);
                        return true;

                    case R.id.menu_kegiatan :
                        setFragment(kegiatanFragment);
                        return true;

                    default:
                        return false;
                }
        }
    }
}

public void setFragment(Fragment fragment) {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame_utama, fragment);
    fragmentTransaction.commit();
};}

This is my code on MainActivity.java . Anyway, i would really appreciate it if you would answer and explain it.

Stuti Rastogi
  • 1,162
  • 2
  • 16
  • 26
Yahya98
  • 5
  • 1
  • 3
  • Need to remove an extra closing bracket from `switch (item.getItemId()))`. Your brackets should **always** match up. – Michael Dodd Jun 19 '18 at 16:11
  • 1
    Android Studio should highlight where missing ; are in red, look down the right hand side scrollbar and you will see a red indicator. – CodeChimp Jun 19 '18 at 16:12
  • You also only need one set of brackets for `findViewById((R.id.menu))` – Michael Dodd Jun 19 '18 at 16:13
  • Finally, you don't need the `;` at the end of the `setFragment` function. – Michael Dodd Jun 19 '18 at 16:14
  • And it looks like a closing bracket `)` needs to be added after `(new BottomNavigationView.OnNavigationItemSelectedListener()`, before the opening curly brace `{`. It would help if you included the error message(s) that you are getting. – Christopher Jun 19 '18 at 16:23

1 Answers1

2

Dear You started a method parenthesis named setOnNavigationItemSelectedListener but never closed, please close the method parenthesis as below:

default:
       return false;
     }
   }
 });
}


public void setFragment(Fragment fragment) {
    FragmentTransaction fragmentTransaction = 
    getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame_utama, fragment);
    fragmentTransaction.commit();
}
krisz
  • 2,686
  • 2
  • 11
  • 18