我有我的AsyncTask,唯一需要注意的是从JSON数组中显示列表使用Retrofit2。但由于数据过于广泛,我决定在应用程序完成工作时发出消息。
private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
private ProgressDialog pDialog;
public MyAsyncTask() {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage(MainActivity.this.getResources().getString(R.string.sync));
pDialog.setIndeterminate(false);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
sincronizacionDB();
Log.e("THREAD_BACKGROUND","SE HA REALIZADO INSERCION DE: " + sqLiteHandler.getLastIdBeneficio());
return null;
}
@Override
protected void onPostExecute(Void result) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
Log.e("THREAD","SE HA REALIZADO INSERCION DE: " + sqLiteHandler.getLastIdBeneficio());
openAll();
}
private void openAll(){
ListadoFragment listadoFragment = new ListadoFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frameContainer, listadoFragment).commit();
}
private void sincronizacionDB(){
if(dbCheck(MainActivity.this,"beneficiosColaborador.db")){
}else{
Call<List<Beneficios>> callBeneficios = restManager.getApiService().getListadoBeneficios();
callBeneficios.enqueue(new Callback<List<Beneficios>>() {
@Override
public void onResponse(Call<List<Beneficios>> call, Response<List<Beneficios>> response) {
List<Beneficios> beneficioData = response.body();
for(Beneficios b : beneficioData){
Log.e("beneficio",b.getIdBeneficio() + " " + b.getNombreBeneficio());
}
}
@Override
public void onFailure(Call<List<Beneficios>> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
}
和MainActivity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
CommonMethod commonMethod = new CommonMethod();
SQLiteHandler sqLiteHandler = new SQLiteHandler(this);
SQLiteDatabase sqLiteDatabase;
private Spinner spinner;
private RestManager restManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
commonMethod.setActivity(this);
commonMethod.setTaskBarColored(R.color.smooth_material_red);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
spinner = (Spinner)findViewById(R.id.spinner_nav);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
new MyAsyncTask().execute();
Log.e("Finalido","ASYNC HA FINALIZADO");
但经过多次尝试,我无法弄清楚这个错误背后的原因。
错误日志
E/WindowManager: android.view.WindowLeaked: Activity com.freelance.crdzbird_dev.clarobadge.UI.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{145dc7ae V.E..... R......D 0,0-480,174} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at com.freelance.crdzbird_dev.clarobadge.UI.MainActivity$MyAsyncTask.onPreExecute(MainActivity.java:238)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591)
at android.os.AsyncTask.execute(AsyncTask.java:539)
at com.freelance.crdzbird_dev.clarobadge.UI.MainActivity.onCreate(MainActivity.java:60)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2650)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
地点位于 DO IN BACKGOUND ,当我致电 pDialog.show()