该活动主要是在导航抽屉上进行后,我的应用程序崩溃了……有人可以帮我吗...
下面是我的Introduction.java代码
public class Introduction extends AppCompatActivity{
//POP UPS
ImageView getstart;
//image button
MediaPlayer background_music;
int length;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.introduction);
bmusic();
getstart=(ImageView)findViewById(R.id.playnow);
final MediaPlayer button= MediaPlayer.create(this,R.raw.intro_button);
getstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.start();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Intent goMain= new Intent(Introduction.this,MainActivity.class);
startActivity(goMain);
finish();
}
}, 1000);
}
});
}
public void bmusic() {
background_music = MediaPlayer.create(this, R.raw.intro_adventure);
background_music.setLooping(true);
background_music.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
background_music.pause();
length = background_music.getCurrentPosition();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
background_music.seekTo(length);
background_music.start();
}
}
这是我在MainActivity.java中的代码
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open
, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new fragment_home()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new fragment_home()).commit();
break;
case R.id.nav_tips:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new fragment_tips()).commit();
break;
case R.id.nav_about:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new fragment_about()).commit();
break;
case R.id.share:
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
break;
case R.id.rate:
Toast.makeText(this, "Rate this app", Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
这是我的logcat上的错误
E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.james.navigate, PID: 4947
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.james.navigate/com.james.navigate.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:201)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
at com.james.navigate.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6915)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
答案 0 :(得分:1)
之所以会这样,是因为您的样式具有caea
,并且在Java代码中声明了ActionBar
。
因此,将其添加到您的Toolbar
:
styles.xml
这将表明此主题没有<item name="windowActionBar">false</item>
,但是您可以在Java端声明ActionBar
或者只使用父级不 Toolbar
:
ActionBar
第二个是最简单的方法。
更新:(适用于AndroidX或最多28个API)
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">