I've uploaded my Android app to the Market today, and I received 4 errors regarding NumberFormatException,Double.parseDouble and Double.valueOf.
Here's the Logcat I see in the Developer Console:
java.lang.RuntimeException: Unable to start activity ComponentInfo{can.you.drive/can.you.drive.do_drive}: java.lang.NumberFormatException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException
at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
at java.lang.Double.parseDouble(Double.java:318)
at java.lang.Double.valueOf(Double.java:356)
at can.you.drive.dont_drive.roundTwoDecimals(dont_drive.java:132)
at can.you.drive.do_drive.onCreate(do_drive.java:125)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
Here's the function in which the error occurs(according to the Log) -
public static double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.###");
return Double.valueOf(twoDForm.format(d));
}
I know that d is double.
Also, this works fine on my phone(Nexus S running 2.3.4). I'm getting the error from other users. That's why I can't really debug it.
I really don't know what my cause this on some phones.
I'd say it's got to do with localization, with . not being the decimal separator in the platforms where the error occurs. Why don't you use a less expensive method of rounding?
public double round(double d, int nDecimals)
{
for(int i=0; i<nDecimals; ++i) d *= 10d;
d=Math.round(d);
for(int i=0; i<nDecimals; ++i) d /= 10d;
return d;
}
use try catch block to handle it.
Perhaps the error occurs when someone from the Deutsch language uses your application. I've had this problem. They use a comma in place of a decimal (',' instead of '.'). This has thrown me off multiple times
To debug it, add a ton of System.out.println()'s all over your code that spit out a variable when it is changed. These are easier than tools like gdb.
Related
View.startDrag throws NPE and this is happening to me for Android API 15 but not API 22.
This issue is very easy to reproduce. Open Android Studio, create a template project with one blank activity using API 15. Adding the following logic to the floating action button:
fab.setOnLongClickListener(
new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
v.startDrag(ClipData.newPlainText("", ""), new View.DragShadowBuilder(v), null, 0);
return true;
}
}
);
The stacktrace for the error is the following:
E/View: Unable to initiate drag
java.lang.NullPointerException
at android.view.Surface.lockCanvas(Surface.java:77)
at android.view.View.startDrag(View.java:13869)
at test.dragdroptest.MainActivity$2.onLongClick(MainActivity.java:59)
at android.view.View.performLongClick(View.java:3827)
at android.view.View$CheckForLongPress.run(View.java:14571)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Not sure what I did wrong here. I have been fighting this issue for two days and there is not much resource online about this. I cannot believe no one else had ran into the same issue...
Apparently, it only happens on the device I used for testing but not from emulator with the same API level. I guess there is no way I can get away from this issue :(
I have a very strange crash in application.
Stacktrace:
java.lang.IndexOutOfBoundsException: charAt: -1 < 0
at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:112)
at android.text.Selection.setSelection(Selection.java:84)
at android.text.Selection.setSelection(Selection.java:127)
at android.widget.Editor$InsertionHandleView.updateSelection(Editor.java:4971)
at android.widget.Editor$HandleView.positionAtCursorOffset(Editor.java:4647)
at android.widget.Editor$HandleView.updatePosition(Editor.java:4675)
at android.widget.Editor$PositionListener.onPreDraw(Editor.java:2714)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:711)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2097)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1179)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4859)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:664)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
I got the stack trace without any links on my application's packages (classes/methods).
Has anyone encountered this problem? Thx.
EDIT
Can you see, the stack trace has not any links on my application's packages/classes, but app anyway crashed.
UPD
I use Selection.setSelection. I don't use SpannableStringBuilder.
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
Selection.setSelection(s, s.length());
}
}
This seems to be an OS bug which require a combination of a certain OS version(e.g. 4.1.2) and a certain device(e.g. Galaxy S3 I9300). This is not your problem. Unfortunately I don't know how to get around this either.
I'm curious how you ran into this issue. Based on the callstack, my guess is you tap on a textview and tried to paste text from clipboard.
Read the first line... it says java.lang.IndexOutOfBoundsException: charAt: -1 < 0. The problem is your string does not have the character you are searching for. Post the code. Then we will see what's wrong...
I'm seeing exceptions when ActivityManager.isUserAMonkey() is run on older Android devices:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.AndroidApp}: java.lang.RuntimeException: Unknown exception code: 1 msg null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unknown exception code: 1 msg null
at android.os.Parcel.readException(Parcel.java:1257)
at android.os.Parcel.readException(Parcel.java:1235)
at android.app.ActivityManagerProxy.isUserAMonkey(ActivityManagerNative.java:2762)
at android.app.ActivityManager.isUserAMonkey(ActivityManager.java:990)
at <com.myapp....> ...
There is a bit of discussion of the bug here (including a classic thats-not-possible response from one of the developers: "In the standard platform implementation this is pretty much not possible.")
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/tQJcM4O4WxM
Its not clear to me if this always happens, or only happens when running under a test monkey, or only on some devices or what. (I'm running into this problem using Apkudo's device testing service, where the user is always a monkey.) Its not clear when this was fixed, either (it doesn't happen on most (all?) newer devices).
The exception seems to be restricted to Android 2.2 (SDK version 8) releases. And seems to have been a bug in android.app.ActivityManagerNative.
Here's the 2.2.1 code (found in ActivityManagerNative.java on grepcode.com):
1248 case IS_USER_A_MONKEY_TRANSACTION: {
1249 data.enforceInterface(IActivityManager.descriptor);
1250 reply.writeInt(isUserAMonkey() ? 1 : 0);
1251 reply.writeNoException();
1252 return true;
1253 }
Here's the 2.3.1 code (which seems to be the same as much more recent 4.x code which I know works correctly). (Also found on grepcode.com):
1248 case IS_USER_A_MONKEY_TRANSACTION: {
1239 data.enforceInterface(IActivityManager.descriptor);
1240 boolean areThey = isUserAMonkey();
1241 reply.writeNoException();
1242 reply.writeInt(areThey ? 1 : 0);
1243 return true;
1244 }
Notice the order of writeNoException and writeInt are reversed in the newer code. The corresponding code to read the parcel seems to be unchanged since 2.2.1 as far as I can tell:
2749 public boolean isUserAMonkey() throws RemoteException {
2750 Parcel data = Parcel.obtain();
2751 Parcel reply = Parcel.obtain();
2752 data.writeInterfaceToken(IActivityManager.descriptor);
2753 mRemote.transact(IS_USER_A_MONKEY_TRANSACTION, data, reply, 0);
2754 reply.readException();
2755 boolean res = reply.readInt() != 0;
2756 data.recycle();
2757 reply.recycle();
2758 return res;
2759 }
This side reads the exception first, then expects the data.
The javadoc for Parcel readException and writeException imply that they record the exception in the parcel's header (and thus shouldn't impact the actual data in the parcel), but it seems the order does matter.
This means on SDK version 8 the ActivietyManager.isUserAMonkey() API will always throw an exception, monkey or not. Android builds after SDK 8 should not throw this exception.
I suspect the SDK 8 exception message might be slightly different without a monkey ("1 msg null" vs. maybe "0 msg null"?), but don't have an example of the exception without a monkey running.
I am getting a crash report back that says OutOfMemoryError in my call to startManagingCursor. Here is the returned stack trace:
java.lang.OutOfMemoryError
at java.util.ArrayList.add(ArrayList.java:123)
at android.app.Activity.startManagingCursor(Activity.java:1652)
at com.bubblewaretechnology.pianounlockscreen.LockDetecter.onResume(LockDetecter.java:633)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
at android.app.ActivityThread.performNewIntents(ActivityThread.java:1789)
at android.app.ActivityThread.handleNewIntent(ActivityThread.java:1795)
at android.app.ActivityThread.access$2300(ActivityThread.java:124)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1015)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
I am very careful to close my cursors, SQLiteDatabase and SQLiteOpenHelper. Here is a short code segment that the crash refers to:
Cursor callCursor = lockLib.getCallState();
startManagingCursor(callCursor);
....
callCursor.close();
Pretty stumped on how to go about troubleshooting this problem. I know startManagingCursor is depreciated but I am not targeting HONEYCOMB or later, so I cant switch to CursorLoader. Does anyone have any suggestions on how to proceed?
I know it's lame to answer your own question but you can remove startManagingCursor, Reference Link, as long as you are managing the lifecycle of your cursor appropriately.
I am getting the following stack trace (below is a complete copy) , this gives me little or no indication as to where in a massive application it is when going wrong and user feedback is nothing beyond "It crashed".
Is there anything I can do to pinpoint this more ?
java.lang.NullPointerException at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
at android.widget.AdapterView.checkFocus(AdapterView.java:689)
at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:813)
at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:43)
at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:54)
at android.widget.ArrayAdapter$ArrayFilter.publishResults(ArrayAdapter.java:469)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Look at the code for ArrayAdapter.getCount():
/**
* {#inheritDoc}
*/
public int getCount() {
return mObjects.size();
}
Clearly mObjects is null, i.e. your ListView or other AdapterView-derived type is trying to use the adapter before you've initialized it with it's data.
What you could do is putting debug information before every call to getCount() using the Log class
Log.d("tag", "Trying to get count on line 50 class Test");
then open logcat and find the last entry of this log before the error occures. Then you have the right location of the crash.