简体   繁体   中英

android.widget.RelativeLayout cannot be cast to android.widget.EditText

I'm attempting to create a note taker using Firebase and I'm falling at the first hurdle as I'm getting the error as per the title of this question.

My XML file is:

<?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>

The java class in question is:

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();
    }
}

}

I've made sure I don't have any duplicate edit text names and got rid of any classes and xml files that are no longer needed just to be sure.

I've cleaned and rebuilt I don't know how many times but can't get past this issue. Does anyone have any other ideas I can use/try?

Thanks

Here you have it twice!

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

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

Just remove the one id in your include tag. While not shown in your post, I bet its top container is RelativeLayout .

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