Android应用程序在运行时崩溃?

发布于 2024-11-15 07:33:24 字数 3620 浏览 2 评论 0原文

我正在 android 中制作一个小应用程序来缩放图像,但该应用程序在运行时崩溃。这是代码:

package new1.zoom;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;


    public class newzoom extends View {
        private Drawable image;
        private int zoomControler=200;
        public newzoom(Context context)
        {
        super(context);
        image=context.getResources().getDrawable(R.drawable.me);
        setFocusable(true);
        }
    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //here u can control the width and height of the images........ this line is very important
    image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
    image.draw(canvas);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_DPAD_UP)// zoom in
    zoomControler+=10;
    if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) // zoom out
    zoomControler-=10;
    if(zoomControler<10)
    zoomControler=10;
    invalidate();
    return true;
    }
    } 

有人能帮我解决这个问题吗?

这是我的 logcat 文件:

06-14 11:30:14.640: ERROR/AndroidRuntime(985): FATAL EXCEPTION: main
06-14 11:30:14.640: ERROR/AndroidRuntime(985): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{new1.zoom/new1.zoom.newzoom}: java.lang.InstantiationException: new1.zoom.newzoom
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.os.Looper.loop(Looper.java:123)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at java.lang.reflect.Method.invoke(Method.java:521)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at dalvik.system.NativeStart.main(Native Method)
06-14 11:30:14.640: ERROR/AndroidRuntime(985): Caused by: java.lang.InstantiationException: new1.zoom.newzoom
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at java.lang.Class.newInstanceImpl(Native Method)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at java.lang.Class.newInstance(Class.java:1429)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     ... 11 more

I am making a small application in android to zoom an image but the application crashes at runtime. Here is the code:

package new1.zoom;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;


    public class newzoom extends View {
        private Drawable image;
        private int zoomControler=200;
        public newzoom(Context context)
        {
        super(context);
        image=context.getResources().getDrawable(R.drawable.me);
        setFocusable(true);
        }
    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //here u can control the width and height of the images........ this line is very important
    image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
    image.draw(canvas);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_DPAD_UP)// zoom in
    zoomControler+=10;
    if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) // zoom out
    zoomControler-=10;
    if(zoomControler<10)
    zoomControler=10;
    invalidate();
    return true;
    }
    } 

Can anybody help me to solve this problem?

Here is my logcat file:

06-14 11:30:14.640: ERROR/AndroidRuntime(985): FATAL EXCEPTION: main
06-14 11:30:14.640: ERROR/AndroidRuntime(985): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{new1.zoom/new1.zoom.newzoom}: java.lang.InstantiationException: new1.zoom.newzoom
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.os.Looper.loop(Looper.java:123)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at java.lang.reflect.Method.invoke(Method.java:521)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at dalvik.system.NativeStart.main(Native Method)
06-14 11:30:14.640: ERROR/AndroidRuntime(985): Caused by: java.lang.InstantiationException: new1.zoom.newzoom
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at java.lang.Class.newInstanceImpl(Native Method)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at java.lang.Class.newInstance(Class.java:1429)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
06-14 11:30:14.640: ERROR/AndroidRuntime(985):     ... 11 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-11-22 07:33:24

请浏览给定的链接。它使用多点触控和点击来缩放图像:Android 多点触控缩放。其中有一个链接: http://blog.sephiroth .it/2011/04/04/imageview-zoom-and-scroll/ 它对我有用。请也尝试一下这个。

please go through the given link. It uses multi touch and tap to zoom the image: Android Multi touch zooming. In that there a link: http://blog.sephiroth.it/2011/04/04/imageview-zoom-and-scroll/ It worked for me. Please try this also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文