gpt4 book ai didi

安卓工作室 : White screen on app run

转载 作者:行者123 更新时间:2023-12-03 18:08:56 25 4
gpt4 key购买 nike

我正在开发一个应用程序,当我运行该应用程序时,我会在启动屏幕之前获得白屏。

在启动画面上,我在后台创建了数据库,我也有推送通知注册码。对于推送通知注册,我引用 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();
}
}

问题是: 我在启动屏幕前的应用程序启动时出现白屏这可能是由于上述链接中给出的推送通知注册。

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

最佳答案

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

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

如果你使用

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

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

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

关于安卓工作室 : White screen on app run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37437705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com