java - ImageButton 无法转换为 android.widget.Edittext

标签 java android

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

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)类Login扩展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>

最佳答案

也许您遇到此问题是因为您没有在 View (login.xml) 中将 Button 控件更改为 ImageButton。如果不是这种情况,您应该清理您的项目并再次构建它,R.java 文件有时不会随着新的更改而自行刷新(这个问题有点奇怪,但有时会发生)。

仅供记录...当发生这种问题并且您的代码没有错误时,这是​​因为您更改了控件的位置(例如...您的 Button 位于开始但一段时间后您将其移至布局的末尾)由于某种原因,项目没有意识到此更改,并且仍然认为您的 Button 是布局中的第一个控件。

很抱歉在答案中写下这个建议...我没有足够的观点来评论您的问题。

关于java - ImageButton 无法转换为 android.widget.Edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23374917/

相关文章:

android - 如何停止在 Android 上以编程方式播放通知声音

java - 如何在 RecyclerView 中使用字符串?

android - 如何在Android Kotlin上增加动态布局?

android - Unresolved reference : Matchers

java - 使用 Java 编写客户端通过 UDP 连接向服务器发送 HTTP 请求消息

android - React Native 构建在 android 中失败,@react-native-community/cli-platform-android/native_modules.gradle' 行 : 130

android - DLL 加载失败 : %1 is not a valid Win32 application - Appcelerator

java - log4j 没有记录到文件

java - FTPSClient,为远程和本地提供什么值

java - Netty代理示例中ctx.write(Unpooled.EMPTY_BUFFER)的用途是什么