I am working on a game in which a player can drag things around the screen. I have a private method that allows me to model a drag event for any of the elements that a player can move. For drag and drop, I actually leave the view that they touched where it is, and creating a new ImageView with and setting drawable to the drawing Cache from the affected view, and then moving this newly created ImageView around the screen, following their finger. When you release your finger (drop the view), I call myLayout.remove(movingImg); to disconnect it from the screen. I had a problem when, if I start a simulated drag and drop event and then manually take one of the other elements, I get a null pointer to the myLayout.remove () call, here is the trace from the log:
04-06 10:37:43.610: ERROR/AndroidRuntime(23203): java.lang.NullPointerException 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2122) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewGroup.drawChild(ViewGroup.java:2506) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewGroup.drawChild(ViewGroup.java:2506) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewGroup.drawChild(ViewGroup.java:2506) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewGroup.drawChild(ViewGroup.java:2506) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2123) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.View.draw(View.java:9032) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.widget.FrameLayout.draw(FrameLayout.java:419) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1910) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewRoot.draw(ViewRoot.java:1608) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewRoot.performTraversals(ViewRoot.java:1329) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.view.ViewRoot.handleMessage(ViewRoot.java:1944) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.os.Handler.dispatchMessage(Handler.java:99) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.os.Looper.loop(Looper.java:126) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at android.app.ActivityThread.main(ActivityThread.java:3997) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at java.lang.reflect.Method.invokeNative(Native Method) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at java.lang.reflect.Method.invoke(Method.java:491) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 04-06 10:37:43.610: ERROR/AndroidRuntime(23203): at dalvik.system.NativeStart.main(Native Method)
The footprint does not indicate anywhere inside my activity. An exception is thrown whenever it tries to call myLayout.remove () in drag and drop simulation mode. I surrounded this line with a try / trick, but did nothing. I know this is the line that causes me problems, because if I comment on this, I will not get an exception, but obviously my view will never be removed from the screen. I create this application on Motorola xoom, I am not sure if this is a specific device problem or not. Does anyone know what can happen here?
This is where .removeView () is called from:
@Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub Log.i(myTag, "Animation End"); try { //myLayout.removeView(simulateMovingImg); //This is the line that is throwing a null pointer simulateMovingImg.setVisibility(View.GONE); //This is how I am currently getting around the issue params = new LayoutParams(oneImg.getWidth(),oneImg.getHeight()); // While its moving it appears larger than normal simulateMovingImg.setLayoutParams(params); // so I set it back to the normal size if(whichTarget == true){ targetImg.setImageDrawable(simulateMovingImg.getDrawable()); //targetImg is one of the 'drop zones' so I set its // drawable to make it seem like the view was 'dropped' into this zone }else{ target1Img.setImageDrawable(simulateMovingImg.getDrawable()); // same with target1Img. } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } iAmDone = true; } });
This is inside the AnimationListener for translational animation, which moves my ImageView from where it starts, where it "drops"
android nullpointerexception layout view
FoamyGuy Apr 6 2018-11-11T00: 00Z
source share