Sumit
Sumit

Reputation: 11

ClassCastException in findViewById while using custom view

I am using custom TextView. But when I am retrieving it using find view by Id I get ClassCastException. I have using it in XML file like this:

<View class="com.android.smsapp.MsgTextView" android:id="@+id/text"

And using it in java file like this.

MsgTextView text = (MsgTextView) row.findViewById(R.id.text);

What am I doing wrong?


@Pavandroid

I have included it in correct file

    package com.android.smsapp;

    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.TextView;

    public class MsgTextView extends TextView {
    private String sender;

    MsgTextView(Context c){
        super(c);
    }

    MsgTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    public void setSender(String s) {
        sender = s;
    }

    public String getSender() {
        return (sender);
    }
}`

Also When I checked properly, LogCat is showing one more line

03-19 14:30:41.476: E/AndroidRuntime(24089): Caused by: java.lang.NoSuchMethodException: MsgTextView(Context,AttributeSet).. But I have defined this constructor. 

Upvotes: 0

Views: 708

Answers (1)

Pavandroid
Pavandroid

Reputation: 1586

instead of the above code use the below code.

<com.android.smsapp.MsgTextView 
          android:id="@+id/text"
          additional parameters here....>
</com.android.smsapp.MsgTextView>

Upvotes: 1

Related Questions