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.

4
  • 5
    Instead of using TicTacButton you should use full package name your.package.TicTacButton in layout. Commented Apr 22, 2013 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? Commented Apr 22, 2013 at 20:20
  • Using the full package name in the XML file fixed it. Commented Apr 22, 2013 at 20:22
  • @harism you should turn your comment into an answer so the OP can mark it "accepted." Commented Apr 22, 2013 at 20:56

1 Answer 1

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" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.