2

I'm trying to create custom button class for my android app

public class TicTacButton extends Button 

I've set all the constructors inside the TicTacButton and created custom methods and properties. In my main activity, I've tried to initialize the Buttons as

TicTacButton btn = (TicTacButton) findViewById(R.id.button1);

I'm getting a

java.castClassException. android.widget.Button cannot be cast to com.example.tictactoetitan.TicTacButton

I tried changing my xml file as

<TicTacButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_toRightOf="@+id/button1" />

It didn't work.

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
Lordking
  • 1,413
  • 1
  • 13
  • 31
  • 5
    Instead of using ``TicTacButton`` you should use full package name ``your.package.TicTacButton`` in layout. – harism Apr 22 '13 at 20:19
  • 2
    You're also going to have to post the xml for button1. What you posted defined button2. How can we tell whether you defined button1 with a Button tag or a TicTacButton tag? – MarsAtomic Apr 22 '13 at 20:20
  • Using the full package name in the XML file fixed it. – Lordking Apr 22 '13 at 20:22
  • @harism you should turn your comment into an answer so the OP can mark it "accepted." – MarsAtomic Apr 22 '13 at 20:56

1 Answers1

4

Using the full package name in the XML file fixed it.

<com.example.tictactoetitan.TicTacButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_toRightOf="@+id/button1" />
Lordking
  • 1,413
  • 1
  • 13
  • 31