Display Error messages on Screen(view) - android

I want to show Error(log) messages from catch block. How can I show all messages(stack) on a single Screen, so that user can get an idea ?
Thanks...

How about using Toast?
Sample:
Toast.makeText(getBaseContext(), getResources().getString(resourceIDForMessage), Toast.LENGTH_SHORT).show();

Related

Unable to get toast message in UI Automator

in our app we are using Toast.makeText(context, context.getString(R.string.contact_not_present), Toast.LENGTH_SHORT).show(); to show toast message
For Ui Automation we are using
https://developer.android.com/training/testing/ui-automator
While automating a use case which should fire a Toast message ,As expected the toast message was fired but i am unable to find it
I am trying to locate the element by
UiObject2 object = device.wait(Until.findObject(By.clazz("android.widget.Toast")), 9000);
But i am always getting the object as null
can anyone help me on this ?

Can't send a SMS using SmsManager in android

I want to send a sms from my android phone but it doesn't work.
here is my code:
public class MainActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
String msg,number;
number= "***********";
msg="This is a message";
try{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, msg,null,null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
}catch(Exception ex){
Toast.makeText(getApplicationContext(),"SMS failed, please try again.",Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
I include 'uses-permission android:name="android.permission.SEND_SMS"'
I run this code from my android phone but it doesn't send any message.
If there is any error how can i see those error and how to fix it.
Please Help me.Thanks in advance.
use this website for reference
<uses-permission android:name="android.permission.SEND_SMS" />
According to your code, the problem is that you haven't called the sendMessage method.
You need to have an action that occurs when you click a button. Google how that is done on Android. Once you have that worked out, you should be able to make the button click to call the sendMessage method.
Also, you'll need to know how to view the error log. There are a number of apps that allow you to do this. It is important to know how to view the error log so that way if your Toast finally pops up, you'll be able to check the log to see why this happened.
I guess you have the sendMessage() some where in the code if you trigger a button click? sendTextMessage work well. Anyway, make sure that you've validated if number is null or bad formatted. This may causes the problem. Make also sure that you are using the proper SMSMAnager (android.telephony.SmsManager) instead of android.telephony.gsm.SmsManager. SEND_SMS permission is also required.
I solve the problem.Actually the problem is for dual sim. And always i deactivate my first sim(Sim1). But this is the problem.My app always find my first sim message center number but i deactivate Sim1.Now i activate my first sim and this is worked.Thanks to all for your answer and help

Toast Alert Dialog is not displaying in ListView

I wrote an Application in which we can add items to the list view..i'm doing validation for that if any one making the same entry it should display an alert
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG);
But it not displaying while I'm running on emulator. I'm sure that my validation code is correct.
Try:
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG).show();
add the show() to the toast like this
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG).show();
You need to specify show() to make the toast visible
Everything is perfect except one that you forget to append .Show() at the end, and also check this .
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG).Show();
Instead#
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG);

How to disable the exception being display on screen

this exception or java number null pointer exception always happen to my application. i surrounded try...catch... but yet still appear this.
try{
} catch (Exception ex) {
toast = Toast.makeText(Main_ParticularCategoryAllNews.this,
Config_ConstantVariable.warnmsg_serverwifidown,
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL
| Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
So how to disable this?
The exception is already being handled by the Toast, so it doesn't reach your catch statement.
I'm guessing that's some error handling by the device you're using already, so most probably you don't have a choice but to find why and where that Toast appears, and fix that.
Check the following and try to implement according to the guide line of stack over flow users
Android Database Locked
Failed to open Database in android app

Android Toast Message is not showing

I have Button.When the user click the button, there are some condition, that condition is not satisfy then need to display Toast but not showing Toast Message...
Code: Edited
Button addMe = (Button)findViewById(R.id.addMe);
addMe.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(selectedReason.equals("--Select--")){
Log.i("TAG","-----");
Toast.makeText(getBaseContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();
}else if(selectedType.equals("--Select--")){
Toast.makeText(getParent(), "Discount type can not be blank", Toast.LENGTH_SHORT).show();
}else{
if(selectedType.equals("Value")){
if(spc_amount.getText().toString().equals("")){
Log.i("TAG","-----");
Toast.makeText(getBaseContext(), "Discount type can not be blank", Toast.LENGTH_SHORT).show();
}else{
if(Double.parseDouble(spc_amount.getText().toString()) > invoiceValue){
Toast.makeText(getBaseContext(), "Amonut can not be grater than invoice", Toast.LENGTH_SHORT).show();
}else{
Discount dis = new Discount();
dis.setCriteriaName(selectedReason);
dis.setDiscountValue(Double.parseDouble(spc_amount.getText().toString()));
spDisList.put(1,dis);
tl.removeAllViews();
loadTableLayout();
}
}
}
}
}
});
I have tried context with getParent() , getApplicationContext() , SpecialDiscountActivity.this & getBaseContext() but not working....
This Toast message coming under the Tab Activity Group
Try:
Toast.makeText(getBaseContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();
It's the .show() that you've omitted everywhere that causes all your toasts to instatiate, but never execute.
Please excuse me if this isn't the solution to your problem, but I accidentally unchecked the 'Show notification' setting of the application once. It may be an idea go into your device settings/application/manager and check this setting.
I think you are missing .show();
It should be...
Toast.makeText(getBaseContext(), "Amount can not be grater than invoice",
Toast.LENGTH_SHORT).show();
Just restart your device! It solved my problem.
Maybe you are not in the UI thread? Try this: http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29
If Toast is not showing that means either you have not called show() method or you are not on UI thread
Ideally, you should create a helper method to show Toast like this on UI thread
/** Execute Toast on UI thread **/
private fun showToast(message: String) {
Handler(Looper.getMainLooper()).post {
// Code here will run in UI thread
Toast.makeText(
this,
message,
Toast.LENGTH_LONG
).show()
}
}
I did like this
Toast.makeText(SalesActivityGroup.group.getParent(), "Amount can not be
grater than invoice", Toast.LENGTH_SHORT).show();
Just bumped across this issue and was wondering how a simple Toast is not appearing. After few trials it struck me to check the notification setting of the app and Voila.
I switched the notification setting ON and it started showing up. I searched and came across the below link, which talks about the same:
https://code.google.com/p/android/issues/detail?id=35013
There could be two possibilities:
1 - You have not append .show(); at the very end of Toast.makeText(getActivity(), "Hi", Toast.LENGTH_SHORT).
2 - There could be situation you are not passing right context
For that try passing getActivity().getApplicationContext();
which in my case resolved the issue.
Good luck :)
Some times Emulator is hanging so restarting the emulator fixes the issue.
Simply restart your emulator not fixed my problem.
Close emulator
Tools -> Avd Manager
In the device list click on "drop-down icon" under "Action" column.
Cold boot now
Now reinstalling the app it will work.
You can use the context of the Activity
like,
Toast.makeText(ActivityName.this,"Reason can not be blank", Toast.LENGTH_SHORT).show()
if this is not working, please put a log.i(); in your each condition may be its going to the last else and you are not getting the Toast.
In my case it was because I wasn't running from Main thread, this fixed it:
val mainActivity = (tabHostActivity.activityContext() as MainActivity)
mainActivity.lifecycleScope.launch{ //Dispatchers.Main, follows lifecycle
Toast.makeText(mainActivity, "my awesome message", Toast.LENGTH_LONG).show()
}
Just Cold boot your device!
It can solve the problem.
Sometimes there may be an error or nullPointerException in the message that we want to print with Toast message. If this error occured then app will simply ignore the Toast message. I had the same issue. I printed the message in Log and found the error, after that I changed the message in Toast. And it worked as I wanted. Try it!!
Android throttles toats so if you send to many they just stop showing up, instead you can use Snackbar:
Snackbar.make(myView, "This is a snack.", Snackbar.LENGTH_SHORT).show();
Try:
Toast.makeText(v.getContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();
Complimer checks all the code and if there is a critical error, it ignores even the lines before the error section and therfore you will not see the "Toast".
Simply, comment the lines which error happens in them (you can find the error lines in Logcat)
Now you can see the "Toast" and can analyze your problem.

Categories

Resources