I have a problem with a piece of code in my application. I am using Android Studio and as a device to view the application I use PIXEL API 30. Now the problem is that the devide PIXEL API 30 does not show error messages to the user. for example I have the following fragment that does a check on a user's name:
public class Fragment_user{
public boolean checkname(String name){
if(name.length() == 0) {
Toast.makeText(getContext(), "user add", Toast.LENGTH_SHORT).show();
return false;
}else return true;
}
}
now if I don't enter name I want to show the user a toast error message but toast doesn't work with the PIXEL API 30 device. why? Did I get the code wrong or does the PIXEL API 30 device not support toast error messages?
note: I don't want to use setError
Some times system launcher get's crash idk why!, So try to close your emulator and restart it with using Cold boot now function. I also faced that error so i fixed it with using like that.
Related
I have an android PrintService launched by an Application.
I would like to be able to display toasts from the PrintService when I'm selecting the printer on which to print etc. (UI generated by the android system, picture here).
On Android <11 it works fine, however for 11+, I'm not able to display a toast...
I've tried several code (Kotlin) :
Toast.makeText(<context>, message, Toast.LENGTH_LONG).show()
Handler(Looper.getMainLooper()).post {
Toast.makeText(context.applicationContext, message, Toast.LENGTH_LONG).show()
}
Where I tried the following values of :
this (the PrintService)
this.applicationContext
this.baseContext
Still, I'm never able to display a toast on the printer selection page...
Is it possible ? What is wrong in my code ?
Occasionally, the instrumentation tests (Espresso) are failing on Google's Firebase Test Lab due to a keyboard on-boarding popup (screenshot) that blocks the screen and prevents tap/type events.
This only happens on the Samsung Galaxy S9+
Here is the exception:
android.support.test.espresso.PerformException: Error performing 'type text(666666)' on view '(is descendant of a: (with id: XXX) and an instance of android.widget.EditText)'.
Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at android.support.test.espresso.base.InputManagerEventInjectionStrategy.injectKeyEvent(InputManagerEventInjectionStrategy.java:113)
Any suggestions?
Maybe you can try to play around with "ime" command from ADB to enable another keyboard (you can log for example the return of ime list in order to get the IDs) before running your tests (e.g. #Before):
getInstrumentation().getUiAutomation().executeShellCommand("ime enable ID");
Thread.sleep(1000);
getInstrumentation().getUiAutomation().executeShellCommand("ime set ID");
Else you can workaround with uiautomator and implement a check before using the keyboard with this kind of piece of code :
...
onView(withId(R.id.inputField)).perform(click());
if (Build.VERSION.SDK_INT >= 23) {
UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject skipButton = device.findObject(new UiSelector().text("SKIP"));
if (skipButton.exists()) {
try {
skipButton.click();
Timber.e(e, "Dismissed popup on Keyboard");
} catch (UiObjectNotFoundException e) {
Timber.e(e, "There is no popup displayed on Keyboard");
}
}
}
onView(withId(R.id.inputField)).perform(typeText("some"), pressImeActionButton());
...
Hope this help !
I also faced this problem. It's on Firebase Test Lab side, and you shouldn't try to find a workaround. Sometimes there are problems with devices you are not responsible for. Instead you should report about it to Firebase team directly if you want it to be fixed as soon as possible.
The fastest way to do it is to go to #test-lab channel of the Firebase Slack community and report them about an issue like that. They will ask you to provide your matrix ID where you experienced something wrong.
As for layout popup, it was fixed the next day it was reported, so you shouldn't see it now.
I've been trying to get errors to display in a text field within the app for ease of error reporting from users.
I've had some success using this code I found on stackoverflow. It's used at the top level of the app but it's not working on device:
//start code
this.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, errorHandler);
function globalErrorHandler(event:UncaughtErrorEvent):void
{
var message:String;
//check for runtime error
if (event.error is Error)
message = (event.error as Error).getStackTrace();
//handle other errors
else if (event.error is ErrorEvent)
message = (event.error as ErrorEvent).text;
else
message = event.error.toString();
//do something with message (eg display it in textfield)
myTextfield.text = message;
}
//end code
At first this wouldn't work on the device and I thought it was because upon the error, when developing on the pc, flashplayer would display the actionscript popup with the error. Which you would need to click "dismiss all" or close and then the globalErrorHandler was called after and then the error written to the textfield. I thought this is what was keeping it from showing up on the device. However, by adding event.preventDefault() I was able to suppress the actionscript popup when developing on the desktop and the error was written to the textfield successfully. This was not the case however on the andriod device. It still just hangs on the error. It's as if the default error event cannot be suppressed on android.
Thanks for your time. Any help appreciated!
EDIT 22/09/2017: I was able to see the error on device finally. It had to do with while on desktop publishing the error would be shown. However, on device the behavior was different and something was covering the textfield, by bring it to the front on error I was able to see it. However, I still see that some errors deeper in the class hierarchy are not being caught.
In our project we have it simpler (and it is working):
stage.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onUncaughtError);
private function onUncaughtError(e:UncaughtErrorEvent):void
{
// Console is basically a TextField for debug/diagnosis output.
if (e.error) Console.error(e.error.getStackTrace());
e.preventDefault();
}
The other thing you should probably check is whether your TextField actually displays any text at all for there might be text embedding issues, unrelated to the error handling routine.
UPD: Loading SWFs so it doesn't mix with the parent.
var request:URLRequest = new URLRequest(path);
var context:LoaderContext = new LoaderContext;
context.applicationDomain = ApplicationDomain.currentDomain;
var loader:Loader = new Loader;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
loader.load(request, context);
I use Toast to indicate my app's state. And I use the following code in order to control toast showing time.
Toast noCardDetectedToast=null;
void setVisibilityNoCardDetectedToast(boolean visible)
{
if(visible)
{
if(noCardDetectedToast==null)
noCardDetectedToast = Toast.makeText(this, R.string.msg_no_card_detected, Toast.LENGTH_SHORT);
noCardDetectedToast.show();
}
else
{
if(noCardDetectedToast!=null)
{
noCardDetectedToast.cancel();
noCardDetectedToast=null;
}
}
}
When it's necessary to show this Toast, the application starts invoking setVisibilityNoCardDetectedToast(true) several times per second.
And when the application doesn't need this toast any more, it invokes setVisibilityNoCardDetectedToast(false).
Everything works fine, but my android device does not fall asleep, as long as the toast is visible.(I tested my application on Android 4.x and 5.0)
This behaviour looks strange for me. What do I do wrong here?
I have a toast that is displayed in the following way:
Toast.makeText(context, "The message", Toast.LENGTH_LONG).show();
I am absolutely certain I am displaying the toast from the UI thread, and I can add that it worked fine for many devices including older updates of the Galaxy S3, but after the latest update none of my toasts are being displayed.
Has anyone else experienced this and has a solution?
In newer Android phones there is a "Show notifications" checkbox in App Settings and for some reason if notifications are disabled it also disables Toasts. The issue has been reported here:
http://code.google.com/p/android/issues/detail?id=35013
But looking on the source code:
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/services/java/com/android/server/NotificationManagerService.java
it seems like it might be intentional:
Line 114:
private static final boolean ENABLE_BLOCKED_TOASTS = true;
Lines 693-707:
final boolean isSystemToast = ("android".equals(pkg));
if (ENABLE_BLOCKED_TOASTS && !isSystemToast && !areNotificationsEnabledForPackageInt(pkg)) {
Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
return;
}
Since the OP hasn't been around in the past 24 hours, I'll post the solution that was found on a thread on the android-developers Google Group.
The problem was that the option to Show Notifications for this app was unchecked in the Settings (this is possible in newer versions of Android). This not only prevents the showing of notifications in the notification bar, but also prevents the display of toasts.
A bug for this has been opened here.