0
我正在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
您是否嘗試調試?什麼是拋出的異常(查看日食logcat)。 – Drakosha 2011-06-14 05:59:05
我已經在上面的問題中放置了日誌貓文件。謝謝 – 2011-06-14 06:04:07
這是連接到實例創建。你如何創建這個類的實例?你的活動代碼是什麼。這看起來很好。也許onDraw有點可疑。嘗試沒有,也許只是作爲一個支票。 – DArkO 2011-06-14 06:15:33