2

I am working on an application that uses canvas.I am using a custom imageview. But when I try to run the application I get an error. The code I have written is as follows

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;
            }
            }

           }





}

and in my xml file where I get the error is :

<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" />        

and log cat is:

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
5
  • 1
    Caused by: java.lang.ClassNotFoundException: com.infy.DrawActivity.myView <--- everything allright within AndroidManifest.xml? Commented Jul 22, 2013 at 13:02
  • yes,its something to do with inflating the myView class Commented Jul 22, 2013 at 13:03
  • It's an inner class right? try referencing it like so: com.infy.DrawActivity$myView stackoverflow.com/questions/2098318/… Commented Jul 22, 2013 at 13:10
  • Use MyView instead of "myView" & try again. Commented Jul 22, 2013 at 13:11
  • 1
    Make all constructrs public. change <ImageView class= to <view class= Commented Jul 23, 2013 at 8:30

3 Answers 3

2

It's an inner class of DrawActivity so you need to reference it in your XML like so:

<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" />

See here: Error referencing an inner class View in layout/main.xml

Alternatively, move it to it's own class.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi , I am still getting an error though. I am getting a class cast exception. The logcat says can't cast widget. imageview to drawactivit$MyView.Could you help me with this .
Can you ask a new question with the latest code? It's quite confusing now and it will make it easier for people to help you with this specific problem. Strangely it's referencing MyView with a capital m but I don't see that anywhere in your code...
Hi, I am not allowed to ask a new question. I have edited the entire question again. Can you please look into it
1

Try adding

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

In your custom ImageView.

3 Comments

You are correct. But this is not the main crash problem. It would appear after OP fixes ClassNotFoundException.
Hi , you were right. But now I am getting a class cast exception. The logcat says can't cast widget. imageview to drawactivit$MyView.
The class cast exception is due to the fact you are using inner class for view. Make a separate class for your View either in the same file or other file and change path in xml.
1

Define your class as public static and add all necessary View constructors

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);
    }

And reference it like this

<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" />

Tip: according to Java conventions class names should start from upper-case letter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.