【发布时间】:2016-07-20 14:09:51
【问题描述】:
我正在学习开发 Android 应用程序,我正在使用抽屉模板,该模板使用工具栏并将其集成到操作栏中。当我尝试制作一个具有抽屉和操作栏操作的主 java 文件时,就会出现问题。然后每个实际布局将从主文件扩展以执行所需的活动。
首先,这是我遇到的错误。
03-30 23:10:24.895 22105-22105/com.mmaengineer.runique E/AndroidRuntime: 致命异常: main 进程:com.mmaengineer.runique,PID:22105 java.lang.RuntimeException:
无法启动 Activity ComponentInfo{com.mmaengineer.runique/com.mmaengineer.runique.MainActivity}:java.lang.IllegalStateException:此 Activity 已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。
原因:java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。 在 android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197) 在 android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
清单文件
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/app_name_settings">
</activity>
</application>
MainActivity.java
public class MainActivity extends MainControls {
public static final int TL = Toast.LENGTH_SHORT; // Toast.LENGTH_LONG for longer
public SharedPreferences mPrefs;
public String gameMode;
public String double_xp;
@Override
protected void onCreate(Bundle savedInstanceState) {
mPrefs = getSharedPreferences("LSprefs", 0);
gameMode = mPrefs.getString("gameMode", "");
double_xp = mPrefs.getString("double_xp", "");
if (gameMode == "") {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
((TextView)findViewById(R.id.textView2)).setText(gameMode);
((TextView)findViewById(R.id.textView3)).setText(double_xp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MainControls.java
public class MainControls extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
【问题讨论】: