重写View.layout(...)中的VirtualMachineError

时间:2012-09-18 11:06:39

标签: android android-view dalvik android-2.3-gingerbread android-inflate

Android抛出了致命的错误,然后我给类充气,它已经覆盖了方法:

Class MyButton extends Button {
    ....
    @Override
    public void layout(int l, int t, int r, int b) {
        super.layout(l, t, r, b);
    }
}

如果我评论我的代码无关紧要。应用程序只能删除这个被覆盖的函数。

错误:

09-18 13:45:43.809: E/AndroidRuntime(13363): FATAL EXCEPTION: main
09-18 13:45:43.809: E/AndroidRuntime(13363): java.lang.VirtualMachineError
09-18 13:45:43.809: E/AndroidRuntime(13363):    at dalvik.system.DexFile.defineClass(Native Method)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:207)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:200)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.view.View.inflate(View.java:8807)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at com.eee.android.activities.Screen1.addSubject(Screen1.java:372)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at com.xxx.android.activities.Screen1.parseData(Screen1.java:348)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at com.xxx.android.activities.Screen1.initalizeData(Screen1.java:178)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at com.xxx.android.activities.Screen1.onResume(Screen1.java:139)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.app.Activity.performResume(Activity.java:3832)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.os.Looper.loop(Looper.java:123)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at android.app.ActivityThread.main(ActivityThread.java:3687)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at java.lang.reflect.Method.invoke(Method.java:507)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-18 13:45:43.809: E/AndroidRuntime(13363):    at dalvik.system.NativeStart.main(Native Method)

有什么想法吗?

编辑:我以其他方式解决了我的问题。只是将drawable生成代码移动到其他事件。但我很有意思,如果我试图覆盖这种方法,为什么我仍然会得到VirtualMachineError。

更新:如果MyButtonView延伸,我会收到同样的错误。

更新:

我发现了其他消息:

09-19 09:24:43.789: W/dalvikvm(17401): Method Lcom/videoclasse/android/widgets/SubjectButton;.layout overrides final Landroid/view/View;.layout
09-19 09:24:43.789: W/dalvikvm(17401): failed creating vtable
09-19 09:24:43.789: W/dalvikvm(17401): Link of class 'Lcom/videoclasse/android/widgets/SubjectButton;' failed

最后我在android 2.3.3中找到了。 View.layout已定义:

public final void layout(int l, int t, int r, int b) {
    boolean changed = setFrame(l, t, r, b);
    if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
        if (ViewDebug.TRACE_HIERARCHY) {
            ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
        }

        onLayout(changed, l, t, r, b);
        mPrivateFlags &= ~LAYOUT_REQUIRED;
    }
    mPrivateFlags &= ~FORCE_LAYOUT;
}
抱歉愚蠢的问题。

1 个答案:

答案 0 :(得分:2)

在android 2.3.3中。 View.layout被定义为最终版。但在android 4.0中,这个功能不再是最终的。我收到此错误的原因是因为我使用的是4.0.0构建器。

感谢@WarrenFaith在代码中创建了错误类型,非常感谢@JesusFreke给出的搜索更多信息的方向。