将可拖动图像视图添加到现有相对视图

时间:2016-05-10 18:58:11

标签: android imageview

目前我有以下代码。它将imageview添加到现有背景中。然而,在创建图像后,程序在我尝试拖动时崩溃了...任何人都有任何关于它为什么不起作用的想法?

ballbutton.setOnClickListener (new View.OnClickListener(){
        @Override
        public void onClick(View view){
            ImageView iv = new ImageView(getApplicationContext());
            iv.setImageDrawable(getDrawable(R.drawable.ball));
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.RIGHT_OF, R.id.resistorbutton);
            iv.setLayoutParams(lp);
            iv.setOnTouchListener(new myListener());
            relativeLayout.addView(iv);
        }
    });

日志如下:

---------崩溃的开始

E / AndroidRuntime:致命异常:主要                                                                               处理:com.example.dan.ball,PID:2795                                                                               java.lang.NullPointerException:尝试调用虚方法' void android.view.ViewGroup.invalidate()'在null对象引用上                                                                                   在com.example.dan.circuit.ball $ myListener.onTouch(MainActivity.java:77)                                                                                   在android.view.View.dispatchTouchEvent(View.java:9290)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)                                                                                   在com.android.internal.policy.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)                                                                                   在com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)                                                                                   在android.app.Activity.dispatchTouchEvent(Activity.java:2765)                                                                                   在com.android.internal.policy.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2364)                                                                                   在android.view.View.dispatchPointerEvent(View.java:9514)                                                                                   在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)                                                                                   在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)                                                                                   在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3642)                                                                                   在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3695)                                                                                   在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3661)                                                                                   在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3787)                                                                                   在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3669)                                                                                   在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3844)                                                                                   在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3642)                                                                                   在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3695)                                                                                   在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3661)                                                                                   在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3669)                                                                                   在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3642)                                                                                   在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)                                                                                   在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)                                                                                   在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)                                                                                   在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)                                                                                   在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)                                                                                   在android.os.MessageQueue.nativePollOnce(本机方法)                                                                                   在android.os.MessageQueue.next(MessageQueue.java:323)                                                                                   在android.os.Looper.loop(Looper.java:135)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

好的,所以我自己设法解决了这个问题。在我的myListener类中,我使用invalidate()来重绘我不需要的视图。