1
Can anyone explain why the error occurs LISTADO NO FINAL DA PERGUNTA
, terminating the application, when I try to finalize the application through another class:
Classe1.class:
...
...
AlertDialogManager alert = new AlertDialogManager();
NetworkAvailable internet = new NetworkAvailable();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!internet.isNetworkAvailable(this)){
alert.showAlertDialog(MainActivity.this, "Sem internet!", false);
if (alert.Saindo()){
//O ERRO ACONTECE PELO FINISH(), EU QUERIA QUE ESSA FUNÇÃO EXECUTASSE
//DEPOIS QUE EU CANCELASSE LÁ NA classe3.
//O app NEM INICIA, se eu deixar esse finish aqui, mas se eu o retirar,
//O LOG executa:
Log("Finalizado","Fechando a aplicação!")
finish();
}
}
}
}
classe2.class:
public class NetworkAvailable{
public boolean isNetworkAvailable (final Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if(networkInfo != null && networkInfo.isConnected()){
/* LARGURA E AUTURA GERALToast.makeText(this, "LARGURA > " + largura, Toast.LENGTH_LONG).show();*/
Toast.makeText(context, "CONECTADO", Toast.LENGTH_LONG).show();
isAvailable = true;
} else {
Toast.makeText(context, "NÃO CONECTADO", Toast.LENGTH_LONG).show();
isAvailable = false;
}
return isAvailable;
}
}
classe3.class:
public class AlertDialogManager {
public boolean showAlertDialog(final Context context, final String title, final Boolean status) {
if(status == false){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setTitle(title);
builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.v("CANCELANDO", "cancelado");
showAlertDialog(context, title, true);
Saindo();
}
});
builder.setPositiveButton("Tentar novamente", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which)
{
final ConnectivityManager conectivtyManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected()) {
}
dialog.dismiss();
showAlertDialog(context, title, false);
}
});
AlertDialog dialog = builder.create(); // calling builder.create after adding buttons
dialog.show();
return status;
}
return status;
}
public boolean Saindo(){
Log.v("SAINDO", "saiu");
return true;
}
}
01-26 22:17:05.721: E/WindowManager(25592): Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418654f0 that was originally added here
01-26 22:17:05.721: E/WindowManager(25592): android.view.WindowLeaked: Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418654f0 that was originally added here
01-26 22:17:05.721: E/WindowManager(25592): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:351)
01-26 22:17:05.721: E/WindowManager(25592): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
01-26 22:17:05.721: E/WindowManager(25592): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
01-26 22:17:05.721: E/WindowManager(25592): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
01-26 22:17:05.721: E/WindowManager(25592): at android.view.Window$LocalWindowManager.addView(Window.java:537)
01-26 22:17:05.721: E/WindowManager(25592): at android.app.Dialog.show(Dialog.java:278)
01-26 22:17:05.721: E/WindowManager(25592): at ffitteste.favoritos.mapa.rafael.AlertDialogManager.showAlertDialog(AlertDialogManager.java:48)
01-26 22:17:05.721: E/WindowManager(25592): at ffitteste.favoritos.mapa.rafael.MainActivity.onCreate(MainActivity.java:103)
01-26 22:17:05.721: E/WindowManager(25592): at android.app.Activity.performCreate(Activity.java:4465)
01-26 22:17:05.721: E/WindowManager(25592): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-26 22:17:05.721: E/WindowManager(25592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-26 22:17:05.721: E/WindowManager(25592): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-26 22:17:05.721: E/WindowManager(25592): at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-26 22:17:05.721: E/WindowManager(25592): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-26 22:17:05.721: E/WindowManager(25592): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 22:17:05.721: E/WindowManager(25592): at android.os.Looper.loop(Looper.java:137)
01-26 22:17:05.721: E/WindowManager(25592): at android.app.ActivityThread.main(ActivityThread.java:4514)
01-26 22:17:05.721: E/WindowManager(25592): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 22:17:05.721: E/WindowManager(25592): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 22:17:05.721: E/WindowManager(25592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-26 22:17:05.721: E/WindowManager(25592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-26 22:17:05.721: E/WindowManager(25592): at dalvik.system.NativeStart.main(Native Method)
01-26 22:17:05.729: E/WindowManager(25592): Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41776208 that was originally added here
01-26 22:17:05.729: E/WindowManager(25592): android.view.WindowLeaked: Activity ffitteste.favoritos.mapa.rafael.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41776208 that was originally added here
01-26 22:17:05.729: E/WindowManager(25592): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:351)
01-26 22:17:05.729: E/WindowManager(25592): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279)
01-26 22:17:05.729: E/WindowManager(25592): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
01-26 22:17:05.729: E/WindowManager(25592): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
01-26 22:17:05.729: E/WindowManager(25592): at android.view.Window$LocalWindowManager.addView(Window.java:537)
01-26 22:17:05.729: E/WindowManager(25592): at android.app.Dialog.show(Dialog.java:278)
01-26 22:17:05.729: E/WindowManager(25592): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
01-26 22:17:05.729: E/WindowManager(25592): at ffiteteste.favoritos.servicos.rafael.GPSTracker.showSettingsAlert(GPSTracker.java:194)
01-26 22:17:05.729: E/WindowManager(25592): at ffitteste.favoritos.mapa.rafael.MainActivity.GPSzoom(MainActivity.java:819)
01-26 22:17:05.729: E/WindowManager(25592): at ffitteste.favoritos.mapa.rafael.MainActivity.onCreate(MainActivity.java:164)
01-26 22:17:05.729: E/WindowManager(25592): at android.app.Activity.performCreate(Activity.java:4465)
01-26 22:17:05.729: E/WindowManager(25592): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-26 22:17:05.729: E/WindowManager(25592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-26 22:17:05.729: E/WindowManager(25592): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-26 22:17:05.729: E/WindowManager(25592): at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-26 22:17:05.729: E/WindowManager(25592): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-26 22:17:05.729: E/WindowManager(25592): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 22:17:05.729: E/WindowManager(25592): at android.os.Looper.loop(Looper.java:137)
01-26 22:17:05.729: E/WindowManager(25592): at android.app.ActivityThread.main(ActivityThread.java:4514)
01-26 22:17:05.729: E/WindowManager(25592): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 22:17:05.729: E/WindowManager(25592): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 22:17:05.729: E/WindowManager(25592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-26 22:17:05.729: E/WindowManager(25592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-26 22:17:05.729: E/WindowManager(25592): at dalvik.system.NativeStart.main(Native Method)
Someone manifests please kkkk
– Lollipop
It could include the stacktrace that appears in the logcat?
– Wakim
They answered this: Make your function return a Boolean and Activity itself closes. In Android architecture vc should not keep instance of Activity in another class or Activity.
– Lollipop
That’s right, @Wakim?
– Lollipop
It doesn’t have that right or wrong, but it’s good practice not to expose your
Activity
for coupling purposes. Whenever you need to pass an instance ofActivity
to another class, think about Pattern Observer/Listener. But in practical terms, this code works*. It works if you instill the objectNetworkAvailable
before use (hehe).– Wakim
How you’re spending Initiating the member
saindo
with the instance of the other Activity?– André Ribeiro
@Andréribeiro, all I have is this. It has how to make it work?
– Lollipop
Without stacktrace and the rest of the code it is difficult to identify the problem.
– André Ribeiro
I’ll edit the question.
– Lollipop