ImageButton无法强制转换为android.widget.Edittext

时间:2014-04-29 20:46:09

标签: java android

它是一个简单的登录屏幕,带有两个编辑文本,一个复选框和一个图像按钮...最近工作正常我将Button更改为imageButton,现在它给了我问题

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)

这是java

public class Login extends Activity实现了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>

3 个答案:

答案 0 :(得分:1)

也许您遇到了这个问题,因为您没有在视图中将Button控件更改为ImageButton(login.xml)。如果情况不是这样,你应该清理你的项目并重新构建它,R.java文件有时不会用新的更改自我刷新(这个问题有点奇怪,但有时会发生)。 / p>

只是为了记录...当这种问题发生并且你的代码没有错误时,它是因为你改变了控件的位置(例如...你的按钮在开头但过了一段时间,你把它移到布局的末尾)由于某种原因,项目没有实现这个改变,仍然认为你的Button是布局中的第一个控件。

很抱歉在回答中写下这个建议......我没有足够的积分在您的问题中发表评论。

答案 1 :(得分:0)

按钮在ImageButton扩展ImageView时扩展TextView。有点奇怪,但那个Android适合你。

我建议可能使用Button然后使用android:background =&#34; @ drawable / MyDrawable&#34;并看看这是否会达到你想要做的目的。

答案 2 :(得分:0)

您在课堂上使用了两次ImageButton:

ImageButton btnLogin;

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

如果你在其他地方使用ImageButton,请删除 ImageButton btnLogin;

其次,请注意您没有在onCreate()的任何位置注册ImageButton。

我的建议是,只需在onCreate()之前删除 ImageButton btnLogin; 并尝试一下。

更改此内容:

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

为:

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

现在应该正常工作。

相关问题