微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 实现扩展/自定义View为构造函数抛出NoSuchMethod

我有一个自定义的TextView,它实现了三个View构造函数(nb,这是我在 Android应用程序中的第一次尝试):
public class DynamicGeometryTextView extends TextView {

    public DynamicGeometryTextView (Context con) { super(con); }

    public DynamicGeometryTextView (Context con,AttributeSet attrs) {
        super(con,attrs);
    }

    public DynamicGeometryTextView (Context con,AttributeSet attrs,int style) {
        super(con,attrs,style); 
    }

这是一个非静态内部类,因为它需要从外部类访问实例数据.它出现在.xml布局中:

<view class="cogdis.chalkboard.displayText$DynamicGeometryTextView"
    android:id="@+id/chalkboard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

一切都编译和安装很好,但在运行时:

Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class cogdis.chalkboard.displayText$DynamicGeometryTextView
    at android.view.LayoutInflater.createView(LayoutInflater.java:596)                                                                         
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)                                                                  
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)                                                                           
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)                                                                            
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)                                                                            
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)                                                                            
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)                                                       
    at android.app.Activity.setContentView(Activity.java:1867)                                                                                 
    at cogdis.chalkboard.displayText.onCreate(displayText.java:26)                                                                             
    at android.app.Activity.performCreate(Activity.java:5008)                                                                                  
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)                                                             
    at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2023)                                                              
    ... 11 more                                                                                                                                
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context,interface android.util.AttributeSet]                       
    at java.lang.class.getConstructorOrMethod(Class.java:460)                                                                                  
    at java.lang.class.getConstructor(Class.java:431)                                                                                          
    at android.view.LayoutInflater.createView(LayoutInflater.java:561)                                                                         
    ... 22 more

在我看来,这意味着它无法找到构造函数的(Context,AttributeSet)版本……但它存在.我看了一些其他的SO帖子,比如Android Custom View Constructor,这一切都指向了相同的结论(我的眼睛),并反复阅读自定义组件的API指南,但我已经被这个问题困扰了一个多小时.

有人有主意吗?有没有办法进一步调试?

对于像我这样的新手来说,如果你的自定义视图在XML布局中被引用,那么非静态内部类是不行的,但是如果你以编程方式创建它,它可以工作,例如:

LayoutInflater lif = getLayoutInflater();
    ViewGroup layout = (ViewGroup)lif.inflate(R.layout.board,null);

    tv = new DynamicGeometryTextView(this);

    layout.addView((View)tv);

在这种情况下,您只需要匹配实际使用的构造函数.可以通过从View继承的setLayoutParams()在构造函数中设置布局参数(WRAP_CONTENT等).

解决方法

无法实例化非静态内部类 without reference to an instance of the outer class.
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

所以这可能是布局填充无法给你的课堂膨胀的原因.删除对外部类成员的引用后,使您的类保持静态.

原文地址:https://www.jb51.cc/android/316321.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐