简体   繁体   中英

ImageButton cannot be cast to android.widget.Edittext

Its a simple login screen with two edit text one checkbox and a imagebutton ... it was working fine recently i have changed Button to imageButton and now it giving me problem

logcat

04-30 02:11:24.699: E/AndroidRuntime(9571): FATAL EXCEPTION: main
04-30 02:11:24.699: E/AndroidRuntime(9571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.markitberry/com.example.markitberry.Login}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.EditText
04-30 02:11:24.699: E/AndroidRuntime(9571):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-30 02:11:24.699: E/AndroidRuntime(9571):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-30 02:11:24.699: E/AndroidRuntime(9571):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-30 02:11:24.699: E/AndroidRuntime(9571):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)

here is java

public class Login extends Activity implements AnimationListener {

ImageButton btnLogin;
EditText inputEmail,inputPassword;
CheckBox loginRemember;

// Animation
Animation animBounce1,animBounce2,animBounce3,animBounce4;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

 // load the animation
    animBounce1 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out);
    animBounce2 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out_right);
    animBounce3 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out);
    animBounce4 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);


    // set animation listener
    animBounce1.setAnimationListener(this);
    animBounce2.setAnimationListener(this);
    animBounce3.setAnimationListener(this);
    animBounce4.setAnimationListener(this);

    inputEmail=(EditText)findViewById(R.id.etloginEmail);
    inputPassword=(EditText)findViewById(R.id.etloginPassword);
    loginRemember=(CheckBox)findViewById(R.id.cbRemember);


    ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            inputEmail.startAnimation(animBounce1);
            inputPassword.startAnimation(animBounce2);
            loginRemember.startAnimation(animBounce3);
        }
    });

}


 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.login_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }


 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Take appropriate action for each action item click
        switch (item.getItemId()) {
        case R.id.action_call:
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:9871952704"));
            startActivity(callIntent);

            // help action
            return true;
        case R.id.action_email:
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                    "mailto","markitberry@gmail.com", null));
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        intent.putExtra(Intent.EXTRA_TEXT, "message");
        startActivity(Intent.createChooser(intent, "Choose an Email client :"));

        case R.id.action_locate:
            Intent i = new Intent(Login.this, Locate.class);
            startActivity(i);
        default:
            return super.onOptionsItemSelected(item);
        }
    }


@Override
public void onAnimationEnd(Animation animation) {
    // TODO Auto-generated method stub
    if (animation == animBounce3) {
        Intent it=new Intent(Login.this,Home.class);
        startActivity(it);
    }

}


@Override
public void onAnimationRepeat(Animation arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onAnimationStart(Animation arg0) {
    // TODO Auto-generated method stub

}

}

login.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_bck"


    android:gravity="center">
    <include 
            layout="@layout/login_cover" 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" />

</RelativeLayout>

login_cover.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:background="@drawable/login_gradient_bck"
    android:orientation="vertical" >



    <EditText
        android:id="@+id/etloginPassword"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_above="@+id/cbRemember"
        android:layout_alignLeft="@+id/etloginEmail"
        android:layout_alignRight="@+id/etloginEmail"
        android:layout_marginBottom="26dp"
        android:background="#BFFFFFFF"
        android:ems="10"
        android:gravity="center"
        android:hint="Enter Password"
        android:inputType="textPassword"
        android:textColor="#FFFFFF" />

    <EditText
        android:id="@+id/etloginEmail"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_above="@+id/etloginPassword"
        android:layout_centerHorizontal="true"
        android:layout_margin="30dp"
        android:layout_marginBottom="36dp"
        android:background="#BFFFFFFF"
        android:ems="10"
        android:gravity="center"
        android:hint="Enter Email"
        android:inputType="textEmailAddress"
        android:textColor="#FFFFFF" />



    <CheckBox
        android:id="@+id/cbRemember"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_above="@+id/btnLogin"
        android:layout_alignLeft="@+id/etloginPassword"
        android:layout_alignRight="@+id/etloginPassword"
        android:layout_marginBottom="62dp"
        android:text="Remember Me"
        android:textColor="#E6E6E6" />


    <ImageButton
        android:id="@+id/btnLogin"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:background="@drawable/button"
        android:src="@drawable/login_icon_2" />
</RelativeLayout>

Maybe you're having this problem because you didn't change your Button control to ImageButton in your view (login.xml). If that's not the case, you should clean your project and build it again, the R.java file sometimes does not refresh it self with the new changes (this issue is kind of weird, but sometimes it happens).

Just for the record... when this kind of problem happens and you have your code with no errors, it's because you changed the position of your control (For example... your Button was at the beginning but after a while you moved it to the end of the layout) for some reason the project doesn't realize this change and still thinks your Button is the first control in the layout.

Sorry to write this suggestion in an answer... I don't have enough points to comment in your question.

Button extends TextView while ImageButton extends ImageView. Kind of wierd, but that's Android for you.

I would suggest possibly using Button then using android:background="@drawable/MyDrawable" and see if that will achieve what you are trying to do.

You are using the ImageButton two times in you class:

ImageButton btnLogin;

And;

ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);

Please remove the ImageButton btnLogin; if you are using the ImageButton somewhere else..

Secondly, note that you are not registering the ImageButton anywhere in the onCreate()..

My suggestion, just remove the ImageButton btnLogin; before the onCreate() and try it.

Or change this :

ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);

to:

btnLogin =(ImageButton) findViewById(R.id.btnLogin);

Should be working now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM