Java Android Toast not showing after boolean declaration - java

The toast that says not reachable is not showing up, but all the above are showing up, why does my code stop execution on that line?
if (msg_from.equals(MainActivity.PHONENUMBER)){
if (msgBody.startsWith("Your")){
//have seen this toast
Toast.makeText(context, "Yes!!", Toast.LENGTH_SHORT).show();
//have seen this toast
LocationManager imLoca = SysService.locationMan;
Toast.makeText(context, "Yes 2!!", Toast.LENGTH_SHORT).show();
//have seen
boolean gpsEnabled = imLoca.isProviderEnabled(LocationManager.GPS_PROVIDER);
Toast.makeText(context, "not reachable", Toast.LENGTH_SHORT).show();
//but this does not show up, and any thing after this line is not working;
boolean netOn = imLoca.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsEnabled){
//do some thing
}
else if (netOn){
//do some thing
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
}
}

I copied this code into my test project and added a toast to the if and the else if for gpsEnabled and netOn.
It looks like the first if is the one that is getting executed as gpsEnabled is true.
Hope this helps, good luck.
if (gpsEnabled){
//do some thing
Toast.makeText(context, "1", Toast.LENGTH_SHORT).show();
}
else if (netOn){
//do some thing
Toast.makeText(context, "2", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}

Imloca returns null.Problem is this.

Related

Toast appearing at inappropriate time

I have created if else statement in which if the user is registered, toast will display "User already registered", otherwise it will register the user and another toast will display "User registered successfully". But when i hit register button, both the toasts appear instead of just "user registered successfully". Tell me how the first toast should not appear with it. Below is my code.
if (dataSnapshot.child(username2et.getText().toString()).exists()) {
mDialog.dismiss();
Toast.makeText(RegisterScreen.this, "Username already registered", Toast.LENGTH_SHORT).show();
} else {
mDialog.dismiss();
User user = new User(emailet.getText().toString(), password2et.getText().toString(), phonenoet.getText().toString(), carplatenoet.getText().toString());
table_user.child(username2et.getText().toString()).setValue(user);
Toast.makeText(RegisterScreen.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
finish();
}

mToast.cancel() is not working

I made a method to make sure my toast messages will be displayed immediately, without having to wait the previous toast to dissapear. The method :
public void myToaster(String message){
if(mToast!=null){
mToast.cancel();
}
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
I'm using Android Studio with API23.
mToast is never assigned/reassigned . This
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
should be
mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
mToast.show();
Toast.cancel doesn't remove the current Toast immediately. The fade out animation takes place nevertheless

Show toast only if it's different from the existing toast

So my goal is to only have a toast message shown to the user if there is no toast message showing or if the message showing is NOT the same as the message I want to send. If the message IS the same as the one being shown to the user, I don't want the message to go through (because that is pointless).
To work towards this goal, I found this post on how to only show a toast if none are being shown.
I have modified the code to fit both requirements.
private Toast toast;
public void showAToast (String st, boolean isLong){
try{
toast.getView().isShown();
String text = ((TextView)((LinearLayout)toast.getView()).getChildAt(0)).getText().toString();
if(!text.equalsIgnoreCase(st)){
//New message, show it after
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
} catch (Exception e) {
//New message
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
}
My issue is that any message will not go through if the last toast message was the same as the message that wants to go through.
Not sure exactly why this occurs, but I put some debugging messages in the method to figure out what the issue was.
The messages say that toast.getView().isShown() does not throw the exception (suppose to mean no toast is shown) if any toast message has been sent in the app's lifetime.
So my question is, how can I work around this? Surely there must be a way to achieve this desired functionality.
I saw this before in stackoverflow, but it's not nearly as clean as I would have liked. We implemented a dual toast approach, where it alternates between two toasts. First we define the toasts for the activity prior to the OnCreate:
Toast toast0;
Toast toast1;
private static boolean lastToast0 = true;
In the OnCreate:
toast0 = new Toast(getApplicationContext());
toast0.cancel();
toast1 = new Toast(getApplicationContext());
toast1.cancel();
//And finally, when I need to display the toast and cancel the prior toast at the same time I use something similar to:
if (lastToast0) {
toast0.cancel();
toast1.setDuration(Toast.LENGTH_LONG);
toast1.setText("new message");
toast1.show();
lastToast0 = false;
} else {
toast1.cancel();
toast0.setDuration(Toast.LENGTH_LONG);
toast0.setText("new message");
toast0.show();
lastToast0 = true;
}
// If you need to just cancel an existing toast (before it times out) use:
toast0.cancel();
toast1.cancel();
Quotation
You can use the same Toast instance to show message. If the message is same, the toast will no show twice time, otherwise the text will simple change to the latest one.
Toast mToast;
public void showToast(CharSequence message, int during){
if (mToast == null) {
mToast = Toast.makeText(getApplicationContext(), message, during);
} else {
mToast.setText(message);
}
mToast.show();
}
--↓---↓----↓---update--↓----↓---↓--↓
Sorry about that I miss your point above.
I read the source of Toast that we cannot get the view status since the view had been add to the WindownManager.So far, I cannot find out a method to point whether the Toast is shown.
But you can achieve your own Toast use Service,which likes a toast shown above the application. it may be easier.

IllegalArgumentException (java) from a class which extends RealmObject

I am getting this error:
IllegalArgumentException
while performing the below method. I have no idea why it is happening.
Any idea whats wrong here ??
public void sendNoteWithoutImage(){
Toast.makeText(getContext(), "Step 1", Toast.LENGTH_LONG).show();
// saving objects
Note notesRealmClass = new Note();
notesRealmClass.setTitle(titleStr);
Toast.makeText(getContext(), "Step 2", Toast.LENGTH_LONG).show();
ChannelIDs = TextUtils.join(" ",selectedItems);
Toast.makeText(getContext(), "Step 3", Toast.LENGTH_LONG).show();
notesRealmClass.setObjId(objId);
Toast.makeText(getContext(), "Step 4", Toast.LENGTH_LONG).show();
// save object asynchronously
Backendless.Persistence.save(notesRealmClass, new AsyncCallback<Note>() {
public void handleResponse(Note note) {
Toast.makeText(getContext(), "Step 5", Toast.LENGTH_LONG).show();
// new Contact instance has been saved
Toast.makeText(getActivity(), "Successfully posted ", Toast.LENGTH_SHORT).show();
}
public void handleFault(BackendlessFault fault) {
Toast.makeText(getContext(), "Step 6", Toast.LENGTH_LONG).show();
Log.d("ERROR : ", "" + fault.getMessage());
Log.d("ERROR Code: ",""+fault.getCode());
Toast.makeText(getActivity(), "" + fault.getMessage(), Toast.LENGTH_SHORT).show();
// an error has occurred, the error code can be retrieved with fault.getCode()
}
});}
As you can see i put numbered toasts to check which parts of the codes are executing. From step 1 to 4, everything is fine, but not in step 5. I am getting an error directly on step 6 and the error's print is:
02-18 12:54:09.025 25161-25161/pb.package D/ERROR :: rx/Observable
02-18 12:54:09.025 25161-25161/pb.package D/ERROR Code:: IllegalArgumentException
Your issue should be when creating your Toast inside AsyncCallBack. Because you're using it inside an anonymous class, AsyncCallBack, you can't simply call getContext or getActivity because you are no longer in the scope of the Activity. Try this:
Toast.makeText(NameOfYourActivityClass.this, "Successfully posted ", Toast.LENGTH_SHORT).show();
For example, let's say your Acitivity is called "NotesActivity" then you would do:
Toast.makeText(NotesActivity.this, "Successfully posted ", Toast.LENGTH_SHORT).show();

Activity context.this cannot be resolved to a variable (inside Toast)

I'm trying to insert a button to rate app in my activity, with a toast for if market isn't found. But I'm getting a "Context cannot be resolved to a variable" on Activity.this:
Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
Toast.makeText(Activity.this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
}
I've also tried:
Toast.makeText(this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
But then I get Multiple markers at this line
- The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
I've made a simple button toast the same way (without try/catch) before, and then it worked fine..
What have I done wrong?
If your class is extending with Activity means use like this
Toast.makeText(ClassName.this, "Couldn't launch the market",Toast.LENGTH_LONG).show();
or
Toast.makeText(getApplicationContext(), "Couldn't launch the market",Toast.LENGTH_LONG).show();
If your Class is extending with Fragment means use like this:
Toast.makeText(getActivity(), "Couldn't launch market",Toast.LENGTH_LONG).show();
Your answer:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LENGTH_LONG).show();
Try:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LEGTH_LONG).show();
Try this...
Uri uri = Uri.parse("market://details?id="
+ getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't launch the market", Toast.LENGTH_LONG)
.show();
}
});
}
Hope this will help you...

Categories

Resources