【问题标题】:Android: Custom TextView Inflate ExceptionAndroid:自定义 TextView 膨胀异常
【发布时间】:2013-04-29 06:07:43
【问题描述】:

我创建了一个自定义 TextView,我唯一做的就是在布局 xml 中放置了一个“实例”。 这会导致应用程序因“Inflate Exception”而崩溃:

这是完整的自定义类(实际上其中还没有任何“自定义”...):

 package com.example.TestApp;
...
public  class MyTextView extends TextView {

public MyTextView (Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);        }


@Override
public void onDraw(Canvas canvas) {

    super.onDraw(canvas);

}

}

完整的布局文件:

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

<com.example.TestApp.MyTextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, MyActivity"
    />
</LinearLayout>

日志是:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.TestApp/com.example.TestApp.MyActivity}: 
android.view.InflateException: Binary XML file line #8: Error inflating class 
com.example.TestApp.MyTextView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3691)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
    at dalvik.system.NativeStart.main(Native Method)

我已经阅读了很多关于这些膨胀异常的帖子,但这次真的是最简单的情况。 xml (com.example.TestApp.MyTextView) 中使用的类路径似乎是正确的 - Intellj IDEA 甚至建议它......

顺便说一句:最小目标 sdk 版本设置为 10。

【问题讨论】:

    标签: android custom-controls inflate-exception


    【解决方案1】:

    像这样添加构造函数

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      添加构造函数

      public MyTextView(Context context, AttributeSet attrs)
      

      【讨论】:

        猜你喜欢
        • 2011-06-29
        • 2012-05-25
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多