1

我試圖創建一個自定義編輯文字,結果被困在這裏了......錯誤通脹類(自定義視圖)

請參考下面

public class MainActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 


public static class MyEditText1 extends EditText{ 

    Paint mPaint; 




    public MyEditText1(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 

    } 

    public MyEditText1(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mPaint=new Paint(); 
     mPaint.setColor(Color.BLACK); 


    } 

    @Override 
    protected void onDraw(Canvas c){ 
     super.onDraw(c); 
     int height=getHeight(); 
     int width=getWidth(); 
     int linespace=10; 
    int count=height/linespace; 

     for(int i=0;i<count;i++){ 
      c.drawLine(0, i*linespace, width, i*linespace, mPaint); 
     } 


    } 



    } 


} 

MyEditText類我的代碼是內部類

,我指的這個觀點在我的XML作爲

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 



<com.example.customedittext.MainActivity.MyEditText1 

    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:ems="10" 
    android:inputType="textNoSuggestions"/> 


</RelativeLayout> 

但應用越來越力CLOS編輯後立即開始。但當我保持內部類在MyEditText.java(並引用它在XML中)一個單獨的文件它工作得很好......保持它作爲內部類的問題是什麼......儘管我已經作出我的內部類是靜態的?

+0

堆棧跟蹤丟失...... – WarrenFaith

回答

6

因爲你的觀點是一個內部類,你需要參考它從你的XML略有不同(注意$符號):

com.example.customedittext.MainActivity$MyEditText1 
+0

使得它作爲你說給我在XML文件中的下面的錯誤 ---「在此行找到多個註解: \t - 元素類型‘com.example.customedittext.MainActivity’必須跟任一屬性規格,‘>’ \t或」 /> 「。」 – amj

+1

感謝@wsanville的提示......我的身影d it out.actually 當視圖類是嵌套的,我們需要使用xml中的類屬性 這裏是鏈接..到[Use Custom component](http://developer.android.com/guide/) topics/ui/custom-components.html#modification) – amj

+0

@amj你應該選擇他的答案作爲正確的答案 - 他的答案只是幫助我處理我遇到的問題。 – Kirk