我有一个自定义的TextView它实现了三个View构造函数(NB,这是我在一个Android应用程序首次尝试):实现扩展/自定义视图抛出NoSuchMethod用于构造
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
为了我的眼睛,这意味着它无法找到(上下文,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);
在这种情况下,您只需要匹配您实际使用的构造函数。布局参数(WRAP_CONTENT等)可以通过从View继承的setLayoutParams()
在构造函数中设置。
只是缩小的原因,你可以删除引用到外部类,并使您的TextView类静态? – 2012-07-25 17:35:18
@ DheerajV.S .:是的,事实上,这确实解决了这个问题:/ – delicateLatticeworkFever 2012-07-25 17:41:03