Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:16:07+00:00 2026-06-03T03:16:07+00:00

I copied this code from a book and tried to run that , basically

  • 0

I copied this code from a book and tried to run that , basically it draw a rectangle in the screen and then try to apply zoom and drag to it . but unfortunately it’s not working and give me fetal error . what’s the problem?

here is the code :

public class DrawStuffActivity extends Activity implements
    OnTouchListener {

      MyView myView;
      int numberOfFingers = 0;
      float oldX[] = new float[2], oldY[] = new float[2];
      Rect rectangle = new Rect(0, 0, 100, 100);
      DisplayMetrics metrics = new DisplayMetrics();
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myView = new MyView(this);
        setContentView(myView);
        myView.setOnTouchListener(this);
        getWindowManager().getDefaultDisplay().
          getMetrics(metrics);
      }
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
          numberOfFingers = 1;
          oldX[0] = event.getX(0);
          oldY[0] = event.getY(0);
          break;
        case MotionEvent.ACTION_POINTER_DOWN:
          numberOfFingers = 2;
          oldX[1] = event.getX(1);
          oldY[1] = event.getY(1);
          break;
        case MotionEvent.ACTION_MOVE:
          handleMove(event);
          break;
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_UP:
          numberOfFingers--;
          break;
        }
        view.invalidate();
        return true;
      }
    float newX[] = new float[2], newY[] = new float[2];
    int xChange[] = new int[2], yChange[] = new int[2];
    int diffX, diffY;
    int newLeft = rectangle.left, newTop = rectangle.top,
        newRight = rectangle.right,
        newBottom = rectangle.bottom;
    void handleMove(MotionEvent event) {
      newX[0] = Math.round(event.getX(0));
      newY[0] = Math.round(event.getY(0));
      xChange[0] = Math.round(newX[0] - oldX[0]);
      yChange[0] = Math.round(newY[0] - oldY[0]);
      oldX[0] = newX[0];
      oldY[0] = newY[0];
      switch (numberOfFingers) {
      case 1:

          newLeft = rectangle.left + xChange[0];
            newTop = rectangle.top + yChange[0];
            newRight = rectangle.right + xChange[0];
            newBottom = rectangle.bottom + yChange[0];
            if (newLeft < 0 || newRight > metrics.widthPixels) {
              newLeft = rectangle.left;
              newRight = rectangle.right;
            }
            if (newTop < 0 || newBottom > metrics.heightPixels) {
              newTop = rectangle.top;
              newBottom = rectangle.bottom;
            }
            rectangle =
                new Rect(newLeft, newTop, newRight, newBottom);
            break;
          case 2:
            newX[1] = Math.round(event.getX(1));
            newY[1] = Math.round(event.getY(1));
            diffX =
                Math.abs(Math.round(newX[1] - newX[0]))
                    - Math.abs(Math.round(oldX[1] - oldX[0]));
            diffY =
                Math.abs(Math.round(newY[1] - newY[0]))
                    - Math.abs(Math.round(oldY[1] - oldY[0]));
            oldX[1] = newX[1];
            oldY[1] = newY[1];
            newLeft = rectangle.left - diffX / 2;
            newTop = rectangle.top - diffY / 2;
            newRight = rectangle.right + diffX / 2;
            newBottom = rectangle.bottom + diffY / 2;
            rectangle =
                new Rect(newLeft, newTop, newRight, newBottom);
            break;
          }

    }

    // The handleMove method is in Listing 3-6.
      class MyView extends View {
        Paint whitePaint = new Paint();
        MyView(Context context) {
            super(context);
            whitePaint.setColor(Color.WHITE);
          }
          @Override
          public void onDraw(Canvas canvas) {
            canvas.drawRect(rectangle, whitePaint);
          }
        }
      }

and here is the error :

 05-03 08:40:57.597: E/AndroidRuntime(278): FATAL EXCEPTION: main
05-03 08:40:57.597: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.allmycode.draw/com.allmycode.draw.DrawstaffActivity}: java.lang.ClassNotFoundException: com.allmycode.draw.DrawstaffActivity in loader dalvik.system.PathClassLoader[/data/app/com.allmycode.draw-1.apk]
05-03 08:40:57.597: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-03 08:40:57.597: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-03 08:40:57.597: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-03 08:40:57.597: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-03 08:40:57.597: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 08:40:57.597: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
05-03 08:40:57.597: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 08:40:57.597: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 08:40:57.597: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
05-03 08:40:57.597: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 08:40:57.597: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 08:40:57.597: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
05-03 08:40:57.597: E/AndroidRuntime(278): Caused by: java.lang.ClassNotFoundException: com.allmycode.draw.DrawstaffActivity in loader dalvik.system.PathClassLoader[/data/app/com.allmycode.draw-1.apk]
05-03 08:40:57.597: E/AndroidRuntime(278):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-03 08:40:57.597: E/AndroidRuntime(278):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-03 08:40:57.597: E/AndroidRuntime(278):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-03 08:40:57.597: E/AndroidRuntime(278):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-03 08:40:57.597: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-03 08:40:57.597: E/AndroidRuntime(278):  ... 11 more

the manifest.xml seems ok :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.allmycode.draw"
    android:versionCode="1"
    android:versionName="1.0" >

  <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".DrawstaffActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:16:08+00:00Added an answer on June 3, 2026 at 3:16 am

    Works fine for me. Probably something with your project setup.
    I just created a new project and copied your code into it.
    A white square that can be moved around.

    [edit]
    Now when you did post the rest of the logcat its easier to see what happened.
    The classnotfound error comes from time to time. I learned that the easiest way to resolv it, is to create a new project, and move the code to the new project.

    [edit again]

    Your manifest: .DrawstaffActivity
    Your code: DrawStuffActivity

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is copied from another file and it works in that file, I
Guys that is code copied from a book (Programming Windows 5th edition): #include <windows.h>
when i copied this code from page error found that there is syntax error
This code snippet hand copied from a book I am reading: /* scmp: string
I just copied this code from the MIT video lecture that is posted online:
I copied this code from another post. I tried the example, however, I am
I'm having trouble with arrays. I copied this code from a book: #include <stdio.h>
I copied this code from an example . I've read it 100 times. Array.prototype.map
I have this sample code for async operations (copied from the interwebs) public class
i have copied this code from the FB.init documentation : <script src=http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US type=text/javascript></script> ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.