3

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)

有任何想法吗?

编辑:我以其他方式解决了我的问题。只是将可绘制生成代码移至其他事件。但我很有趣,如果我尝试覆盖这个方法,为什么我仍然会得到 VirtualMachineError。

更新:如果我MyButton从 扩展View,我得到同样的错误。

更新:

我发现了其他消息:

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;
}

对不起愚蠢的问题。

4

1 回答 1

2

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

感谢@WarrenFaith 在代码中发现的错误输入,非常感谢@JesusFreke 提供了搜索更多信息的方向。

于 2012-09-19T06:44:55.060 回答