我刚刚开始学习Android应用开发,现在我将为初学者创建一个简单的应用。
我遵循了YouTube上的教程,但是我不知道为什么在尝试运行我的应用程序时会出现一些错误。它说他们期望一个;,)等。我试图查看我的代码,并确保每一个(都有)和每行都有;
但是该列表在扩大,我不知道实际上是什么错误。
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();
};}
这是我在MainActivity.java上的代码。无论如何,如果您能回答并解释它,我将不胜感激。
答案 0 :(得分:1)
亲爱的您启动了一个名为setOnNavigationItemSelectedListener
的方法括号,但从未关闭,请按如下所示关闭方法括号:
default:
return false;
}
}
});
}
public void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_utama, fragment);
fragmentTransaction.commit();
}