Android keeps on reporting crashes from users which I can't reproduce on my phone.
I can find the lines which seem to be incorrect:
cursor.moveToFirst();
elechs=cursor.getString(2);
elecls=cursor.getString(3);
gass=cursor.getString(4);
waters=cursor.getString(5);
cursor.close();
if (elechs.length()!=0){
elechdb=Double.valueOf(elechs);
}
else {
elechdb=0.0;
}
if (elecls.length()!=0){
elecldb=Double.valueOf(elecls);}
else {
elecldb=0.0;
}
if (gass.length()!=0){
gasdb=Double.valueOf(gass);
}
else {
gasdb=0.0;
}
if (waters.length()!=0){
waterdb=Double.valueOf(waters);
}
else {
waterdb=0.0;
}
elecldb=Double.valueOf(elecls);
gasdb=Double.valueOf(gass);
waterdb=Double.valueOf(waters);
If I look at the code, it doesn't make any sense.
I think I forgot to delete the last three lines. First I check the string. If the string is empty it will store the value as zero.
The incorrect last three lines will also try to make a double if the cell is empty. This cause a lot of crashes. However not on my machine.
I believe that it shouldn't be possible to make a of an empty cell.
Does anyone know why this error doesn't crash my phone?
Your best solution is probably just to remove the problematic lines that shouldn't be there anyway, preferably add actual error handling around calls to Double.valueOf() in case the input is completely malformed (there may be inconsistent behaviour if the cell is empty, but if it says "hello world", everything will crash), and release an update.
Since the users are getting NumberFormatException, you should catch the exception and perform appropriate action when it happens.
if (elechs.length()!=0) {
try {
elechdb=Double.valueOf(elechs);
} catch (NumberFormatException e) {
// Perform error handling
}
}
If the users are getting NullPointerException, you should check if the strings are null before checking for their lengths.
If the data is put in by the user, you should do both of the above to avoid future problems.
Related
I am getting the above error : System.AggregateException: One or more errors occurred.
With this line of code here:
List<tblDeviceIds> installIDs = KumulosHelper.Functions.Device.GetDeviceIDsOfUser(toUser);
The Method "GetDeviceIdsOfUser" looks like this:
public static List<tblDeviceIds> GetDeviceIDsOfUser(string username)
{
IDictionary<string, string> myDict = new Dictionary<string, string>();
myDict.Add("username", username);
return KumulosGeneral.getTblDeviceIds("getDeviceIDsOfUser", myDict);
}
So, there is really nothing fancy going on.
Sometimes, but only on CERTAIN users above error. So even when the user would be "null", which by the way he never is, the list would just return nothing. BUT instead it crashes. This itself is something I didnt quite understand, so what I did was:
List<tblDeviceIds> installIDs = null;
try
{
installIDs = KumulosHelper.Functions.Device.GetDeviceIDsOfUser(toUser);
}
catch
{
installIDs = null;
}
This would be a bullet prove workaround, but yet: It goes into try, it crashes, it never goes into catch, it is dead.
Would someone care to explain?
Thanks!
O, maybe this has something todo with doing this on another thread? This is the function that calls all that:
await Task.Run(() =>
{
Intermediate.SendMessageToUser(toUsername, temp);
});
As you can see, it is inside an async task... but that should not be a problem, right?
The reason you receive an AggregateException is because the exception is originating from within a Task (that is likely running on a separate thread). To determine the cause, walk the line of InnerException(s).
Regarding the catch not catching, my suggestions would be: Ensure the latest code is being used. Add Tracing instead of relying on breakpoints. And see if the inner exception is thrown from yet another thread (is GetDeviceIDsOfUser also using async?)
See also: Why is .NET exception not caught by try/catch block?
Many of us have come across the dreaded
E/WindowManager﹕ android.view.WindowLeaked
This can happen for several reasons as is mentioned here and in many other SO questions.
I have tried making sure my dialogs are in a "good place" before showing them, often wrapping them around certain conditions like:
if (!isFinishing() && !isDestroyed()) {
mDialog.show();
}
I'm curious if anyone knows of a "perfect environment" for which an AlertDialog is guaranteed to always display without error. From personal experience, even with the above conditions, the WindowLeaked error still always comes up.
This really is a problem I faced when changing the orientation of the Activity, whenever we try to display a dialog with a closed Activity this error will skyrocket. In a less important case, as in a Activity data recording information, I simply ignore the Dialog and closing with the following code:
#Override
public void onPause() {
super.onPause();
try {
if (myAlertDialog != null && myAlertDialog.isShowing()) {
myAlertDialog.dismiss();
myAlertDialog = null ;
}
} catch (Exception e) {
e.printStackTrace();
}
}
But if you need to keep it on the screen, the only way I found was locking the orientation of Activity.
I'm developing a videogame in Android which closes unexpectedly and randomly. The case is I have the following code envolving my game loop:
try {
//game loop
}
catch(Exception e)
{
//...
}
catch(OutOfMemoryError e)
{
//...
}
For some reason, the app is not entering any of those catchs. I thought it was a memory problem, but with the 'OutOfMemoryError' it should be covered right? Or this just covers when we demand a lot of memory all the sudden?
I was wondering if there is any catch condition to capture ALL the possible situations because this is driving me crazy...
Without looking at any code. I would posit that perhaps you have an endless loop in your gameloop and it is causing the app to be unresponsive. Which is causing the OS to handle any exceptions or issues raised.
I want to install my app which contains regestration form whose username and password is stored im sqlite database. after copyiny .apk file my apps does not contains any database table for that work.
So my question is how to import this database in my app which is installed on android device it is working fine on emulator.
Have you initialized database class object
EditDatabase db=new EditDatabase(this);
onCreate or onResume activity?
if not then initialize it.
Update from logcat output
Based on the logcat output, it shows that this is an unhandled exception. i.e. it happens outside the try{} ... catch{} block of the onClick(View) method.
This means that it is a problem in one of these 2 lines:
unname = username1.getText().toString();
storePassword1 = db.getdata(unname);
My feeling is that username1 is null because it is not referenced in your layout XML file.
Here's how you should check - replace those 2 lines with these null checks:
if (unname != null)
{
unname = username1.getText().toString();
}
else
{
Log.d("MyTag", "unname is null");
}
if (storePassword1 != null)
{
storePassword1 = db.getdata(unname);
}
else
{
Log.d("MyTag", "storePassword1 is null");
}
if (unname == null || storePassword1 == null)
{
return;
}
Run the code again, check the logcat output again, and see if it tells you about the problem.
Also try not to use System.out(String) - rather use the Log.d(String, String) methods. These are more useful on Android.
Original
Firstly, please provide a stack trace - it will show where the null pointer error happened.
For Android, the most useful way of doing that will be to use the adb logcat terminal command. This outputs the internal log of your Android device/emulator to the screen, so you can view what went wrong.
In your catch{} block, I would put the following line:
Log.d("MyTag", "Stack Trace of exception...", e);
This will output the text, and information about the error e to this log - I think it will include the stack trace.
Copy the lines starting with "MyTag" and paste them into your question.
Secondly, without the stack trace, confirm that these 3 variables are not null typically:
username1
db
password (if the app actually crashed with the NullPointerException then this one is not the problem, because the exception would be handled by the catch{} block)
i.e. confirm that you have initialised all of them before the click event.
In my app I want to catch all types of exceptions and send reports by e-mail. For that I'm using global try catch block. But now I need to recognize exception by type. How can I do it?
try{
...
}
catch (Exception e){
//Here I need to recognize exception by type
send(Error);
}
Why you don't simple send the whole stacktrace?
send(e.getStackTrace())
It not only contains the Exception type but also where (file, class, line) it occurred.
Additionally, you can also simply use the toString() method.
See the java doc for further information
Instead of rolling your own error logging and reporting mechninism I strongly recommend you use ACRA Its free, open source, and supports sending error logs to email. I have used it for quite some time and it is very good.
This will give you all sorts of information such as phone make, model, resolution, free memory, as well as a full stack trace of the error. Its by far the easiest way to get quality error reporting into an Android app.
The best part is it takes all of about 5 minutes to get setup and integrated.
e.getClass() // will give you Class object
e.getClass().getName() // will give you class name
However if you know the class names already you can use
if(e instanceof A)
{
// some processing
}
else if(e instanceof B)
{
//some processing
}
else
{
//
}