尝试扩展为BaseActivity时出错

时间:2017-03-19 07:23:32

标签: android

我正在尝试将Nav Drawer活动创建为基本活动。我做了类似下面的事情。

当我跑步时,我得到了以下错误。

  

E / AndroidRuntime:致命异常:主要流程:   com.bugmanagement.pankaj.androidexample,PID:2297
  java.lang.RuntimeException:无法启动活动   ComponentInfo {com.bugmanagement.pankaj.androidexample / com.bugmanagement.pankaj.androidexample.Activities.UserManagement.Auth.LoginActivity}:   java.lang.IllegalStateException:此Activity已经有一个动作   酒吧提供的窗户装饰。不要求   Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false   您的主题是使用工具栏。在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)       在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)       在android.app.ActivityThread.-wrap12(ActivityThread.java)处   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)
  在android.os.Handler.dispatchMessage(Handler.java:102)at   android.os.Looper.loop(Looper.java:154)at   android.app.ActivityThread.main(ActivityThread.java:6077)at   java.lang.reflect.Method.invoke(Native Method)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
  引起:java.lang.IllegalStateException:此Activity已经有   窗户装饰提供的动作栏。不要求   Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false   您的主题是使用工具栏。在   android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:199)       在   android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:133)       在   com.bugmanagement.pankaj.androidexample.Activities.UserManagement.BaseActivity.onCreate(BaseActivity.java:31)       在   com.bugmanagement.pankaj.androidexample.Activities.UserManagement.Auth.LoginActivity.onCreate(LoginActivity.java:30)       在android.app.Activity.performCreate(Activity.java:6662)at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)       在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)       在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)       在android.app.ActivityThread.-wrap12(ActivityThread.java)处   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)       在android.os.Handler.dispatchMessage(Handler.java:102)at   android.os.Looper.loop(Looper.java:154)at   android.app.ActivityThread.main(ActivityThread.java:6077)at   java.lang.reflect.Method.invoke(Native Method)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

style.xml位于

之下
<resources>

    <!-- Base application theme. -->
    <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" />

</resources>

styles.xml(V21)

<resources>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

主要活动

public class MainActivity extends BaseActivity {

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

基本活动

public class BaseActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

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

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.base, menu);
        return true;
    }

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

2 个答案:

答案 0 :(得分:0)

问题在于:

  

此活动已有窗口装饰提供的操作栏。请勿在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏。

您正在使用带有操作栏的主题,同时在xml中使用工具栏并设置为操作栏。

在style.xml中使用没有操作栏的主题更改AppTheme的父级:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar" >
   <item name="windowActionBar">false</item>
   <item name="windowNoTitle">true</item>
   <!-- your item here -->
</style>

编辑:

用于导航图标

在你的活动中一定要有类似的东西:

    DrawerLayout drawerLayout;
    ActionBarDrawerToggle actionBarDrawerToggle;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_closed);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);

        //your code
    }


    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

希望这有帮助。

抱歉我的英文。

答案 1 :(得分:0)

  

请勿请求Window.FEATURE_SUPPORT_ACTION_BAR并进行设置   windowActionBar在主题中为false

以下是问题的根源,因为您设置了<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>

在您将工具栏设置为actionBar的活动中 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);

从style.xml文件中删除此行<item name="windowActionBar">false</item>

你可以使用这段代码没有动作栏主题样式。在样式文件中添加此代码

<style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>

    <item name="android:windowBackground">@color/primary</item>

    <item name="colorControlNormal">@color/iron</item>
    <item name="colorControlActivated">@color/white</item>
    <item name="colorControlHighlight">@color/white</item>
    <item name="android:textColorHint">@color/iron</item>

    <item name="colorButtonNormal">@color/primary_darker</item>
    <item name="android:colorButtonNormal" tools:targetApi="lollipop">@color/primary_darker</item>

    <item name="android:typeface">monospace</item>
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>