从SplashScreenActivity(全屏)移动到MainActivity(非全屏)时应用程序崩溃

时间:2016-01-07 11:24:01

标签: android fullscreen splash-screen

我有SplashScreenActivity全屏,3秒后移动到MainActivity

styles.xml

SplashScreenActivity

<!-- Theme for Splash with no Action bar full screen -->
<style name="splash_screen" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>
styles.xml

MainActivity

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="vpiCirclePageIndicatorStyle">@style/CustomCirclePageIndicator</item>
</style>

当应用从全屏移动到非全屏时,SplashScreenActivityMainActivity移动到Layout getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 会出现,然后向下移动一点。

我已经使用过了

android:paddingTop="25dp"

以及

SplashScreenActivity

但没有运气。

任何人都可以提供这个问题的解决方案吗?

编辑:

public class SplashScreenActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); EventBus.getDefault().register(this); callWeatherService(this); delayedTask(); } private void delayedTask() { Runnable splashRunnable = new Runnable() { @Override public void run() { startActivity(new Intent(this, MainActivity.class)); } }; new Handler().postDelayed(splashRunnable, Numerics.THREE_THOUSAND); } public void onEventMainThread(WeatherModel weather) { if (weather != null) { Preferences.getIns().saveWeather(weather); } } @Override public void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }

MainActivity

public class MainActivity extends AppCompatActivity implements ExpandableListView.OnGroupClickListener, ExpandableListView.OnChildClickListener, ExpandableListView.OnGroupExpandListener, View.OnClickListener { private Toolbar mToolbar; private DrawerLayout mDrawerLayout; private FragmentManager mFragmentManager; private FragmentTransaction mFragmentTransaction; private ExpandableListAdapter mExpandableListAdapter; private ExpandableListView mExpandableListView; private List<String> mGroupList; private Map<String, List<String>> mChildList; private TextView toolbarTitle; private ImageView weatherImg; private int selectedItem = -1; private int lastExpandedPosition = -1; private TextView userEmailTxt; private TextView userNameTxt; private ImageView editBtn; public int getSelectedItem() { return selectedItem; } public void setSelectedItem(int selectedItem) { this.selectedItem = selectedItem; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayShowTitleEnabled(false); toolbarTitle = (TextView) findViewById(R.id.toolbar_title); weatherImg = (ImageView) findViewById(R.id.weather_img); weatherImg.setOnClickListener(this); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle( this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { super.onDrawerClosed(view); if (lastExpandedPosition != -1 ) { mExpandableListView.collapseGroup(lastExpandedPosition); } lastExpandedPosition = -1; } }; mDrawerLayout.setDrawerListener(mDrawerToggle); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); mDrawerToggle.syncState(); setDrawerList(); setUserDetails(); setFonts(); loadFragment(0); } private void setUserDetails() { userNameTxt = (TextView) findViewById(R.id.user_name); userEmailTxt = (TextView) findViewById(R.id.user_email); editBtn = (ImageView) findViewById(R.id.edit_btn); editBtn.setOnClickListener(this); userNameTxt.setText(R.string.lokes); userEmailTxt.setText(R.string.lokesh_mail); } private void setDrawerList() { mExpandableListView = (ExpandableListView) findViewById(R.id.expandable_lst); mExpandableListView.setGroupIndicator(null); prepareListData(); mExpandableListAdapter = new ExpandableListAdapter(this, mGroupList, mChildList); // setting list adapter mExpandableListView.setAdapter(mExpandableListAdapter); mExpandableListView.setOnGroupClickListener(this); mExpandableListView.setOnChildClickListener(this); mExpandableListView.setOnGroupExpandListener(this); } private void prepareListData() { // data to expandable list view } private void loadFragment(int position) { String title = null; invalidateOptionsMenu(); Fragment fragment = null; if (position == getSelectedItem()) { // Already selected fragment is being displayed } else { switch (position) { case Numerics.ZERO: fragment = new DashBoardFragment(); title = getString(R.string.dashboard); break; case Numerics.ONE: break; case Numerics.TWO: break; case Numerics.THREE: break; case Numerics.FOUR: break; case Numerics.FIVE: break; default: break; } if (fragment != null) { showFragment(fragment, title); } setSelectedItem(position); } } public void showFragment(Fragment fragment, String title) { if (fragment != null) { String backStateName = fragment.getClass().getName(); mFragmentManager = getSupportFragmentManager(); mFragmentTransaction = mFragmentManager.beginTransaction(); mFragmentTransaction.replace(R.id.dashboard_container, fragment); mFragmentTransaction.addToBackStack(backStateName); mFragmentTransaction.commit(); setActionBarTitle(title); if (backStateName == DashBoardFragment.class.getSimpleName()) { clearBackStack(); } } } public void setActionBarTitle(String title) { if (toolbarTitle != null && !TextUtils.isEmpty(title)) { toolbarTitle.setText(title); } } private void clearBackStack() { mFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); setSelectedItem(Numerics.ZERO); } @Override public void onBackPressed() { if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { mDrawerLayout.closeDrawers(); } else { int backStackCount = mFragmentManager.getBackStackEntryCount(); if (backStackCount > 0) { popFragments(backStackCount); } else { finish(); } } } private void popFragments(int backStackCount) { String fragmentTag = mFragmentManager.getBackStackEntryAt( backStackCount - 1).getName(); if (!TextUtils.isEmpty(fragmentTag)) { Fragment currentFragment = mFragmentManager .findFragmentById(R.id.dashboard_container); if (currentFragment != null && currentFragment instanceof DashBoardFragment) { finish(); } else { mFragmentManager.popBackStack(); } } } @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { if (mChildList.get(mGroupList.get(groupPosition)) .isEmpty() && mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { mDrawerLayout.closeDrawer(Gravity.LEFT); } return false; } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { v.setBackgroundResource(R.color.exp_lst_selected); if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { mDrawerLayout.closeDrawer(Gravity.LEFT); } openChild(groupPosition, childPosition); return false; } private void openChild(int groupPosition, int childPosition) { //handle on open child } @Override public void onGroupExpand(int groupPosition) { if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) { mExpandableListView.collapseGroup(lastExpandedPosition); } lastExpandedPosition = groupPosition; } private void setFonts() { Fonts.getInstance(this).setMediumFont(toolbarTitle); } @Override public void onClick(View v) { ToastFactory.showToast(R.string.app_name); } }

AndroidManifest.xml :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />

<application
    android:allowBackup="true"
    android:name=".activities.App"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".activities.MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme"/>

    <activity android:name=".activities.SplashScreenActivity"
        android:theme="@style/splash_screen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".services.rest.RestService" />
</application>

编辑2:

{{1}}

1 个答案:

答案 0 :(得分:0)

你有太多的行无效。这是我的例子,按预期工作: 在清单中没有:

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" />
</application>

在我的styles.xml中没有任何内容,只需从正确的主题扩展

 <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
           //nothing to do with fullscreen it all extended
</style>

启动活动仅LinearLayout。 主要活动根本没有覆盖窗口,您已经扩展了正确的主题。

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private Toolbar toolbar;
    private ActionBarDrawerToggle mDrawerToggle;

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        toolbar = (Toolbar) findViewById(R.id.main_page_toolbar_parent);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setTitle(null);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.action_settings, R.string.app_name) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportFragmentManager().beginTransaction().replace(R.id.container_frame, new MainActivityFragment()).commit();
    }
}

它适用于我需要Splash的所有应用程序。