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();

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...