How to disable the exception being display on screen - android

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

Related

Exception Handling in Android Studio

I have written some simple on button click code. When I run my application and click on the button, it gives the following Error:
Unfortunately, MyApplication has stopped.
Code under the button is following:
public void onClick(View v) {
if (v == mCapture) {
try {
CaptureFingerPrint();
RegisterFingerprint();
} catch (Throwable e) {
mTextViewResult.setText(e.toString());
}
}
}
I actually don't want to close my application accidentally and display error in label as i done in exception part of the above code which does not restrict my application to close.
Important to mention that i does not face this error in android studio. I face it when APK installed and run directly on mobile,
Please anyone guide how to restrict my application to close on button click and displaying error in label. The same i done in c# without any issue.
Without posting the stacktrace, the only reason I see that can make your app crash is mTextViewResult being null or e.toString() providing a null response. Was mTextViewResult properly identified inside the layout? If yes, try replacing e.toString() with "" + e.getMessage().
It would be best, though, if you found and checked the stack trace.

How to enable stacktrace in android studio? Where is its window?

I develop an app which has problem in HTTP execute method. Someone suggested me to check stacktrace. But I don't know how!
So how can I enable it? where is exactly its window?
I put --full-stacktrace in File > Settings > compiler > command-line options . So I just see Threads and Frames in Debugger window.
(I know about log cat and I use it, I need to use stack trace)
You're looking for Logcat. The keybind to bring it up is Alt+6. You can also bring it up by clicking on it at the bottom of android studio:
Perhaps you are catching the exception but ignoring the result, you can log your stacktrace like this:
public void doSomething() {
try {
//do something that might throw an exception
} catch (Exception e) { //be as specific as possible when catching an exception
Log.e("ExceptionTag", e.getMessage(), e);
}
}

How to see the crash reason in Eclipse debugger

Following this Android tutorial: http://developer.android.com/resources/tutorials/hello-world.html I added two lines which should cause null pointer exception:
Object o = null;
o.toString();
Now I set breakpoint on the second line and start debugger. When debugger breaks, I click "Step over" and application crashes. However, I don't see any useful information in the debugger. Debug window shows ActivityThread.performLaunchActivity, Source window shows "No source found". I don't see any information about exception, null pointer, etc. in any Eclipse debugger window, and don't see anything pointing to my line of code that causes crash. So, what am I missing?
Edit. Maybe Android UI framework has its own exception handling mechanizm, which prevents me to see exception immediately in my code? Something like this happens in another UI frameworks, like WinForms, Qt, wxWidgets.
After exception occurs you should check Logcat window for details: there you find the entire stack leading to the line of code where exception is. In some cases exception will occur in some other class (not yours) - then you should look for your package/class name in the stack to find otu whether you're "responsible" for it.
Corresponding output will not appear in Logcat immediately - let it run for some time (or until it crashes).
Also you can set "Java exception breakpoint" so the execution will break whenever there is an exception.
You can see the error in the LogCat. Window->Show View->other->Logcat
Try something like this...
try {
Object o = null;
o.toString();
}
catch (Exception e) {
e.printStackTrace();
}
...and use the DDMS perspective in eclipse.
Ideally you want to catch specific exceptions so the catch block would be...
catch (NullPointerException npe {
npe.printStackTrace();
}
...but doing a 'catch all' for Exception as in my first example and using the eclipse DDMS perspective to view logcat output will give you a good head start.
Finally, I found useful information in the "Breakpoints" debugger window. It shows NullPointerException and points exactly to offensive line.

Android exception.getMessage() kills App

I have a question. Let's say I have the following code:
try{
//do something that could throw an exception
}
catch(Exception e){
System.out.println(e.getMessage();
}
Executing this in the emulator works fine, but when I tried to run it on my phone, the app craches (NullPointerException, apparently "e" is null).
How can that be?
Nope. If exception occurs than e must have something inside (and thats the purpose).
If you use System.out to print your logs, you should be able to see the error in LogCat under the System.out tag. Try checking that out and come back again.

How to programatically hide Caller ID on Android

on Android phones, under Call -> Additional settings -> Caller ID
it is possible to hide your caller ID. I want to do that programatically from my code, but was not able to find a way to do that.
I searched through
android.provider
android.telephony
for 2.1 release and was not able to find it.
Has anybody successfully solved this issue?
Thanks in advance. Best regards.
Here I will describe two approaches I tried.
1.) It is possible to display Additional Call Settings screen from your application. Although it looks like it is part of the Settings application, that is not true. This Activity is part of the Native Phone Application, and it may be approached with the following intent:
Intent additionalCallSettingsIntent = new Intent("android.intent.action.MAIN");
ComponentName distantActivity = new ComponentName("com.android.phone", "com.android.phone.GsmUmtsAdditionalCallOptions");
additionalCallSettingsIntent.setComponent(distantActivity);
startActivity(additionalCallSettingsIntent);
Then user has to manually press on the CallerID preference and gets radio button with 3 options.
This was not actually what I wanted to achieve when I asked this question. I wanted to avoid step where user has to select any further options.
2.) When approach described under 1.) is executed in the Native Phone Application, function setOutgoingCallerIdDisplay() from com.android.internal.telephony.Phone has been used.
This was the basis for the next approach: use Java Reflection on this class and try to invoke the function with appropriate parameters:
try
{
Class <?> phoneFactoryClass = Class.forName("com.android.internal.telephony.PhoneFactory");
try
{
Method getDefaultPhoneMethod = phoneFactoryClass.getDeclaredMethod("getDefaultPhone");
Method makeDefaultPhoneMethod = phoneFactoryClass.getMethod("makeDefaultPhone" , Context.class);
try
{
makeDefaultPhoneMethod.invoke(null, this);
Object defaultPhone = getDefaultPhoneMethod.invoke(null);
Class <?> phoneInterface = Class.forName("com.android.internal.telephony.Phone");
Method getPhoneServiceMethod = phoneInterface.getMethod("setOutgoingCallerIdDisplay", int.class, Message.class);
getPhoneServiceMethod.invoke(defaultPhone, 1, null);
}
catch (InvocationTargetException ex)
{
ex.printStackTrace();
}
catch (IllegalAccessException ex)
{
ex.printStackTrace();
}
}
catch (NoSuchMethodException ex)
{
ex.printStackTrace();
}
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
Firstly I tried just to use getDefaultPhone(), but I get RuntimeException
"PhoneFactory.getDefaultPhone must be called from Looper thread"
Obviously, issue lies in the fact that I tried to call this method from the Message Loop that was not the Native Phone App one.
Tried to avoid this by making own default phone, but this was a security violation:
ERROR/AndroidRuntime(2338): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.provider.Telephony.SPN_STRINGS_UPDATED from pid=2338, uid=10048
The only way to overcome (both of) this would be to sign your app with the same key as the core systems app, as described under
Run secure API calls as root, android
I'm not sure if this is a global feature, but Australian phones can hide their number by prefixing the caller's number with #31# or 1831. This may not be the perfect solution, but a prefix like this could possibly work for your requirements during coding.

Categories

Resources