I am getting this error in my published application, only clients receive this error. I already tried several times to replicate the same mistake however unsuccessfully.
I also already tried to use the below code at all locations where there is a Dialog but also not solved.
if (dialog.isShowing ()) {
dialog.dismiss ();
}
The error report
java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{16faa139 V.E..... R.....I. 0,0-0,0} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:412)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:338)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:122)
at android.app.Dialog.dismissDialog(Dialog.java:522)
at android.app.Dialog.dismiss(Dialog.java:504)
**at br.my.project.de.a(Unknown Source)
at br.my.project.de.onPostExecute(Unknown Source)**
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
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)
I could see that you are trying to dismiss a ProgressDialog on the postExecute of an AsyncTask. This in itself is a good practice but is sometimes buggy, I kind of experienced this also before especially while you're showing the ProgressDialog and suddenly rotate the view.
A solution I've found to fix this is below:
You will need these function to handle the proper dismissal and avoid crashes.
private void dismissProgressDialog(ProgressDialog progressDialog) {
if (progressDialog != null) {
if (progressDialog.isShowing()) {
//get the Context object that was used to create the dialog
Context context = ((ContextWrapper) progressDialog.getContext()).getBaseContext();
// if the Context used here was an activity AND it hasn't been finished or destroyed
// then dismiss it
if (context instanceof Activity) {
// Api >=17
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (!((Activity) context).isFinishing() && !((Activity) context).isDestroyed()) {
dismissWithExceptionHandling(progressDialog);
}
} else {
// Api < 17. Unfortunately cannot check for isDestroyed()
if (!((Activity) context).isFinishing()) {
dismissWithExceptionHandling(progressDialog);
}
}
} else
// if the Context used wasn't an Activity, then dismiss it too
dismissWithExceptionHandling(progressDialog);
}
progressDialog = null;
}
}
public void dismissWithExceptionHandling(ProgressDialog dialog) {
try {
dialog.dismiss();
} catch (final IllegalArgumentException e) {
// Do nothing.
} catch (final Exception e) {
// Do nothing.
} finally {
dialog = null;
}
}
On your AsyncTask's onPostExecute implement the function.
#Override
protected void onPostExecute(Boolean b) {
// pass in the progressDialog as a parameter to the method
dismissProgressDialog(progressDialog);
}
fun Activity?.dismissDialog(dialog: Dialog?) {
if (isActivityActive()) {
dialog?.apply {
if (isShowing) dismiss()
}
}
}
fun Activity?.isActivityActive(): Boolean {
return null != this && !isFinishing && !isDestroyed
}
You are calling dismiss on a dialog that is currently not being shown anymore. As in: your Activity/Fragment is possibly already destroyed when you call dismiss.
Write this code in your activity's
onStop()
method.When anybody presses the home button and if dialog is opened than this error will come. Because on click of home button onPause() and onStop() method calls.Hope this helps.
if (dialog!=null && dialog.isShowing ()) {
dialog.dismiss ();
}
Related
I have made an android app using Android Studio and I have implemented Google AdMob into it.
However, when it comes to showing ads, whenever I try to show it from a function, it does not show.
Here is the function:
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.jodastudios.universalskyremote", Context.MODE_PRIVATE);
int clicks = sharedPreferences.getInt("clicks", 0);
sharedPreferences.edit().putInt("clicks", 0);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}
Whenever I show the ads on the OnCreate method, they show.
Here is the code:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.jodastudios.universalskyremote", Context.MODE_PRIVATE);
int clicks = sharedPreferences.getInt("clicks", 0);
sharedPreferences.edit().putInt("clicks", 0);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}
},9000);
I don't know why the ads are not showing from a function: does anybody know how to fix this?
Second code is delayed for 9 sec and that's enough time for the ad to load.
Function cannot display ad that has not been loaded!
You have to set AdListener and show add from onAdLoaded method or create callback to show from activity...
...or call your function from onAdLoaded(){
InterstitialAd interstitialAd = new InterstitialAd(context);
interstitialAd.setAdUnitId(adIdent);
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
interstitialAd.show();
//TODO or create callback to show from activity
if (adCallback != null) {
adCallback.onAdLoaded();
}
}
.....................
.....................
}
and
interstitialAd.loadAd(new AdRequest.Builder().build());
I want to update my app immediately every time I publish an update. I followed the instructions from the official android documentation on how to test the auto update feature , but nothing happens. I placed some logs in order to check whether these functions initialize, but the logcat shows nothing either. Is this a problem in my syntax, or should I place these functions somewhere else? Currently, all my update code is written inside the main, starting class of the app.
On create method of the class
private static final int REQUEST_APP_UPDATE = 560;
private AppUpdateManager appUpdateManager;
private InstallStateUpdatedListener installStateUpdatedListener;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
appUpdateManager = AppUpdateManagerFactory.create(this);
installStateUpdatedListener = new
InstallStateUpdatedListener() {
#Override
public void onStateUpdate(InstallState state) {
if (state.installStatus() == InstallStatus.DOWNLOADED){
} else if (state.installStatus() == InstallStatus.INSTALLED){
if (appUpdateManager != null){
appUpdateManager.unregisterListener(installStateUpdatedListener);
}
} else {
Log.i(TAG, "InstallStateUpdatedListener: state: " + state.installStatus());
}
}
};
appUpdateManager
.getAppUpdateInfo()
.addOnSuccessListener(
appUpdateInfo -> {
Log.d("TAG", "here");
// Checks that the platform will allow the specified type of update.
if ((appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE)
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE))
{
// Request the update.
try {
Log.d("TAG", "here");
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
REQUEST_APP_UPDATE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
});
appUpdateManager.registerListener(installStateUpdatedListener);
...
On resume and on stop handling methods:
#Override
protected void onResume() {
super.onResume();
appUpdateManager
.getAppUpdateInfo()
.addOnSuccessListener(
appUpdateInfo -> {
if (appUpdateInfo.updateAvailability()
== UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
// If an in-app update is already running, resume the update.
try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
REQUEST_APP_UPDATE);
Log.d("TAG", "tu");
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
});
}
#Override
protected void onStop() {
super.onStop();
if (appUpdateManager != null) {
appUpdateManager.unregisterListener(installStateUpdatedListener);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_APP_UPDATE) {
if (resultCode != RESULT_OK) {
Log.d("TAG", "Update flow failed! Result code: " + resultCode);
// If the update is cancelled or fails,
// you can request to start the update again.
}
}
}
I suggest you to move all your AppUpdateInfo retrieving to onResume() since it is more reliable entry point of an activity (for example, if Activity has came to background and then was opened again by user). OnCreate method will be called only if activity was destroyed, since that, you may not see update dialog after reopening application if it was minimized.
#Override
public void onResume() {
super.onResume();
appUpdateManager.getAppUpdateInfo().addOnSuccessListener( info -> {
boolean isStalledUpdate = info.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS;
boolean isReadyForUpdate =
info.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& info.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE;
if (isReadyForUpdate || isStalledUpdate) {
appUpdateManager.startUpdateFlowForResult(
info,
AppUpdateType.IMMEDIATE,
this,
REQ_CODE
);
}
}
Sometimes app is not automatically synchronized with Play Store updates, so you need to do the following:
Make sure you have an update available manually - go to Play Store, check for updates and make sure that you have an update available for your application.
After that open your app (your activity, that calls update manager in onResume) and you will see immediate update dialog.
Also, one tip for you - make your Activity implement InstallStateUpdateListener and override method onStateUpdate like this to handle different events.
#Override
public void onResume() {
// All previous logic
// If update is available or was stalled call this
appUpdateManager.registerListener(this);
}
#Override
public void onStateUpdate(InstallState state) {
if (state == null) return;
switch (state.installStatus()) {
case InstallStatus.INSTALLED:
appUpdateManager.unregisterListener(this)
return;
case InstallStatus.FAILED:
appUpdateManager.unregisterListener(this)
return;
case InstallStatus.CANCELED:
appUpdateManager.unregisterListener(this)
return;
default:
// provide your own logic
return;
}
}
This will help you to avoid calling separate instance of listener in activity lifecycle methods.
The code that I provided was working, the problem of not updating was a matter of the device itself not knowing whether an update is available. After refreshing the update list on the Play Store, the app update manager initialized the auto update window when starting the app.
I developed an application Android that detect NFC card then it open IsoDep connection with it to send APDU and get response. When application detect the NFC card it open a work session, I created a thread that every 500 ms check the isoDep connection, if connection was lost application close the work session (stop pooling , delete card data ...).
My application is device owner and is locked and it contain only one activity that start a webview to turn html code for user interfaces.
In The MainActivity, I stop the pooling in the onPause() before the super.onPause() and I restart it in the onResume() after the super.onResume().
Also I always call NfcAdapter.enableReaderMode() in onResume() and NfcAdapter.disableReaderMode in onPause().
//enable nfc dispatch
Bundle options = new Bundle();
options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 50);
NfcManager manager = (NfcManager) activity.
getApplicationContext().getSystemService(Context.NFC_SERVICE);
if(manager == null) {
return;
}
NfcAdapter nfcAdapter = manager.getDefaultAdapter();
if(nfcAdapter == null) {
return;
}
try {
nfcAdapter.enableReaderMode(activity,
new NfcAdapter.ReaderCallback() {
#Override
public void onTagDiscovered(final Tag tag) {
cardDetected(tag);
}
},
NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK |
NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
options);
} catch (Exception e) {
}
//disable nfc dispatch
NfcManager manager = (NfcManager)activity.getApplicationContext().getSystemService(Context.NFC_SERVICE);
if(manager == null) {
return;
}
NfcAdapter nfcAdapter = manager.getDefaultAdapter();
if(nfcAdapter == null) {
return;
}
try {
nfcAdapter.disableReaderMode(activity);
} catch (Exception e) {
}
I use Samsung Xcover4 Android 8 to test the application. And my problem is sometime when the onResume is called I see in the logcat a DeadObjectException :
E/NFC: NFC service dead - attempting to recover
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:777)
at android.nfc.INfcAdapter$Stub$Proxy.setAppCallback(INfcAdapter.java:1026)
at android.nfc.NfcActivityManager.requestNfcServiceCallback(NfcActivityMan ager.java:494)
at android.nfc.NfcActivityManager.onActivityResumed(NfcActivityManager.java:674)
at android.app.Application.dispatchActivityResumed(Application.java:240)
at android.app.Activity.onResume(Activity.java:1369)
at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:514)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1363)
at android.app.Activity.performResume(Activity.java:7432)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3780)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3845)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1773)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
When it happen card is never detected and I have to restart NFC or application to be able to detect card.
I have a custom android plugin for unity which opens a Webview activity. When I try to open the activity it crashes. When I try to initialise the WebView, below log statements appear from log cat.
Caused by: java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
at com.android.webview.chromium.WebViewChromium.createThreadException(WebViewChromium.java:252)
at com.android.webview.chromium.WebViewChromium.checkThread(WebViewChromium.java:267)
at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:163)
at android.webkit.WebView.<init>(WebView.java:661)
at android.webkit.WebView.<init>(WebView.java:596)
at android.webkit.WebView.<init>(WebView.java:579)
at android.webkit.WebView.<init>(WebView.java:566)
at android.webkit.WebView.<init>(WebView.java:556)
at com.vadrnet.unitysdk.MyWebViewClass.<init>(MyWebViewClass.java:31)
at com.vadrnet.unitysdk.WebViewActivity$1.run(WebViewActivity.java:207)
at android.app.Activity.runOnUiThread(Activity.java:5850)
at com.vadrnet.unitysdk.WebViewActivity.onCreate(WebViewActivity.java:205)
at android.app.Activity.performCreate(Activity.java:6582)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
My code for starting the activity via Unity Script is
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
AndroidJavaClass jc = new AndroidJavaClass("com.vadrnet.unitysdk.ManagerClass");
AndroidJavaObject className = jc.CallStatic<AndroidJavaObject>("instance");
className.Call("click", currentActivity, url);
}));
and the Click function in the Plugin is
public void click(Activity root, String url){
Intent intent = new Intent(root, WebViewActivity.class);
intent.putExtra("url", url);
root.startActivity(intent);
}
I couldn't get why the error occurs despite being opening the activity in the UI thread itself. Any help would be great.
you can use this bit of code may be it will be of help
please check this before posting
QA link
runOnUiThread(new Runnable() {
#Override
public void run() {
// Code for WebView goes here
}
});
// This code is BAD and will block the UI thread
webView.loadUrl("javascript:fn()");
while(result == null) {
Thread.sleep(100);
}
The best solution by the web is
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Your code
}
});
When you do that look here so see how to use runOnUiThread currently
Example on how to use it in Unity (documentation link):
void Start()
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
activity.Call("runOnUiThread", new AndroidJavaRunnable(runOnUiThread));
}
void runOnUiThread()
{
Debug.Log("I'm running on the Java UI thread!");
}
Another Example on how to use it in Unity: (Link to source)
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
public void run() {
// Run in Thread...
}
});
But you can also use
UserContrl1_LOadDataMethod()
{
string name = "";
if(myObject.InvokeRequired)
{
myObject.Invoke(new MethodInvoker(delegate { name = textbox1.text; }));
}
if(name == "MyName")
{
// do whatever
}
}
For more info you can use This Link
I have successfully sent an email in the background and displayed a progress dialog to the user as said email is sent. However when the user flips the screen while the dialog is up I get an IllegalArgumentException. I have tried using a WeakReference object and it doesn't seem to be fixing the problem. This is my AsyncTask class.
private class SendMailTask extends AsyncTask<Mail, String, EmailStatusResponce> {
private final WeakReference<ProgressDialog> weakReference;
public SendMailTask() {
ProgressDialog progressDialog = new ProgressDialog(SendReportActivity.this);
progressDialog.setMessage("Sending...");
progressDialog.setCancelable(false);
progressDialog.show();
weakReference = new WeakReference<>(progressDialog);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected EmailStatusResponce doInBackground(Mail... mail) {
return mail[0].send();
}
#Override
protected void onPostExecute(EmailStatusResponce result) {
super.onPostExecute(result);
if (result != null && weakReference != null) {
weakReference.get().dismiss(); // This is where the exception is thrown.
if (result.isSuccess()) {
Intent intent = new Intent(SendReportActivity.this, MainActivity.class);
startActivity(intent);
}
else {}
}
}
This is the exception
java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{688d3e2 V.E...... R......D 0,0-1026,348} not attached to window manager
Thanks in advance for any and all help.
EDIT: More logcat
--------- beginning of crash
12-11 16:22:40.154 1976-1976/com.blazapps.allenfamilymedicine E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.blazapps.allenfamilymedicine, PID: 1976
java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{f89e667 V.E...... R......D 0,0-760,232} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:424)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:350)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
at android.app.Dialog.dismissDialog(Dialog.java:362)
at android.app.Dialog.dismiss(Dialog.java:345)
at com.blazapps.allenfamilymedicine.SendReportActivity$SendMailTask.onPostExecute(SendReportActivity.java:168)
at com.blazapps.allenfamilymedicine.SendReportActivity$SendMailTask.onPostExecute(SendReportActivity.java:138)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I still do no know why the exception is being thrown but because I save lots of information in EmailStatusResponse I can just surround the exception with a try catch and if the task was complete I get the result I expected.
try {
weakReference.get().dismiss();
} catch (Exception e) {}
If anyone can figure a better solution I would really appreciate it. I hate just catching exceptions. There is usually a better way.