I have an AlertDialog it works fine but I noticed that it marks me an error in the line where I put dialog.show (); I do not know how I could solve it.
IngresarAdmin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final AlertDialog.Builder mBuilder=new AlertDialog.Builder(InicioSesion_Principal.this);
View Mview= getLayoutInflater().inflate(R.layout.zona_restrigida,null);
final EditText claveAdmin=(EditText)Mview.findViewById(R.id.Clave_proveedor);
Button EntrarClaveAdmin=(Button)Mview.findViewById(R.id.Ingresar_clave_proovedor);
EntrarClaveAdmin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!claveAdmin.getText().toString().isEmpty()){
Intent intent=new Intent(getApplicationContext(), menu_administrador.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
finish();
}
else {
Toast.makeText(InicioSesion_Principal.this,"Clave incorrecta",Toast.LENGTH_SHORT).show();
}
}
});
mBuilder.setView(Mview);
AlertDialog dialog=mBuilder.create();
dialog.show();
}
});
This is the error that shows me in the dialog.show();
It does not close the application at any time, but how can I fix it?
android.view.WindowLeaked: Activity com.tienda.app.aplicaciontienda.Iniciar_Sesion_3.InicioSesion_Principal has leaked window com.android.internal.policy.PhoneWindow$DecorView{245d004 V.E...... R......D 0,0-683,503} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:375)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:319)
at com.tienda.app.aplicaciontienda.Iniciar_Sesion_3.InicioSesion_Principal$3.onClick(InicioSesion_Principal.java:119)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
try using dialog.create().show();
I had the same problem.
You need to call .dismiss();
if(dialog != null)
dialog.dismiss();
Related
I am creating a code that checks for the current location, however, when I check for the current location, it crashes. I looked into Stack Trace and saw this. I read other forums but I am not familiar with their solutions. I am also a beginner in Java. I used an Amazon Fire Tablet to download this app. When I tried testing it on the emulator, the app did not crash.
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.LOCALE_SETTINGS }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1797)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
at android.app.Activity.startActivityForResult(Activity.java:3761)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:3722)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at android.app.Activity.startActivity(Activity.java:4032)
at android.app.Activity.startActivity(Activity.java:4000)
at com.example.calculatorandlocationfinderfinal.MainActivity$7.onClick(MainActivity.java:181)
at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5491)
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:984)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Below is the code where it happened.
private void onGPS() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_LOCALE_SETTINGS));
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Always use this method to start intents other than your app's -
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
if(intent.resolveActivity(context.getPackageManager()) != null){
startActivity(intent);
}else{
//handle activity not found
}
this way you won't get ActivityNotFoundException.
You can also use try catch as -
try {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
} catch (e: ActivityNotFoundException) {
//handle activity not found
}
I suppose you want to navigate to GPS settings screen.
try this.
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
The phone/device might be missing locale settings screen altogether which leads to the crash.
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //This line solve my issue
startActivity(viewIntent);
public class ProgressCircleActivity extends AppCompatActivity {
private ProgressDialog progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_circle_activity);
progressBar = new ProgressDialog(ProgressCircleActivity.this);
progressBar.setMessage("Please wait ...");
progressBar.setCancelable(true);
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3*1000);
Intent i=new Intent(ProgressCircleActivity.this,PhoneNumberLogin.class);
startActivity(i);
//Remove activity
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.dismiss();
}
}).start();
}
}
Here is my logcat
10-05 16:12:22.577 27797-27797/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
10-05 16:12:32.397 27797-27797/com.example.dis015.meetdax2danish E/WindowManager: android.view.WindowLeaked: Activity com.example.dis015.meetdax2danish.ProgressCircleActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{25b6bc4a V.E..... R......D 0,0-501,174} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:520)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:287)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:325)
at com.example.dis015.meetdax2danish.ProgressCircleActivity.onCreate(ProgressCircleActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6575)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3143)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)
at android.app.ActivityThread.access$1000(ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
This is my splashScreenActivity when i open the app is getting crash.But in some mobiles the app is not crashing.I dont know what is the issue please help me to sort out
You did not dismiss the progressbar so it is showing that leaked window message.
Before finishing the activity please dismiss the progressbar dialog.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_circle_activity);
progressBar = new ProgressDialog(ProgressCircleActivity.this);
progressBar.setMessage("Please wait ...");
progressBar.setCancelable(true);
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3*1000);
Intent i=new Intent(ProgressCircleActivity.this,PhoneNumberLogin.class);
startActivity(i);//dismiss the progressdialog
progressBar.dismiss();
//Remove activity
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I have created an app with 5 tabs, in my 1st tab I am having a button on click of which I want to go to tab3. This is my code of button event --
moreButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(view.getContext(), Tab3_Image.class);
view.getContext().startActivity(intent);
}
});
But while clicking the button the app crash, below is the logcat error --
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.s2s.doupnow, PID: 22481
java.lang.NullPointerException
at com.s2s.doupnow.Tab1_Home$6.onClick(Tab1_Home.java:313)
at android.view.View.performClick(View.java:4444)
at android.view.View$PerformClick.run(View.java:18457)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(Native Method)
Change this -:
moreButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(view.getContext(), Tab3_Image.class);
view.getContext().startActivity(intent);
}
});
To-:
private Context context=this; //make it global
Button moreButton3=(Button)findViewById(R.id.buttoninxml);
moreButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, Tab3_Image.class);
startActivity(intent);
}
});
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
My application has a Progress Dialog for login process and when the orientation is changed while dialog box is open, app crashes.This all works fine, except when screen orientation changes while the dialog is up. At this point the app crashes. I am figuring out this issue from the last 3 nights but not able to get it, please help.
My fragment:
public class Example extends Fragment {
private static final String TAG = "LoginActivity";
private static final int REQUEST_SIGNUP = 0;
Unbinder unbinder;
#BindView(R.id.input_email) EditText _emailText;
#BindView(R.id.input_password) EditText _passwordText;
#BindView(R.id.btn_login) Button _loginButton;
#BindView(R.id.link_signup) TextView _signupLink;
#Override
public void onDestroyView() {
super.onDestroyView();
// unbind the view to free some memory
unbinder.unbind();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.Example, container, false);
unbinder=ButterKnife.bind(this,rootView);
_loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login();
}
});
_signupLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Intent create= new Intent(getActivity(),NewAccount.class);
startActivity(create);
}
});
return rootView;
}
public void login() {
Log.d(TAG, "Login");
if (!validate()) {
onLoginFailed();
return;
}
_loginButton.setEnabled(false);
final ProgressDialog progressDialog = new ProgressDialog(getActivity(),
R.style.AppTheme_Dark_Dialog);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Authenticating...");
progressDialog.show();
//new YourAsynTask(getActivity()).execute();
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
// TODO: Implement your own authentication logic here.
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
// On complete call either onLoginSuccess or onLoginFailed
onLoginSuccess();
// onLoginFailed();
progressDialog.dismiss();
}
}, 3000);
}
#Override
public void onPause() {
Log.e("DEBUG", "OnPause of loginFragment1");
super.onPause();
}
public void onLoginSuccess() {
_loginButton.setEnabled(true);
Intent i=new Intent(getActivity(),SuccessLogin.class);
startActivity(i);
}
public void onLoginFailed() {
Toast.makeText(getActivity(), "Login failed", Toast.LENGTH_LONG).show();
_loginButton.setEnabled(true);
}
public boolean validate() {
boolean valid = true;
String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
_emailText.setError("enter a valid email address");
valid = false;
} else {
_emailText.setError(null);
}
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
_passwordText.setError("between 4 and 10 alphanumeric characters");
valid = false;
} else {
_passwordText.setError(null);
}
return valid;
}
Logcat output:
11-16 19:20:10.955 4022-4022/com.example.a1332931.login_application E/WindowManager: android.view.WindowLeaked: Activity com.example.a1332931.login_application.TabActivity has leaked window com.android.internal.policy.PhoneWindow$DecorView{42b6135 V.E...... R......D 0,0-683,232} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:375)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:319)
at com.example.a1332931.login_application.Example.login(Example.java:156)
at com.example.a1332931.login_application.Example$1.onClick(Example.java:67)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-16 19:20:10.957 4022-4095/com.example.a1332931.login_application E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb8aa6c60
11-16 19:20:12.512 4022-4022/com.example.a1332931.login_application E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a1332931.login_application, PID: 4022
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference
at com.example.a1332931.login_application.Example.onLoginSuccess(Example.java:200)
at com.example.a1332931.login_application.Example$3.run(Example.java:168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Add this configuration change in your Android manifest activity:
<activity
android:name="YourActivity"
android:configChanges="orientation|keyboardHidden|screenSize"/>
My application is crash and in logs I see this, I try to find how I can fix it , but only I find that is cause a contex and a peole use YourActivityName.this and it works fine , but I have like this and my applications is crash
This is logs :
Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy#482fc81 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:584)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:86)
at android.app.Dialog.show(Dialog.java:322)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953)
at pl.eltegps.smokkomunikator.ui.activity.MainActivity.runServices(MainActivity.java:505)
at pl.eltegps.smokkomunikator.ui.activity.MainActivity.onStart(MainActivity.java:316)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
at android.app.Activity.performStart(Activity.java:6288)
at android.app.Activity.performRestart(Activity.java:6334)
at android.app.ActivityThread.handleSleeping(ActivityThread.java:3688)
at android.app.ActivityThread.access$2900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
This is code :
protected void runServices() {
if (LocationUtil.isLocationEnabled(getBaseContext())) {
if (checkPermission()) {
runLocationService();
}
} else {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(getResources().getString(R.string.location_settings_disabled));
dialog.setPositiveButton(getString(R.string.go_to_location_settings), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
App.getBus().post(new PageNavigateEvent(Const.APP_SETTINGS_LOCATION));
}
});
dialog.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
finish();
}
});
dialog.show();
}
runTrackerService();
runConfigService();
}
and this line cause errors :
dialog.show();
Add Current ActivityName.this instead of this.
AlertDialog.Builder dialog = new AlertDialog.Builder(YourCurrentActivityName.this);