I knew this question has been asked but I couldn't solve it. I want to show simple AlertDialog on Android's Service. I can show it nicely on MainActivity but I had a problem on Service , Here is my code:
CustomMainActivity.java:
public void popupDialogMain()
{
final Context context = getApplicationContext();
Handler h1 = new Handler(context.getMainLooper());
h1.post(new Runnable() {
#Override
public void run() {
if (mBXmpp)
mBXmppService.popupDialogMain2();
}
});
}
XmppService.java:
public static void popupDialogMain2()
{
AlertDialog.Builder builder = new AlertDialog.Builder(CustomMainActivity.this)
.setMessage("Look at this dialog!")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
}
I get an error on this line:
AlertDialog.Builder builder = new AlertDialog.Builder(CustomMainActivity.this)
Android Manifest:
I added this permission:
android.permission.SYSTEM_ALERT_WINDOW
And I get this error:
not an enclosing class: CustomMainActivity
Any suggestion to solve it?
You cannot access a this from a static method.
Related
Please help me guys. I tried to put SetCancelable(false) in dialogfragment but still not working.
this is my DialogFragment:
public static class UsageAcessDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Grant Usage Access permission")
.setTitle("Usage Access Permission")
.setCancelable(false)
.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
startActivityForResult(
new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Thank you for those who will comment.
You dont need to builder! you are inside dialog fragment
try this one setCancelable(false)
insted of using builder.setCancelable(false)
You don't need to associate setCancelable with the builder. write setCancelable method directly because you are inside a DialogFragment
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
builder.setTitle("Usage Access Permission");
builder.setMessage("Grant Usage Access permission");
setCancelable(false); // this line
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
tartActivityForResult(
new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS);
}
});
return builder.create();
}
I have currently working with some application and exploring things and I encountered this error. This just happened out of nowhere and earlier it seems working fine.
I read some questions related to my problem but I can't relate my codes to them so I am really confused. I'm still beginner so any help would be appreciated.
Logcat
4386-4386/com.example.gab.quadrantms E/WindowManager: android.view.WindowLeaked: Activity com.example.gab.quadrantms.Home has leaked window DecorView#abb868f[] that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:576)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:363)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:128)
at android.app.Dialog.show(Dialog.java:454)
at com.example.gab.quadrantms.SurveysFragment$1$1$1.onDataChange(SurveysFragment.java:120)
Fragment
if (getActivity() != null) {
if (myPosition.equals("Project Manager")) {
bottomSheetDialog = new BottomSheetDialog(getActivity());
final View bottomSheetDialogView = getLayoutInflater().inflate(R.layout.bottom_dialog_layot, null);
bottomSheetDialog.setContentView(bottomSheetDialogView);
View viewView = bottomSheetDialogView.findViewById(R.id.view);
View assignView = bottomSheetDialogView.findViewById(R.id.assign);
View deleteView = bottomSheetDialogView.findViewById(R.id.delete);
bottomSheetDialog.show(); //THIS LINE WHERE THE ERROR IS POINTING
if (bottomSheetDialog.isShowing()) {
assignView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String id = viewHolder.setSurveyID(model.getSurveyID());
String loc = viewHolder.setSurveyLocation(model.getSurveyLocation());
String type = viewHolder.setSurveyType(model.getSurveyType());
String progress = viewHolder.setProgress(model.getProgress());
if (progress.equals("Finished")) {
Toast.makeText(getContext(), "This project is Already Finished.", Toast.LENGTH_SHORT).show();
} else {
Intent surveyAssign = new Intent(getActivity(), AssignActivity.class);
surveyAssign.putExtra("id", id);
surveyAssign.putExtra("loc", loc);
surveyAssign.putExtra("type", type);
surveyAssign.putExtra("progress", progress);
startActivity(surveyAssign);
getActivity().finish();
}
}
});
deleteView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder message = new AlertDialog.Builder(getContext());
message.setMessage("Are you sure you want to Delete this Project?").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, int which) {
String id = viewHolder.setSurveyID(model.getSurveyID());
mRemoveDuplicate = FirebaseDatabase.getInstance().getReference().child("Surveys");
mRemoveDuplicate.child(id).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Project Deleted.", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = message.create();
alert.setTitle("Message");
alert.show();
}
});
Please add your Activity Name in All Dialog where your fragment is run.
Eg.
bottomSheetDialog = new BottomSheetDialog(YourActivity.this);
AlertDialog.Builder message = new AlertDialog.Builder(YourActivity.this);
I hope this can help You!
Thank You.
When does the app crashes? Is it while showing the dialog or while closing the dialog.
If its while closing the dialog, check whether you are dismissing the dialog properly.
Mostly WindowLeaked error occurs when you finish the parent activity before dismissing the dialog properly.
Note : If the app crashes during OnClick on assignView, finishing the activity here getActivity().finish(); in OnClickListener might be the problem.
I've got a class inside my MainActivity class from which I'm trying to display an AlertDialog. The Dialog isn't showing and there are no errors. The code for the dialog is:
AlertDialog.builder builder = new AlertDialog.builder(MainActivity.this);
builder.setMessage("Unkown Game Code. please try again.").setTitle("Game Code Error").setNeutralButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}
builder.create().show();
As the code is triggered when you receive a message via WebSocket, it might probably be async.
Try and run your code in your Activitys main Thread:
YourActivity.this.runOnUiThread(new Runnable() {
public void run() {
//Your Code
}
});
When I create AlertDialog in backgroud background thread AsyncTask, I get error. if I create AlertDialog outside of AsyncTask its working great. How I can fix that?
final ProgressDialog mDialog = new ProgressDialog(PageAndExercise.this);
mDialog.setMessage(getString(R.string.loading));
mDialog.setCancelable(false);
mDialog.show();
alertDialog2 = new AlertDialog.Builder(this);
AsyncTask.execute(new Runnable() {
#Override
public void run() {
mDialog.dismiss();
alertDialog2.setTitle(getString(R.string.tnxupload));
// Setting Dialog Message
alertDialog2.setMessage(getString(R.string.tnxupload2));
alertDialog2
.setCancelable(false)
.setPositiveButton("", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(getString(R.string.tnxupload3), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialog2.create();
// show it
alertDialog2.show();
}
});
The error:
28928-31794/com.example.exampleE/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #5
Process: com.example.example, PID: 28928
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
You should look more into what AsyncTask does and why.
It is a convenience class to allow work to be done in the background. The doInBackground method allows long-running work to not block the UI thread.
However, if you want to display or perform tasks on the UI thread, you need to make sure those happen on the UI thread. For example, your line:
mDialog.dismiss();
should execute in onPreExecute because it impacts the UI. Likewise:
alertDialog2.show()
is trying to change the UI. This should be run in onPostExecute.
Fundamentally, though, building an AlertDialog is not a long running task at all. None of that needs to be in AsyncTask unless you have intentions to expand what happens prior to displaying the next dialog.
See this post here: http://blog.nikitaog.me/2014/10/11/android-looper-handler-handlerthread-i/
You can't interact with UI non from main-Thread.
The short solution here is:
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
// Call UI related methods.
}
});
I guess AsyncTask implement to your code is something like this:
final ProgressDialog mDialog = new ProgressDialog(this);
mDialog.setMessage("loading");
mDialog.setCancelable(false);
mDialog.show();
final AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(this);
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
mDialog.dismiss();
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
AlertDialog alertDialog = alertDialog2.create();
alertDialog2.setTitle("tnxupload");
alertDialog2.setMessage("tnxupload");
alertDialog2.setCancelable(false).setPositiveButton("", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).setNegativeButton("tnxupload3", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alertDialog2.show();
}
#Override
protected Void doInBackground(Void... params) {
return null;
}
}.execute();
I'm trying to make a extern class for AlertDialog. I want to have an universal class to use it quickly.I know the code isn't difficult at all, but there are anyhow many rows to write (or copy) and if I would find a mistake I maybe had to change many code...
I've everything but one thing I don't get.
So it works but returning the correct onClick doesn't work.
I've also tried to make an while loop before returning, but then the app is hanging....
Has somebody any idea?
public class RalaAlertDialog{
private static AlertDialog.Builder alertDialog;
private static long onClick=RalaInterfaceDefault.FehlerSpezialZahl;
//neutralButton
public static long AlertDialogNeutral(Context class_this, String mssg, String ntrlBttnTxt, boolean dismissable, String title){
onClick=RalaInterfaceDefault.FehlerSpezialZahl; //default error number
alertDialog=new AlertDialog.Builder(class_this);
if(mssg.equals("")){
mssg="DEFAULT-TEXT";
}
if(title.equals("")){
title="DEFAULT-TITLE";
}
if(ntrlBttnTxt.equalsIgnoreCase("")){
System.out.println("No values set - default in use.");
ntrlBttnTxt="OK";
}
alertDialog.setMessage(mssg)
.setCancelable(dismissable);
alertDialog.setTitle(title);
alertDialog.setPositiveButton(ntrlBttnTxt,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
onClick=0;
dialog.dismiss();
}
}
);
AlertDialog a=alertDialog.create();
a.show();
//wait until button is click before continuing
return onClick;
}
public static AlertDialog getAlertDialog(Context ctx, String title, String message, String posButton, boolean dismissable, final DialogInterface.OnClickListener ocl) {
AlertDialog.Builder builder =new AlertDialog.Builder(ctx);
builder.setTitle(title)
.setMessage(message)
.setCancelable(dismissable)
.setPositiveButton(posButton,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.dismiss();
if(ocl!=null) ocl.onClick(dialog, id);
}
});
AlertDialog dialog = builder.create();
return dialog;
}
Use it like this :
AlertDialog dialog = getAlertDialog(this,"Hello","World","OK",false,new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("DIALOG","OK Clicked");
}
});
dialog.show();
Of course you need only one OnClickListener, but I like it better that way.