2012-07-25 56 views
7

我有一個自定義的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()在構造函數中設置。

+1

只是縮小的原因,你可以刪除引用到外部類,並使您的TextView類靜態? – 2012-07-25 17:35:18

+0

@ DheerajV.S .:是的,事實上,這確實解決了這個問題:/ – delicateLatticeworkFever 2012-07-25 17:41:03

回答

9

沒有辦法實例化非靜態內部類without reference to an instance of the outer class

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

所以這可能是佈局充氣器未能膨脹你的課堂的原因。在刪除對外部類成員的引用後,使您的類變爲靜態。

+0

!@ $#我想我會嘗試實例化它首先沒有XML佈局,有很多耦合。 – delicateLatticeworkFever 2012-07-25 17:46:18

9

變化:

public class DynamicGeometryTextView extends TextView { 

要:

public static class DynamicGeometryTextView extends TextView { 

爲了正確地引用,它必須是一個static內部類

相關問題