【问题标题】:Android Studio: White screen on app runAndroid Studio:应用程序运行时出现白屏
【发布时间】:2016-05-25 12:49:54
【问题描述】:

我正在开发一个应用程序,当我运行该应用程序时,我会在启动画面之前出现白屏。

在初始屏幕上,我在后台创建了数据库,我也拥有推送通知注册码。对于推送通知注册,我参考this 链接。所以我的闪屏代码如下:

public class SplashScreenActivity extends AppCompatActivity {
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    private BroadcastReceiver mRegistrationBroadcastReceiver;

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

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
                boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
                if (sentToken) {
                    // TODO token sent to server
                } else {
                    // TODO show error that token not sent to server
                }
            }
        };

        if (checkPlayServices()) {
            // Start IntentService to register this application with GCM.
            Intent intent = new Intent(this, RegistrationIntentService.class);
            startService(intent);
        }

        InitializeScreen();
    }
    private boolean checkPlayServices() {
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (apiAvailability.isUserResolvableError(resultCode)) {
                apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i("Splash screen activity", "This device is not supported.");
                finish();
                }
            return false;
        }
        return true;
    }

    @Override
    protected void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
    }

    private void InitializeScreen() {
        new LoadDataBase(SplashScreenActivity.this).execute(SplashScreenActivity.this);
    }

    private class LoadDataBase extends AsyncTask<Context, Void, Void> {
        Context context;

        public LoadDataBase(Context context){
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Context... arg0) {
            // Create data base from assets folder.
            DatabaseHelper databaseHelper = new DatabaseHelper(arg0[0]);
            try {
                databaseHelper.createDataBase();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Closing the Data base.
            databaseHelper.close();

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pauseSplashScreen(context);
        }
    }

    public void pauseSplashScreen(final Context context) {
        // New Thread call.
        new Thread() {
            // Running Thread.
            public void run() {
                int count = 0;
                while (count < 5) {
                    try {
                        Thread.sleep(1000);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                    count++;
                }

                Intent intent = new Intent(context, Activity2.class);
                startActivity(intent);
                finish();
            }
        }.start();
    }
}

问题是:我在启动屏幕之前在应用启动时出现白屏,这可能是由于上面链接中提供的推送通知注册。

我应该怎么做才能避免出现白屏。请指导我。

【问题讨论】:

    标签: android android-studio google-cloud-messaging splash-screen


    【解决方案1】:

    您正在使用私有内部类来创建数据库。然后从那里调用 pauseSplashScreen(context)。然后开始一个新线程?为什么?

    我建议为异步任务提供它自己的类,并带有一个接口,以便将信号返回给您的调用活动。

    如果你使用

    Intent intent = new Intent(context, Activity2.class);
    startActivity(intent);
    

    您不必调用finish(),该调用最好用于完成当前活动并返回到前一个活动。 (我让你使用一个线程来不阻止你应用程序的其余部分运行,让那个线程回到你身边,然后移动到下一个活动不是更好吗?)

    所以,为了回答你的问题,我的猜测是你的 Activity 的启动需要一些时间,你看到的是一个加载的 blanc 布局。

    【讨论】:

    • 我删除了整个数据库创建代码和 pauseSplash 屏幕。只保留推送通知注册码,然后同样的事情发生了......
    • 可能是这个问题吗? stackoverflow.com/questions/36575229/… 否则:使用调试器?还是把Push Notification注册码移到onResume()中?
    【解决方案2】:

    只是因为你用的是debug版本,换到release版本就没有了,不用担心!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      相关资源
      最近更新 更多