java - 意外隐式转换为字符串 : layout tag was EditText error

标签 java android android-studio android-layout androidx

我收到此错误。

无法解决此错误,请帮忙。

项目中没有其他错误。

卡在这了。

主要 Activity

 public class MainActivity extends AppCompatActivity {

    String name, email;
    FirebaseAuth mAuth;
    Button submit;
    DatabaseReference rootRef,demoRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
        getSupportActionBar().hide(); // hide the title bar
        setContentView(R.layout.activity_main);

        name =  findViewById(R.id.uName);
        email = findViewById(R.id.uEmail);

我正在使用 constarint 布局。我已将所有内容设置正确。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center">

    <EditText
        android:id="@+id/uName"
        android:layout_width="0dp"
        android:layout_height="58dp"
        android:layout_marginStart="48dp"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="48dp"
        android:layout_marginRight="48dp"
        android:background="@drawable/border"
        android:hint="@string/name"
        android:paddingLeft="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

错误

Unexpected implicit cast to String: layout tag was EditText less... (Ctrl+F1) 
Inspection info:Keeps track of the view types associated with ids and if it finds a usage of the id in the Java code it ensures that it is treated as the same type.  Issue id: WrongViewCast
 Type parameter T has incompatible upper bounds: View and String

最佳答案

您好,欢迎来到 Stack Overflow

实际上,您正在尝试从布局中获取 editText 的引用。

name = findViewById(R.id.uName);

变量name不是EditText类型变量。它是String类型变量。这就是为什么您的应用程序显示转换异常。 使用 EditText 代替 String 并替换

String name, email;

EditText name, email;

关于java - 意外隐式转换为字符串 : layout tag was EditText error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57017622/

相关文章:

java - 如何将日期转换为json

java - 无法解析/找到 OAuth2ServerConfigurerAdapter

java - Java中是否存在用于满足接口(interface)的空方法的习语?

java - HttpGet 无法识别网址

android - 如何解决 AAB 版本中的 Google Play 64 位错误?

javascript - Android Studio Zxing Scanner,如果QR码的结果是URL,如何重定向到网页?

Java不可靠的字符串RSA加密/解密

android - 是否有关于如何使用 Naver 的 Line URL SCHEME 的示例或教程?

android - 允许多个应用通过库访问一个 Firebase 项目

android - 如何在主 Activity 中隐藏标题栏 : I'm Using Android Studio