James B
James B

Reputation: 9605

Can't find class for extended view?

I'm extending the Button view to MyButton to add some bespoke methods, i.e.,

public class MyButton extends Button {

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

    // Bespoke methods etc.

}

and then in the layout xml

<com.package.mine.MyButton
        android:id = "@+id/button"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />    

This works - great! However, when I try and put the MyButton class as a static inner class in my activity the application keeps crashing, i.e., in the activity I define MyButton as

public static class MyButton extends Button {

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

    // Bespoke methods etc.

}

and I refer to it in the xml layout as

<com.package.mine.MyActivity.MyButton
        android:id = "@+id/button"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />

The package in the manifest file is defined as package="com.package.mine", and the logcat error is:

java.lang.ClassNotFoundException: Didn't find class "com.package.mine.MyActivity.MyButton" on path: DexPathList[[zip file "/data/app/com.package.mine-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.package.mine-1, /vendor/lib, /system/lib]]

Can't figure out where I'm going wrong. Any ideas? Thanks.

Upvotes: 1

Views: 1328

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

com.package.mine is your package name and MyButtonis your Custom button class name.

So you should have that class under the package name. What you are doing is wrong.

Put your custom button class in a separate file. Your package name is not com.package.mine.MyActivity. It is com.package.mine

You can do like this if you want an inner class

    public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyButton mb = new MyButton(this);
        setContentView(mb);
        mb.setText("hello");
    }
    public  class MyButton extends Button {

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

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

        @Override
        public void setText(CharSequence text, BufferType type) {
            // TODO Auto-generated method stub
            super.setText(text, type);
        }
    }
}

Upvotes: 3

Related Questions