【问题标题】:Unexpected implicit cast to String: layout tag was EditText error意外隐式转换为字符串:布局标记是 EditText 错误
【发布时间】:2019-07-13 08:55:03
【问题描述】:

我收到此错误。

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

项目中没有其他错误。

坚持这一点。

主活动

 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

【问题讨论】:

    标签: java android android-studio android-layout androidx


    【解决方案1】:

    您好,欢迎使用 Stack Overflow

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

    name = findViewById(R.id.uName);

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

    String name, email;
    

    与

    EditText name, email;
    

    【讨论】:

    • 我的愚蠢错误:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多