I\'m trying to open a dialog window, but every time I try to open it it throws this exception:
Uncaught handler: thread main exiting due to uncaught exceptio
For nested dialogs this issue is very common, It works when
AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(MyActivity.this);
is used instead of
mDialogBuilder = new AlertDialog.Builder(getApplicationContext);
this alternative.
This Worked for me--
new AlertDialog.Builder(MainActivity.this)
.setMessage(Html.fromHtml("<b><i><u>Spread Knowledge Unto The Last</u></i></b>"))
.setCancelable(false)
.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).show();
Use
ActivityName.this
You can also do this
public class Example extends Activity {
final Context context = this;
final Dialog dialog = new Dialog(context);
}
This worked for me !!
Android documents suggests to use getApplicationContext();
but it will not work instead of that use your current activity while instantiating AlertDialog.Builder or AlertDialog or Dialog...
Ex:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
or
AlertDialog.Builder builder = new AlertDialog.Builder((Your Activity).this);
Another solution is to set the window type to a system dialog:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
This requires the SYSTEM_ALERT_WINDOW permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
As the docs say:
Very few applications should use this permission; these windows are intended for system-level interaction with the user.
This is a solution you should only use if you require a dialog that's not attached to an activity.
public class Splash extends Activity {
Location location;
LocationManager locationManager;
LocationListener locationlistener;
ImageView image_view;
ublic static ProgressDialog progressdialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
progressdialog = new ProgressDialog(Splash.this);
image_view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
locationManager.requestLocationUpdates("gps", 100000, 1, locationlistener);
Toast.makeText(getApplicationContext(), "Getting Location plz wait...", Toast.LENGTH_SHORT).show();
progressdialog.setMessage("getting Location");
progressdialog.show();
Intent intent = new Intent(Splash.this,Show_LatLng.class);
// }
});
}
Text here:-
use this for getting activity
context for progressdialog
progressdialog = new ProgressDialog(Splash.this);
or progressdialog = new ProgressDialog(this);
use this for getting application context for BroadcastListener
not for progressdialog
.
progressdialog = new ProgressDialog(getApplicationContext());
progressdialog = new ProgressDialog(getBaseContext());