Android KITKAT (4.0.3 API 15) View.startDrag() throws NullPointerException - android

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 :(

Related

Firebase Authentication causing Android fatal exception

I just finished integrating Firebase real time data base and Authentication into my Android Studio application. It ran successfully two or three times on my Google Nexus 7 genymotion emulator that has google play services on it. Then I tried launching it again and I started getting the below error:
06-25 16:50:46.994 7699-7699/
com.careersvirtualsolutions.uprate E/AndroidRuntime:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.google.firebase.auth.FirebaseAuth$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
I think this has something to do with the Firebase Authentication SDK but I am not sure.
Do you know what this is caused by? How can I solve it?
I actually figured it out, the problem was with the AuthStateListener.
So if you're having a similar issue, try testing your project with the AuthStateListener commented out and see if it launches without crashing. If it does you can then figure out how to replace the existing listener.
Hope this helps in some way!

findViewById returning null TextView

This one is driving me and my team completely crazy.
We can't reproduce as it works well on all our test devices but we are getting thousands of crash reports complaining of java.lang.NullPointerException for a TextView which is obtained by findViewById in onCreate (after calling setContentView and with the correct id and layout file)
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxxxxxxxxxxxx.activities.PremiumActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.NullPointerException
at xxxxxxxxx.activities.PremiumActivity.onCreate(PremiumActivity.java:258)
at android.app.Activity.performCreate(Activity.java:5163)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(NativeStart.java)
Here is the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.premium_activity_layout);
premiumInfoHeaderTextView = (TextView) findViewById(R.id.premiumHeaderTextView);
int themeColor = MPThemeManager.getSharedInstance(this).themeColor();
premiumInfoHeaderTextView.setTextColor(themeColor);
That last line is the one causing the nullPointerException (line 258)
Not sure if it can be related but the activity extends SherlockFragmentActivity
And of course premiumInfoHeaderTextView is declared as a TextView outside the onCreate method as we also use it elsewhere.
A crazy fix to a crazy bug!!
As expected in my earlier comment, the bug comes from the fact the an old layout xml file for tablets (under /res/layout-large) has beed removed in our latest version (we now use a single layout file under /res/layout).
Remember the line causing the Null Exception:
premiumInfoHeaderTextView = (TextView) findViewById(R.id.premiumHeaderTextView);
This is due to the fact that our former version layout did not have any view with id = premiumHeaderTextView and that on some tablets for users who upgraded from the previous to the new version, the old layout file has not been deleted and the system is therefore looking for id in the old /res/layout-large/premium_activity_layout.xml rather than in the new (common to all devices) /res/layout/premium_activity_layout.xml
Using crashlytics to understand that all crashes were happening on tablet devices was key to identify this "bug" which seems like a system bug on some tablets as they should not keep old resource files when the apk is upgrade.
So in summary, I copied the /res/layout/premium_activity_layout.xml to /res/layout-large in order to force tablet users to use the new layout and not an old "cached" version. That has completely fixed the bug

Android. Strange crash in charAt (setSelection)

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...

Android exception - Unknown origin (possibly widget)

I have the following Android exception. It is reported by Bugsense and I cannot find out where it comes from as there is no reference to any class of my app.
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
at android.widget.AbsListView$ListItemAccessibilityDelegate.onInitializeAccessibilityNodeInfo(AbsListView.java:2512)
at android.view.View.onInitializeAccessibilityNodeInfo(View.java:4849)
at android.widget.LinearLayout.onInitializeAccessibilityNodeInfo(LinearLayout.java:1768)
at android.view.View.createAccessibilityNodeInfo(View.java:4812)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:825)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:848)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:848)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:848)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:848)
at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchAccessibilityNodeInfos(AccessibilityInteractionController.java:734)
at android.view.AccessibilityInteractionController.findAccessibilityNodeInfoByAccessibilityIdUiThread(AccessibilityInteractionController.java:205)
at android.view.AccessibilityInteractionController.access$400(AccessibilityInteractionController.java:49)
at android.view.AccessibilityInteractionController$PrivateHandler.handleMessage(AccessibilityInteractionController.java:985)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
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:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
I do suspect it has something to do with updating a widget. Any ideas on how to trace?
Got similar Nullpointer with a Google map view.
try and turn off any accessibility options enabled on your phone?
And there is a another accessibility bug reported on the Android Issues tracker here
It looks like there is no easy way to turn of accessibility from code, except for overriding the addChildrenForAccessibility method in a custom view. (see above link)

App crashes on super.onSaveInstanceState [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NullPointerException on onSaveInstanceState with AndroidFragments
when I call super.onSaveInstanceState I get a
NullPointerException
at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1542)
on some devices, some others work.
public void onSaveInstanceState(Bundle savedInstanceState) {
//...
super.onSaveInstanceState(savedInstanceState);
}
With Android 4.1 on a Nexus Tablet its working, on a Samsung with Android 4.0 not. What is the problem here and how can I solve it? Uncommenting onSaveInstanceState works, but I dont think this is correct and it makes my app behave incorrect somewhere else.
Thanks
Marvin
edit
added stacktrace
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1542)
at android.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1572)
at android.app.Activity.onSaveInstanceState(Activity.java:1164)
at com.MApp.MActivity.onSaveInstanceState(MActivity.java:1111)
at android.app.Activity.performSaveInstanceState(Activity.java:1113)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1188)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3337)
at android.app.ActivityThread.access$700(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1163)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
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:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Ok I figured it out now.
This is because my fragment had no
onSaveInstanceState
I had to fill the bundle with dummy values.
It's a bug in android in version 4.0. However, I don't know about other versions.

Categories

Resources