【问题标题】:Implementing extended/custom View throws NoSuchMethod for constructor实现扩展/自定义视图为构造函数抛出 NoSuchMethod
【发布时间】:2012-07-27 01:09:33
【问题描述】:

我有一个自定义的 TextView,它实现了三个 View 构造函数(nb,这是我第一次尝试安卓应用):

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 指南,但我已经被这个问题难住了不止一个小时。

有人有什么想法吗?有没有办法进一步调试?

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

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

    tv = new DynamicGeometryTextView(this);

    layout.addView((View)tv);

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

【问题讨论】:

  • 只是为了缩小原因,您可以删除对外部类的引用并使您的 TextView 类静态吗?
  • @DheerajV.S.:是的,事实上这确实解决了问题:/

标签: android


【解决方案1】:

无法实例化非静态内部类without reference to an instance of the outer class

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

所以这可能是布局充气器未能充气您的类的原因。删除对外部类成员的引用后,使您的类成为静态的。

【讨论】:

  • !@$# 我想我会先尝试在没有 xml 布局的情况下实例化它,有很多耦合。
【解决方案2】:

变化:

public class DynamicGeometryTextView extends TextView {

收件人:

public static class DynamicGeometryTextView extends TextView {

为了正确引用它,它必须是static 内部类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-22
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    • 2018-05-28
    • 2015-09-11
    • 2018-08-24
    相关资源
    最近更新 更多