簡體   English   中英

android.widget.RelativeLayout無法轉換為android.widget.EditText

[英]android.widget.RelativeLayout cannot be cast to android.widget.EditText

我正在嘗試使用Firebase創建筆記記錄器,但由於遇到此問題的標題而出現錯誤,因此我遇到了第一個障礙。

我的XML文件是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.example.android.frapp.NoteTakerActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:id="@+id/appBarLayout2">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />

</android.support.design.widget.AppBarLayout>

<include
    android:id="@+id/noteTitleTxt"
    layout="@layout/content_note_taker" />


<EditText
        android:id="@+id/noteTitleTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/appBarLayout2"
        android:ems="10"
        android:hint="Enter Title"
        android:inputType="text" />

    <Spinner
        android:id="@+id/spinnerNoteType"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/noteTitleTxt"
        android:layout_marginTop="13dp"
        android:entries="@array/type" />

    <Button
        android:id="@+id/addNoteBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinnerNoteType"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="Add Note" />


</RelativeLayout>

有問題的Java類是:

package com.example.android.frapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class NoteTakerActivity extends AppCompatActivity {

EditText editNoteTitle; // It's this causing the issue
Button addButton;
Spinner spinnerType;

DatabaseReference databaseNotes;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note_taker);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    databaseNotes = FirebaseDatabase.getInstance().getReference("notes");

    editNoteTitle = (EditText) findViewById(R.id.noteTitleTxt);
    addButton = (Button) findViewById(R.id.addNoteBtn);
    spinnerType = (Spinner) findViewById(R.id.spinnerNoteType);

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNote();
        }
    });

}

private void addNote() {
    String title = editNoteTitle.getText().toString().trim();
    String type = spinnerType.getSelectedItem().toString();

    if (!TextUtils.isEmpty(title)) {

        String id = databaseNotes.push().getKey(); // id being created is unique every time

        Notes notes = new Notes(id, title, type);

        databaseNotes.child(id).setValue(notes); // to send data to database

        Toast.makeText(this, "Title added", Toast.LENGTH_SHORT).show();

    } else {
        Toast.makeText(this, "You must give the note a title", Toast.LENGTH_SHORT).show();
    }
}

}

我已經確保沒有重復的編輯文本名稱,並且為了確定不再使用了不再需要的任何類和xml文件。

我已經清理並重建了多少次,但無法解決這個問題。 有人有我可以使用/嘗試的其他想法嗎?

謝謝

在這里,你有兩次!

<include
    android:id="@+id/noteTitleTxt"  // <-----------------
    layout="@layout/content_note_taker" />

<EditText
    android:id="@+id/noteTitleTxt"  // <-----------------

只需刪除include標簽中的一個ID即可。 盡管您的帖子中未顯示,但我敢打賭其頂部的容器是RelativeLayout

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM