S.I.
S.I.

Reputation: 3375

Cannot cast from EditText to Button

I've read a lot of posts about this but it didn't help. I've tried with clean and re-build the project but it didn't help either. I don't really understand what this error means. Can someone help to solve this error?

RegisterActivity.java

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import library.DatabaseHandler;
import library.UserFunctions;

public class RegisterActivity extends Activity {
Button buttonRegister;
Button buttonLinkToLogin;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";

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

    // Importing all assets like buttons, text fields
    inputFullName = (EditText) findViewById(R.id.registerName);
    inputEmail = (EditText) findViewById(R.id.registerEmail);
    inputPassword = (EditText) findViewById(R.id.registerPassword);
    buttonRegister = (Button) findViewById(R.id.buttonRegister);
    buttonLinkToLogin = (Button) findViewById(R.id.buttonLinkToLoginScreen);
    registerErrorMsg = (TextView) findViewById(R.id.register_error);

    // Register Button Click event
    buttonRegister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            ProgressDialog progressDialog = new ProgressDialog(RegisterActivity.this);
            progressDialog.setMessage("Registering...");
            RegisterTask registerTask = new RegisterTask(RegisterActivity.this, progressDialog);
            registerTask.execute();
        }
    });

    // Link to Login Screen
    buttonLinkToLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(i);
            // Close Registration View
            finish();
        }
    });
}

public void registerReport(Integer responseCode) {
    int duration = Toast.LENGTH_LONG;
    Context context = getApplicationContext();
    
    if (responseCode == 0) {
        Toast toast = Toast.makeText(context, "Register Error", duration);
        toast.show();
    }
    else {
        Toast toast = Toast.makeText(context, "Register Success", duration);
        toast.show();
        Intent i = new Intent(getApplicationContext(),
                DashboardActivity.class);
        startActivity(i);
        finish();
    }
    
    
    
}

public EditText findViewById(int registeremail) {
    // TODO Auto-generated method stub
    return null;
}
}

And the lines are :

buttonRegister = (Button) findViewById(R.id.buttonRegister);
buttonLinkToLogin = (Button) findViewById(R.id.buttonLinkToLoginScreen);

Register.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3b3b3b" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dip" >
    <!--  View Title Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="REGISTER"
        android:textSize="25dip"
        android:textStyle="bold" />
    <!--  Name Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Full Name" />
    <!--  Name TextField -->
    <EditText
        android:id="@+id/registerName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:singleLine="true"/>

    <!--  Email Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Email" />
    <!--  Email TextField -->
    <EditText
        android:id="@+id/registerEmail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:singleLine="true"/>

    <!--  Password Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:text="Password" />
    <!--  Password TextField -->
    <EditText
        android:id="@+id/registerPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:password="true" 
        android:singleLine="true"/>
    

    <!--  Error message -->
    <TextView android:id="@+id/register_error"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#e30000"
                android:padding="10dip"
                android:textStyle="bold"/>

    <!--  Login Button -->

    <Button
        android:id="@+id/buttonRegister"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:text="Register" />

    <!--  Link to Login Screen -->
    <Button
        android:id="@+id/buttonLinkToLoginScreen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dip"
        android:background="@null"
        android:text="Already registred. Login Me!"
        android:textColor="#21dbd4"
        android:textStyle="bold" />
</LinearLayout>

</ScrollView>

update:

Button buttonRegister;
Button buttonLinkToLogin;

I can see this in console output:

res\layout\register.xml:75: error: Error: No resource found that matches the given name (at 'id' with value '@id/buttonLinkToLoginScreen').
res\layout\register.xml:67: error: Error: No resource found that matches the given name (at 'id' with value '@id/buttonRegister').

Upvotes: 0

Views: 588

Answers (2)

Kamil Kłys
Kamil Kłys

Reputation: 2047

I suppose you declared your two buttons as TextFields, not Buttons. That is why it says that project has errors and cannot even be runned. If it runs and then throws errors, that it cannot cast, problem would be elswere.

I suppose you copy-pasted controls declarations and forgot to change their types.

UPDATE

Why are you overriding findViewById method? get rid of it. As you can see you are overriding default method which would return correct object. Your method returns wrong object (TextField) - that is why it says that it cannot cast from TextField to Button, and it is null if that was not enough.

Upvotes: 1

Faruk Yazici
Faruk Yazici

Reputation: 2404

Three probable solutions;

  • Try all lowercase letters in ID's in XML file
  • Restart Eclipse
  • No errors should remain on res directory, otherwise it cannot recognize ID's, so clear if any red marks

These often solve such kind of problems.

Upvotes: 0

Related Questions