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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请浏览给定的链接。它使用多点触控和点击来缩放图像: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.