致命的例外

时间:2014-04-17 09:32:56

标签: android eclipse android-layout

    package com.example.artsecappp;

    import android.app.Activity;


    import android.content.ClipData;
    import android.content.Intent;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.view.DragEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.DragShadowBuilder;
    import android.view.View.OnClickListener;
    import android.view.View.OnDragListener;
    import android.view.View.OnTouchListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.GridLayout;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.Toast;

    public class DragandDrop extends Activity {

        Button b1, c1;
        GridLayout grd;
        public int id, iID, IDL, DropId;
        LinearLayout l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12;
        public ImageView img, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dragand_drop);
            grd = (GridLayout) findViewById(R.id.gridviewww);
            l1 = (LinearLayout) findViewById(R.id.zerozero);
            l2 = (LinearLayout) findViewById(R.id.onezero);
            l3 = (LinearLayout) findViewById(R.id.twozero);
            l4 = (LinearLayout) findViewById(R.id.zeroone);
            l5 = (LinearLayout) findViewById(R.id.oneone);
            l6 = (LinearLayout) findViewById(R.id.twoone);
            l7 = (LinearLayout) findViewById(R.id.zerotwo);
            l8 = (LinearLayout) findViewById(R.id.onetwo);
            l9 = (LinearLayout) findViewById(R.id.twotwo);
            l10 = (LinearLayout) findViewById(R.id.zerothree);
            l11 = (LinearLayout) findViewById(R.id.onethree);
            l12 = (LinearLayout) findViewById(R.id.twothree);

            i1 = (ImageView) findViewById(R.id.myimage1);
            i2 = (ImageView) findViewById(R.id.myimage2);
            i3 = (ImageView) findViewById(R.id.myimage3);
            i4 = (ImageView) findViewById(R.id.myimage4);
            i5 = (ImageView) findViewById(R.id.myimage5);
            i6 = (ImageView) findViewById(R.id.myimage6);
            i7 = (ImageView) findViewById(R.id.myimage7);
            i8 = (ImageView) findViewById(R.id.myimage8);
            i9 = (ImageView) findViewById(R.id.myimage9);
            i10 = (ImageView) findViewById(R.id.myimage10);
            i11 = (ImageView) findViewById(R.id.myimage11);
            i12 = (ImageView) findViewById(R.id.myimage12);

            findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage5).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage6).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage7).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage8).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage9).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage10).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage11).setOnTouchListener(new MyTouchListener());
            findViewById(R.id.myimage12).setOnTouchListener(new MyTouchListener());

            findViewById(R.id.zerozero).setOnDragListener(new MyDragListener());
            findViewById(R.id.zeroone).setOnDragListener(new MyDragListener());
            findViewById(R.id.zerotwo).setOnDragListener(new MyDragListener());
            findViewById(R.id.zerothree).setOnDragListener(new MyDragListener());
            findViewById(R.id.zerofour).setOnDragListener(new MyDragListener());
            findViewById(R.id.onezero).setOnDragListener(new MyDragListener());
            findViewById(R.id.oneone).setOnDragListener(new MyDragListener());
            findViewById(R.id.onetwo).setOnDragListener(new MyDragListener());
            findViewById(R.id.onethree).setOnDragListener(new MyDragListener());
            findViewById(R.id.onefour).setOnDragListener(new MyDragListener());
            findViewById(R.id.twozero).setOnDragListener(new MyDragListener());
            findViewById(R.id.twoone).setOnDragListener(new MyDragListener());
            findViewById(R.id.twotwo).setOnDragListener(new MyDragListener());
            findViewById(R.id.twothree).setOnDragListener(new MyDragListener());
            findViewById(R.id.twofour).setOnDragListener(new MyDragListener());

            i1.setId(1);
            i2.setId(2);
            i3.setId(3);
            i4.setId(4);
            i5.setId(5);
            i6.setId(6);
            i7.setId(7);
            i8.setId(8);
            i9.setId(9);
            i10.setId(10);
            i11.setId(11);
            i12.setId(12);

            l1.setId(20);
            l2.setId(21);
            l3.setId(22);
            l4.setId(23);
            l5.setId(24);
            l6.setId(25);
            l7.setId(26);
            l8.setId(27);
            l9.setId(28);
            l10.setId(29);
            l11.setId(30);
            l12.setId(31);

            b1 = (Button) findViewById(R.id.back);
            c1 = (Button) findViewById(R.id.cntnue);

            c1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                    Intent intnt = new Intent(DragandDrop.this, Confirmation.class);
                    startActivity(intnt);

                }
            });

        }

        public class MyTouchListener implements OnTouchListener {

            public boolean onTouch(View view, MotionEvent motionEvent) {

                MyDragListener drag = new MyDragListener();
                drag.view1 = view;

                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    // Getting id of image which is dragged from any of linear
                    // layout
                    iID = view.getId();
                    img = (ImageView) view;


                    Toast.makeText(DragandDrop.this, "Image ID::" + iID,
                            Toast.LENGTH_LONG).show();
                    ClipData data = ClipData.newPlainText("", "");
                    DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                            view);
                    view.startDrag(data, shadowBuilder, view, 0);

                    //view.setVisibility(View.VISIBLE);

                    return true;
                } else {
                    return false;
                }
            }

        }

        public class MyDragListener implements OnDragListener {
            Drawable enterShape = getResources().getDrawable(
                    R.drawable.shape_droptarget);
            Drawable normalShape = getResources().getDrawable(R.drawable.shape);

            public View view1;

            @Override
            public boolean onDrag(View v, DragEvent event) {

                switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    // do nothing
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    v.setBackgroundDrawable(enterShape);
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    v.setBackgroundDrawable(normalShape);
                    break;
                case DragEvent.ACTION_DROP:
                    // Dropped, reassign View to ViewGroup

                    View view = (View) event.getLocalState();

                    ViewGroup owner = (ViewGroup) view.getParent();
                    Toast.makeText(getApplicationContext(), "<>>>>>>>>>>>"+ owner.getId(), Toast.LENGTH_LONG).show();
                    owner.removeView(view);

                    LinearLayout container = (LinearLayout) v;

                    container.addView(view);

                    view.setVisibility(View.VISIBLE);

                    DropId = v.getId();

                    Toast.makeText(DragandDrop.this, "Drop ID::" + DropId +" Linear Layout >>>>"+ container.getId(),
                            Toast.LENGTH_LONG).show();

    //              int child = container.getChildCount();
    //              Toast.makeText(getApplicationContext(), "No Of hild >>>" + child, Toast.LENGTH_LONG).show();
    //              ImageView imgC = (ImageView) container.getChildAt(0);
    //              
    //              Drawable currentDraw = imgC.getDrawable();
    //              
    //              Drawable selectedDraw = img.getDrawable();
    //              
    //              imgC.setImageDrawable(selectedDraw);
    //              
    //              img.setImageDrawable(currentDraw);

                    if(l1.getId()==20)
                    {
                        int childcount1 = l1.getChildCount();
                        View vat1 = null;
                        for (int i = 0; i < childcount1; i++) {
                            vat1 = l1.getChildAt(i);
                            Toast.makeText(new DragandDrop(), "Childcount in l1=="+ childcount1,Toast.LENGTH_LONG).show();
                        }
                        vat1.setVisibility(View.VISIBLE);

                    }

                    if(l2.getId()==21)
                    {
                        int childcount2 = l2.getChildCount();
                        View vat2 = null;
                        for (int i = 0; i < childcount2; i++) {
                            vat2 = l2.getChildAt(i);
                            Toast.makeText(new DragandDrop(), "Childcount in l1=="+ childcount2,Toast.LENGTH_LONG).show();
                        }
                        vat2.setVisibility(View.VISIBLE);

                    }

                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    v.setBackgroundDrawable(normalShape);

                default:
                    break;
                }
                return true;
            }
        }

    }

错误

04-17 12:48:34.390: I/System.out(452): measured width0
04-17 12:48:34.500: I/System.out(452):  width0
04-17 12:48:34.580: I/System.out(452): windowwidth480
04-17 12:48:34.580: I/System.out(452): windowheight800
04-17 12:48:34.680: D/AndroidRuntime(452): Shutting down VM
04-17 12:48:34.690: W/dalvikvm(452): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-17 12:48:34.720: E/AndroidRuntime(452): FATAL EXCEPTION: main
04-17 12:48:34.720: E/AndroidRuntime(452): java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
04-17 12:48:34.720: E/AndroidRuntime(452):  at android.os.Parcel.readException(Parcel.java:1331)
04-17 12:48:34.720: E/AndroidRuntime(452):  at android.os.Parcel.readException(Parcel.java:1281)
04-17 12:48:34.720: E/AndroidRuntime(452):  at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:626)
04-17 12:48:34.720: E/AndroidRuntime(452):  at android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:3558)
04-17 12:48:34.720: E/AndroidRuntime(452):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1165)
04-17 12:48:34.720: E/AndroidRuntime(452):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
04-17 12:48:34.720: E/AndroidRuntime(452):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 12:48:34.720: E/AndroidRuntime(452):  at android.os.Looper.loop(Looper.java:137)
04-17 12:48:34.720: E/AndroidRuntime(452):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-17 12:48:34.720: E/AndroidRuntime(452):  at java.lang.reflect.Method.invokeNative(Native Method)

我正在致命的例外。我要做的是计算掉落条件下每个布局的子计数。确切地说,我必须将图像从一个线性布局拖放到另一个。在添加条件之前我是拖放成功的。但在添加条件时,会发生致命异常。所以请帮助我。提前谢谢..

1 个答案:

答案 0 :(得分:1)

试试这个..

new DragandDrop()

中的所有DragandDrop.this更改为Toast

更改此

Toast.makeText(new DragandDrop(), "Childcount in l1=="+ childcount1,Toast.LENGTH_LONG).show();

Toast.makeText(DragandDrop.this, "Childcount in l1=="+ childcount1,Toast.LENGTH_LONG).show();