I can’t seem to get this to work, after a progressDialog finish display how can I display an alert dialog?
here’some relevant code:
progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
new Thread() {
public void run() {
try{
initializer();
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
progressDialog.dismiss();
SharedPreferences welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
welcome = welcome_pref.getString("getwelcome", "null");
if (welcome.equals("no")) {
}else{
AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show();
welcome.setContentView(R.layout.welcome);
welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor welc = welcome_pref.edit();
welc.putString("getwelcome", "no");
welc.commit();
}
}
}.start();
}
private void initializer() {
startMoving = (ImageView) findViewById(R.id.imMove);
aboutApp = (ImageView) findViewById(R.id.imAbout);
devSite = (ImageView) findViewById(R.id.imDevSite);
movingTips = (ImageView) findViewById(R.id.imTips);
rateApp = (ImageView) findViewById(R.id.imRate);
start = (EditText) findViewById(R.id.etFrom);
end = (EditText) findViewById(R.id.etTo);
startMoving.setOnClickListener(this);
aboutApp.setOnClickListener(this);
devSite.setOnClickListener(this);
movingTips.setOnClickListener(this);
rateApp.setOnClickListener(this);
tvabout = (TextView) findViewById(R.id.tvabout);
tvstartmove = (TextView) findViewById(R.id.tvStart);
tvrate = (TextView) findViewById(R.id.tvRate);
tvdevelop = (TextView) findViewById(R.id.tvDev);
tvmovetips = (TextView) findViewById(R.id.tvTips);
tvabout.setShadowLayer(30, 0, 0, 0xff000000);
tvstartmove.setShadowLayer(30, 0, 0, 0xff000000);
tvrate.setShadowLayer(30, 0, 0, 0xff000000);
tvdevelop.setShadowLayer(30, 0, 0, 0xff000000);
tvmovetips.setShadowLayer(30, 0, 0, 0xff000000);
checklistitem();
}
private void checklistitem() {
SharedPreferences createlistitem_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
checkitem = createlistitem_pref.getString("getcheck", "null");
if (checkitem.equals("no")) {
}else{
SQLHandler checkitemstat = new SQLHandler(MainActivity.this);
checkitemstat.open();
checkitemstat.createList();
checkitemstat.close();
SharedPreferences setitem_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor setcheck = setitem_pref.edit();
setcheck.putString("getcheck", "no");
setcheck.commit();
}
}
}
I’m getting an error saying Can't create handler inside thread that has not called Looper.prepare() I know whats causing this error but i don’t know how to fix this.
This line of code is the one that’s causing this.
progressDialog.dismiss();
SharedPreferences welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
welcome = welcome_pref.getString("getwelcome", "null");
if (welcome.equals("no")) {
}else{
AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show();
welcome.setContentView(R.layout.welcome);
welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor welc = welcome_pref.edit();
welc.putString("getwelcome", "no");
welc.commit();
}
Add your Alert Dialog show code within runonUiThread,
Something like this,
As one of the answer suggested here says, you can’t update your UI from a worker thread. You got to be in UI thread when you update any UI.
So you need to use handlers or runonUiThread to do this. But since runOnUiThread internally uses handlers it should do help you.