错误膨胀类/视图

时间:2014-06-24 17:13:16

标签: android android-view android-inflate

我知道还有许多其他问题要求提出错误信息,但是没有一个答案对我有帮助。我在类记事本中有一个名为DrawableView的自定义视图。

public class Notepad extends Activity {
    ...
    public class DrawableView extends View {
        public DrawableView(Context c) {
            super(c);
            setup();
        }

        public DrawableView(Context c, AttributeSet as) {
            super(c,as);
            setup();
        }

        public DrawableView(Context c, AttributeSet as, int ds) {
            super(c,as, ds);
            setup();
        }
    }
}

然后我的xml看起来像

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <view class="com.acuitus.main.Notepad$DrawableView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    </view>
</LinearLayout>

并导致错误

06-24 10:05:44.338: E/AndroidRuntime(16745): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acuitus.testlayouts/com.acuitus.main.Notepad}: android.view.InflateException: Binary XML file line #7: Error inflating class com.acuitus.main.Notepad$DrawableView
...
06-24 10:05:44.338: E/AndroidRuntime(16745): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.acuitus.main.Notepad$DrawableView
...
06-24 10:05:44.338: E/AndroidRuntime(16745): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

如果您真的需要,可以使用完整的错误堆栈

06-24 10:05:44.338: E/AndroidRuntime(16745): FATAL EXCEPTION: main
06-24 10:05:44.338: E/AndroidRuntime(16745): Process: com.acuitus.testlayouts, PID: 16745
06-24 10:05:44.338: E/AndroidRuntime(16745): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.acuitus.testlayouts/com.acuitus.main.Notepad}: android.view.InflateException: Binary XML file line #7: Error inflating class com.acuitus.main.Notepad$DrawableView
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.os.Looper.loop(Looper.java:136)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.app.ActivityThread.main(ActivityThread.java:5102)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at java.lang.reflect.Method.invoke(Method.java:515)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at dalvik.system.NativeStart.main(Native Method)
06-24 10:05:44.338: E/AndroidRuntime(16745): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.acuitus.main.Notepad$DrawableView
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.view.LayoutInflater.createView(LayoutInflater.java:603)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.app.Activity.setContentView(Activity.java:1937)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at com.acuitus.main.Notepad.onCreate(Notepad.java:36)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.app.Activity.performCreate(Activity.java:5248)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
06-24 10:05:44.338: E/AndroidRuntime(16745):    ... 11 more
06-24 10:05:44.338: E/AndroidRuntime(16745): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
06-24 10:05:44.338: E/AndroidRuntime(16745):    at java.lang.Class.getConstructorOrMethod(Class.java:472)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at java.lang.Class.getConstructor(Class.java:446)
06-24 10:05:44.338: E/AndroidRuntime(16745):    at android.view.LayoutInflater.createView(LayoutInflater.java:568)
06-24 10:05:44.338: E/AndroidRuntime(16745):    ... 22 more

我很困惑,因为我明确表示我没有为AttributeSet提供方法。有人能帮忙吗?

1 个答案:

答案 0 :(得分:0)

您应该将View类声明为静态或将其设置为另一个文件......甚至更好。