简体   繁体   中英

Inconvertible types: cannot cast 'android.view.View' to 'android.widget.EditText'

I'm new in android studio. I'm simply adding EditText in address activity.

I get this error:

Inconvertible types: cannot cast android.view.View to android.widget.EditText on (EditText) findViewById(R.id.xmlid);

AddAddressActivity:

package com.onlinefire.e_shop.activities;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.onlinefire.e_shop.R;

import java.util.HashMap;
import java.util.Map;

public class AddAddressActivity extends AppCompatActivity {

EditText name;
EditText address;
EditText city;
EditText postalCode;
EditText phoneNumber;
Toolbar toolbar;
Button addAddressBtn;

FirebaseFirestore firestore;
FirebaseAuth auth;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_address);

    toolbar = findViewById(R.id.add_address_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    auth = FirebaseAuth.getInstance();
    firestore = FirebaseFirestore.getInstance();


    name = (EditText) findViewById(R.id.ad_name);
    address = (EditText) findViewById(R.id.ad_address);
    city = findViewById(R.id.ad_city);
    phoneNumber = findViewById(R.id.ad_phone);
    postalCode = (EditText) findViewById(R.id.ad_code);
    addAddressBtn = findViewById(R.id.ad_add_address);


    addAddressBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String userName = name.getText().toString();
            String userCity = city.getText().toString();
            String userAddress = address.getText().toString();
            String userCode = postalCode.getText().toString();
            String userNumber = phoneNumber.getText().toString();

            String final_address = "";

            if (!userName.isEmpty()) {
                final_address+=userName+", ";
            }

            if (!userCity.isEmpty()) {
                final_address+=userCity+", ";
            }

            if (!userAddress.isEmpty()) {
                final_address+=userAddress+", ";
            }

            if (!userCode.isEmpty()) {
                final_address+=userCode+", ";
            }

            if (!userNumber.isEmpty()) {
                final_address+=userNumber+", ";
            }
            if (!userName.isEmpty() && !userCity.isEmpty() && !userAddress.isEmpty() && !userCode.isEmpty() && !userNumber.isEmpty()){

                Map<String,String> map = new HashMap<>();
                map.put("userAddress",final_address);

                firestore.collection("CurrentUser").document(auth.getCurrentUser().getUid())
                        .collection("Address").add(map).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentReference> task) {
                        if (task.isSuccessful()){
                            Toast.makeText(AddAddressActivity.this, "Address Added", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(AddAddressActivity.this,AddressActivity.class));
                            finish();
                        }
                    }
                });

            }else {
                Toast.makeText(AddAddressActivity.this, "Kindly Fill All Field", Toast.LENGTH_SHORT).show();
              }
          }
      });

    }

}

activity_add_address:

  <?xml version="1.0" encoding="utf-8"?>
  <androidx.constraintlayout.widget.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.onlinefire.e_shop.activities.AddAddressActivity">


  <TextView
      android:id="@+id/textView8"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="56dp"
      android:layout_marginTop="24dp"
      android:text="Create Address"
      android:textSize="24sp"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/add_address_toolbar" />

  <com.google.android.material.textfield.TextInputLayout
      android:id="@+id/textInputLayout3"
      android:layout_width="300dp"
      android:layout_height="64dp"
      android:layout_marginTop="40dp"
      android:hint="Name"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.424"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/textView8">


    <EditText
        android:id="@+id/ad_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout4"

    android:layout_width="300dp"
    android:layout_height="64dp"
    android:layout_marginTop="10dp"
    android:hint="Address Lane"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.424"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout3">

    <EditText
        android:id="@+id/ad_address"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout5"

    android:layout_width="300dp"
    android:layout_height="64dp"
    android:layout_marginTop="10dp"
    android:hint="City"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.424"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout4">

    <EditText
        android:id="@+id/ad_city"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/textInputLayout6"

    android:layout_width="300dp"
    android:layout_height="64dp"
    android:layout_marginTop="10dp"
    android:hint="Postal Code"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.424"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout5">

    <EditText
        android:id="@+id/ad_code"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="300dp"
    android:layout_height="64dp"
    android:layout_marginTop="10dp"
    android:hint="Phone Number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.424"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textInputLayout6">

    <EditText
        android:id="@+id/ad_phone"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>

<Button
    android:id="@+id/ad_add_address"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp"
    android:backgroundTint="@color/blue"
    android:text="Add Address"
    android:textColor="@android:color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.504"
    app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Any of the similar question does not help me. Please help me.

You can resolve this by deleting R.java files in Android Studio project.

Sorry for answering my own Question.
This error is resolved by deleting R.java files in Android Studio project.

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