在android中运行时应用程序崩溃?
问题描述:
我正在android中制作一个小应用程序来放大图像,但应用程序在运行时崩溃。这里是代码:在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
答
请到通过给定链路。它使用多点触摸并点击缩放图像:Android Multi touch zooming。在那里有一个链接:http://blog.sephiroth.it/2011/04/04/imageview-zoom-and-scroll/它为我工作。请尝试这一点。
您是否尝试调试?什么是抛出的异常(查看日食logcat)。 – Drakosha 2011-06-14 05:59:05
我已经在上面的问题中放置了日志猫文件。谢谢 – 2011-06-14 06:04:07
这是连接到实例创建。你如何创建这个类的实例?你的活动代码是什么。这看起来很好。也许onDraw有点可疑。尝试没有,也许只是作为一个支票。 – DArkO 2011-06-14 06:15:33