2011-12-08 3 views
0

私はサブクラスButtonにしようとしましたが、私はプロジェクトを起動するときに多くのエラーがあります。あなたは見て、これを修正する方法を教えていただけますか?Androidのサブクラスボタン:多くのエラー

package my.project.name; 

import android.app.Activity; 
import android.os.Bundle; 

public class MyProjectActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

とxml::

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

    <MyButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/myb" 
     android:tag="tag" 
     /> 

</LinearLayout> 

EDIT:

package my.project.name; 

import android[...] 

public class MyButton extends Button { 

    public MyButton(){ 
     super(null); 
    } 

    public MyButton(Context context){ 
     super(context); 
    } 

    public MyButton(Context context, AttributeSet attrs){ 
     super(context, attrs); 
    } 

    public MyButton(Context context, AttributeSet attrs, int defStyle){ 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onDraw(Canvas canvasObject) { 
     super.onDraw(canvasObject); 
     int x = 100; 
     int y = 100; 
     int width = 80; 
     int height = 200; 
     Paint thePaint = new Paint(); 
     thePaint.setColor(Color.WHITE); 
     RectF rectangle1 = new RectF(x,y,x+width,y+height); 
     canvasObject.drawRoundRect(rectangle1, 10.0f, 10.0f, thePaint); 
    } 

} 

メインクラスを(私は多分50のエラーが来るんだ)実際に、矩形は表示されません。私はエラーがないので、サブクラスは動作しますが、 "@ null"の背景でさえ、四角形は表示されません。

どうもありがとう

+2

うーん、何のエラー?それは重要なディテールのようなものです。あなたがXMLであなたのボタンの完全修飾名を使用しなければならないことがわかりました。 (例: 'my.project.name.MyButton') – kcoppock

+1

i!= I ......... – Shoban

+1

受け取ったエラーのいくつかを追加してください。また、XMLに完全修飾名前空間を記述する必要があります。 のように –

答えて

3

は、あなたのボタンのサブクラスである場合は、XMLレイアウトの要素が動作しないための完全修飾名を使用して、完全修飾名前空間に

<namespace.from.button.MyButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/myb" 
    android:tag="tag" 
    /> 
+0

ラファエル、コンストラクタについては、すべてのコンストラクタをサブクラスに書き直す必要がありますか?またはこれは何らかのエラーを引き起こす可能性がありますか? – Paul

+0

が必要コンストラクタはButtonの使い方に依存します:MyButton(Context)は通常Java内で呼び出されます。あなたはButtonからXMLを膨張させているので、Button(Context、AttributeSet)のみが必要です。 –

+0

ありがとう、もう一度申し訳ありませんが、私はあまりにも多くの質問をしたくありませんが、投稿を編集しました:サブクラスは起動時にエラーはありませんが、 "四角形"は画面に表示されません... – Paul

0

<MyButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/myb" 
    android:tag="tag" 
    /> 

の名前を変更します内部クラス。このような場合は、クラス属性を追加し、要素名として汎用ビューを使用する必要があります。

<view 
    class="my.project.name.MyProjectActivity$MyButton" 
    ... 

http://developer.android.com/guide/topics/ui/custom-components.html

関連する問題