处理自定义imageview时的Classcast异常

时间:2013-07-22 12:59:29

标签: android

我正在使用canvas的应用程序。我正在使用自定义imageview。但是当我尝试运行应用程序时,我收到一个错误。我写的代码如下

private static MyView myimageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            myimageView = (MyView) findViewById(R.id.myView);

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            myimageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            Intent i = getIntent();     
            loc = i.getFloatArrayExtra("Location");
            myimageView.buildDrawingCache();

            bmp1 = Bitmap.createBitmap(myimageView.getDrawingCache());
            bmp2 = Bitmap.createBitmap(myimageView.getDrawingCache());


        }


    }

    public static class MyView extends ImageView implements OnTouchListener{ // or some other View-based class
           public MyView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

           MyView(Context context, AttributeSet attrs) {
                super(context, attrs);
            }

            MyView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }

        boolean drawRectangle = false;
           PointF beginCoordinate;
           PointF endCoordinate;



           public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int count=1;

                if(event.getAction() ==MotionEvent.ACTION_DOWN) {


                     if (count==1){

                         x1 = event.getX();
                         y1 = event.getY();



                         System.out.println(count+"count of value a;skd");

                         Log.i(TAG, "coordinate x1 : "+String.valueOf(x1)+" y1 : "+String.valueOf(y1));
                     }
                }
                     else if(event.getAction() ==MotionEvent.ACTION_MOVE){
                         invalidate();
                        // imageView.setImageBitmap(bmp2);
                         x2 = event.getX();
                         y2 = event.getY();
                         posX=(float)(x1+x2)/2;
                         posY=(float)(y1+y2)/2;
                         radius=(float) Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))/2;
                        // onDraw();
                            }
                if(event.getAction() ==MotionEvent.ACTION_UP)
                     {
                    invalidate();
                     // onDraw();
                        //bmp2=bmp;
                         if(action==2)
                         {


                     }
                     }





                return true;

            }

           public void onDraw(){
                bmp = Bitmap.createBitmap(myimageView.getWidth(), myimageView.getHeight(), Config.ARGB_8888);
                c = new Canvas(bmp);
                myimageView.draw(c);
                Paint pnt = new Paint();
                pnt.setColor(Color.BLUE);
                pnt.setStyle(Paint.Style.STROKE);
                pnt.setStrokeWidth(3);
            if(action==1)
            {

                c.drawCircle(posX,posY, radius, pnt);
                pnt.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                myimageView.setImageBitmap(bmp);
                i = 1;
            }else{

                float h=(float) 30.0;

                float phi = (float) Math.atan2(y2 - y1, x2 - x1); 
                float angle1 = (float) (phi - Math.PI / 6); 
                float angle2 = (float) (phi + Math.PI / 6);

                float x3 = (float) (x2 - h * Math.cos(angle1));
                float x4 = (float) (x2 - h * Math.cos(angle2));
                float y3 = (float) (y2 -  h * Math.sin(angle1));
                float y4 = (float) (y2 -  h * Math.sin(angle2));

               c.drawLine(x1, y1,x2,y2 ,pnt);
               c.drawLine(x2, y2,x3,y3 ,pnt);
                c.drawLine(x2, y2,x4,y4 ,pnt);



                myimageView.setImageBitmap(bmp);
                i=1;
            }
            }

           }





}

在我的xml文件中,我得到的错误是:

<ImageView class= "com.infy.DrawActivity$MyView"
    android:id="@+id/myView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.01"
    android:src="@drawable/ic_launcher" />        

和log cat是:

07-23 10:47:13.098: E/AndroidRuntime(815): FATAL EXCEPTION: main
07-23 10:47:13.098: E/AndroidRuntime(815): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/29 }} to activity {com.infy/com.infy.DrawActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.infy.DrawActivity$MyView
07-23 10:47:13.098: E/AndroidRuntime(815):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
07-23 10:47:13.098: E/AndroidRuntime(815):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
07-23 10:47:13.098: E/AndroidRuntime(815):  at android.app.ActivityThread.access$1100(ActivityThread.java:123)
07-23 10:47:13.098: E/AndroidRuntime(815):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
07-23 10:47:13.098: E/AndroidRuntime(815):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 10:47:13.098: E/AndroidRuntime(815):  at android.os.Looper.loop(Looper.java:137)
07-23 10:47:13.098: E/AndroidRuntime(815):  at android.app.ActivityThread.main(ActivityThread.java:4424)
07-23 10:47:13.098: E/AndroidRuntime(815):  at java.lang.reflect.Method.invokeNative(Native Method)
07-23 10:47:13.098: E/AndroidRuntime(815):  at java.lang.reflect.Method.invoke(Method.java:511)
07-23 10:47:13.098: E/AndroidRuntime(815):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-23 10:47:13.098: E/AndroidRuntime(815):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-23 10:47:13.098: E/AndroidRuntime(815):  at dalvik.system.NativeStart.main(Native Method)
07-23 10:47:13.098: E/AndroidRuntime(815): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.infy.DrawActivity$MyView
07-23 10:47:13.098: E/AndroidRuntime(815):  at com.infy.DrawActivity.onActivityResult(DrawActivity.java:153)
07-23 10:47:13.098: E/AndroidRuntime(815):  at android.app.Activity.dispatchActivityResult(Activity.java:4649)
07-23 10:47:13.098: E/AndroidRuntime(815):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
07-23 10:47:13.098: E/AndroidRuntime(815):  ... 11 more

3 个答案:

答案 0 :(得分:2)

它是DrawActivity的内部类,因此您需要在XML中引用它,如下所示:

<view class="com.infy.DrawActivity$myView" 
    android:id="@+id/myView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.01"
    android:src="@drawable/ic_launcher" />

见这里:Error referencing an inner class View in layout/main.xml

或者,将它移到它自己的班级。

答案 1 :(得分:1)

将您的类定义为public static并添加所有必需的View构造函数

public static class myView extends ImageView {
    myView(Context context) {
        super(context);
    }

    myView(Context context, AttributeSet attrs) {
        super(context, attrs)
    }

    myView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

并像这样引用它

<view class="com.infy.DrawActivity$myView"
    android:id="@+id/myView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.01"
    android:src="@drawable/ic_launcher" />

提示:根据Java约定,类名应以大写字母开头。

答案 2 :(得分:1)

尝试添加

myView(Context context, AttributeSet attrs) and myView(Context context, AttributeSet attrs, int defStyle)

在自定义ImageView中。