Android Error handling - Sorry - The application has stopped unexpectedly - android

I've got it wrapped in a try catch, but the exception still trips that ugly screen.
URL u = null;
try {
u = new URL(txturl.getText().toString());
}
catch (MalformedURLException e) {
ReportError(e,"Unable to connect to "+u);
}
calls this:
private void ReportError(Exception e, String message){
Display(message+" - "+ e.getMessage().toString());
System.out.println(">>>>>>>>>>>>>>>>>>>>> "+message+" - "+e.getMessage().toString());printStackTrace();
}
Any way around this. It happens on the Android 2.2 emulator with Eclipse and on my Sprint Hero.
Do I have to validate the form?
Thanks.

I just needed to return after these exceptions. duh. sorry.

Related

Connecting my app to local network using smb protocol

I'm trying to connect my Android application to my company local network (Windows) using smb protocol. The problem is I'm kinda newbie on this matter and something is missing me.
The goal is download the file AREQA.txt from the network to the device. However I don't even can verify if the code can trace the file location because the application crashes when I compile it to the device (it loads fine but crashes when I call the DownLoadF001 procedure). Here's the code:
public void DownLoadF001(View v) {
jcifs.Config.registerSmbURLHandler();
String user;
String password;
String filename;
File localFile;
SmbFile path = null;
try {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null,"######", "********");
path = new SmbFile("smb:\\192.168.1.11/.../AREQA.txt", auth);
try {
if(path.exists()){
Toast.makeText(getApplicationContext(), "Sucesso!", Toast.LENGTH_LONG).show();
}
} catch (Exception e1) {
Toast.makeText(getApplicationContext(), e1.toString(), Toast.LENGTH_LONG).show();
}
} catch (MalformedURLException e2) {
Toast.makeText(getApplicationContext(), e2.toString(), Toast.LENGTH_LONG).show();
}
}
I already tried to remove the inside try from the main one (with all its associated code), and the application stops crashing. However, without it, I can't see if the connection is working.
EDIT: I managed to catch the error (Exception e1):
java.lang.NullPointerException: Attempt to invoke virtual method int java.lang.String.length()' on a null object reference. Any ideas to solve it?
Also, as pointed by #greenapps, I'm calling this procedure from a .xml button by onClick method.

Find why Xamarin app Crashes randomly

I am making a app with Xamarin.Forms, the app in the iOS doesn't crash but in Android, Application is crashing randomly, even if I only switch the tabs.
What is the best way to find what is making the app to stop working?
Thanks
What is the best way to find what is making the app to stop working?
well to add exception handling
try {
// ...
} catch(Exception e) {
// ...
}
or like the below example
public static void main(String[] args) throws FileNotFoundException, IOException {
try{
ExceptionHandler(1);
ExceptionHandler(2);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
System.out.println(" error to be checked");
}
testException(0);
}
public static void ExceptionHandler(int i) throws FileNotFoundException, IOException{
if(i =1 ){
FileNotFoundException myException = new FileNotFoundException("error for code 1 "+i);
throw myException;
}else if(i =2){
throw new IOException("error on 2 ");
}
}
You can also look into a crash reporting service like Hockey App (https://hockeyapp.net/ -- the free level is enough for getting crash reports). You'll get crashes reported there, including crashes in code that you can't catch.
Crash reports aren't quite as handy has being able to break in the debugger, but it's often enough to point you in the right direction.
Instructions on integrating Hockey App to a Xamarin.Forms app: https://support.hockeyapp.net/kb/client-integration-cross-platform/how-to-integrate-hockeyapp-with-xamarin

Sony: SampleNotificationExtension: Sample app throwing exceptions

I'm attempting to send notifications to Sony LiveWare devices
I've installed: com.sonyericsson.extras.liveware.extension.notificationsample, but I get errors and exceptions while trying to run it:
Load the preference activity onto the device
Check "Active" on the preferences screen
The following method is called in SampleExtensionService:
private void addData() {
Random rand = new Random();
int index = rand.nextInt(5);
String name = NAMES[index];
String message = MESSAGE[index];
long time = System.currentTimeMillis();
long sourceId = NotificationUtil
.getSourceId(this, EXTENSION_SPECIFIC_ID);
if (sourceId == NotificationUtil.INVALID_ID) {
Log.e(LOG_TAG, "Failed to insert data");
return;
}
String profileImage = ExtensionUtils.getUriString(this,
R.drawable.widget_default_userpic_bg);
ContentValues eventValues = new ContentValues();
eventValues.put(Notification.EventColumns.EVENT_READ_STATUS, false);
eventValues.put(Notification.EventColumns.DISPLAY_NAME, name);
eventValues.put(Notification.EventColumns.MESSAGE, message);
eventValues.put(Notification.EventColumns.PERSONAL, 1);
eventValues.put(Notification.EventColumns.PROFILE_IMAGE_URI, profileImage);
eventValues.put(Notification.EventColumns.PUBLISHED_TIME, time);
eventValues.put(Notification.EventColumns.SOURCE_ID, sourceId);
try {
getContentResolver().insert(Notification.Event.URI, eventValues);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "Failed to insert event", e);
} catch (SecurityException e) {
Log.e(LOG_TAG, "Failed to insert event, is Live Ware Manager installed?", e);
} catch (SQLException e) {
Log.e(LOG_TAG, "Failed to insert event", e);
}
}
The first problem is that "long sourceId = NotificationUtil.getSourceId(this, EXTENSION_SPECIFIC_ID)" returns -1
I'm not sure how to change, but the docs say it is an optional field. So I removed it to see if I could get any further:
When removing the sourceId code I get an exception on the insert call:
"java.lang.SecurityException: You do not have sufficient permission to perform a insert"
My Environment:
HTC One V paired with a LiveView wrist pager
http://www.amazon.co.uk/Sony-Ericsson-0001516170-LIVE-VIEW/dp/B00477X6DA
I've got the followings apps installed
LiveView
SmartConnect(LiveWare Manager)
I'd appreciate any help getting this sample to work or if anyone knows of a more straight forward approach to doing simple text notifications for LiveWare.
EDIT:
Made some progress. Found an emulator here:
android-sdk\add-ons\addon-sony_add-on_sdk_1_0-sony-16\apk_files
I installed that on the phone. The "SampleExtensionService" works fine in the emulator.
My assumption now is that add-on sample is not compatible with the LiveView device.
The LiveView is unfortunately not supported by the SmartExtension APIs. Instead, there is a specific developer kit for the LiveView here.
If you would, in the future, buy a newer wearable device from Sony Mobile, a notification extension based on the Smart Extension APIs would be usable for several devices including SmartWatch, SmartWatch 2, SBH50, SBH52 etc.

How to catch Amazon AWS errors Android

I am constantly getting error reports (from users) such as:
Caused by: Status Code: 400, AWS Service: AmazonSimpleDB, AWS Request ID: c5cb109d-bbff-fcea-bc0d-0cb60ff8f6af, AWS Error Code: RequestExpired, AWS Error Message: Request has expired. Timestamp date is 2012-06-06T13:19:59.415Z. Current date is 2012-06-06T14:20:03Z
Apparently this is because the user has the wrong timezone or something set? Regardless, I would like to catch this particular error and post a message to the user asking them to check their timezone settings however I can't find a way to do it. If I catch AmazonServiceException, the error shows up as null.
How can I catch errors based on Status Code or even Error Code? The current code that I tried looks like this:
try {
dostuff()
} catch (IOException e) {
updateAWS("DownloadErrors");
return "filenotfound";
} catch (AmazonServiceException e) {
return "downloadfail";
}
However AmazonServiceException e is always null so I can't pull any information from it.
other code:
private void doStuff() throws IOException, AmazonServiceException{
//code here
}
Apparently this is what I needed. SDb tracks "RequestExpired" and S3 uses "RequestTimeTooSkewed"
Also, this appears to be occurring because the system time is >15 minutes different than the AWS server. I put a note to the user to check their time and use "Automatic" date/time if possible. Tested it myself and reproduced the error as well as the solution.
try {
result = doOperations();
} catch (AmazonServiceException e) {
if (e.getErrorCode().equals("RequestExpired") || e.getErrorCode().equals("RequestTimeTooSkewed")) {
result = "timestamp";
}
}
return result;
}

Need some explaining

So this is the weirdest thing ever to happen to me during programing. Yes I'm no pro at programing, but I'm learning as I go. I've got an app talking to a server, a socket in the main thread, reading is done in a separate class and thread and writing in a separate class with asynctask.
The problem is LocationManager. I could talk to server and write/read commands just fine, I implemented the LocationManager and its listener.
I then proceeded to implement a method to update my textview with the new coordinates on locatinChanged. So far so good. Thing is when I use the Emulator control in eclipse and send coordinates the app crashed with a stringOutOfBoundsException (I've programed for 3 years now never seen this). I looked at the code stepped through it and so on. Read the stacktrace, logcat, console and everywhere I could think of but it got me nowhere. Until I finally went to the readerthread which looks like this:
public class ReaderThread extends Thread {
public void run() {
new Thread(new Runnable(){
public void run(){
try {
//Establish a bufferedreader to read from the socket/server.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()), 8 * 1024);
}
catch(Exception e) { e.printStackTrace(); }
//As long as connect is true.
while (connected) {
String line;
try {
//Try to read a line from the reader.
line = in.readLine();
System.out.println(in.readLine());
if (in == null) {
//No one has sent a message yet.
System.out.println("No data recieved");
}
else {
int i = 0;
//As long as someone is sending messages.
while((line = in.readLine()) != null ){
//Make a new Message.
Message msg;
msg = new Message();
//Set the object to the input line.
msg.obj = line;
//Set an id so it can be identified in the main class and used in a switch.
msg.what = i;
System.out.println("i is: "+i);
//Send the message to the handler.
Main.this.h.sendMessage(msg);
}
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}).start();
}
The variable i is in an if statement depending on what the server sent but I cut that out as it has nothing to do with this problem.
The problem is the freaking catch. When the catch is IOException, the app crashes. Out of dumb luck I changed this to Exception and printed e.message to catch the error and see what caused it. Thing is this change fixed it. How can switching IOException to just plain Exception fix a problem like this?
Its like with IOException the program says: "hey your not gonna catch the error but there is no error" but with Exception it says "Well now you could catch it so I'll proceed".
My app is working but I just can't grasp this, why and how does this happen?
You're essentially telling the application to catch the base Exception class. This means that any type of error will be caught, since all exception classes descend from that base type. Since StringOutOfBoundsException does not descend from IOException it was not being caught before and the error was not being caught. Instead of catching all exceptions, you might try the following:
try {
// Your code here...
} catch (IOException e) {
Log.e(TAG, "Caught an IOException!", e);
} catch (StringOutOfBoundsException e) {
Log.e(TAG, "Caught a string out of bounds Exception!", e);
}
I'm unable to determine what is actually throwing the StringOutOfBoundsException to begin with. It may be in the if statement that you cut out of your example.
while (connected) {
String line;
try {
//Try to read a line from the reader.
line = in.readLine();
System.out.println(in.readLine());
if (in == null) {
//No one has sent a message yet.
System.out.println("No data recieved");
}
The test for in == null is in a funny location. You should receive a NullPointerException if that test were to ever return true by nature of calling methods on it a few lines earlier. Obviously something is a little funny with this code.
You fail to save the return value from in.readLine() the second time you call it. I hope it did not contain anything useful. (Though, since you print the line, you obviously wanted to know what data it contained.)
Whatever that line was (from the first call to in.readLine()), it gets thrown away; there's nothing else in the loop that uses it before it is over-written on this line:
while((line = in.readLine()) != null ){
At this point, the two lines that you read are gone forever.
I'm not entirely sure what should be done to fix this; if it were me, I'd be sorely tempted to start over with a sheet of paper and sketch out what the method should be doing without looking at the existing code, then compare the sketch against the code to see which cases each one has overlooked.

Categories

Resources