[英]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. 我正在尝试使用Firebase创建笔记记录器,但由于遇到此问题的标题而出现错误,因此我遇到了第一个障碍。
My XML file is: 我的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>
The java class in question is: 有问题的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();
}
}
}
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. 我已经确保没有重复的编辑文本名称,并且为了确定不再使用了不再需要的任何类和xml文件。
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. 只需删除include
标签中的一个ID即可。 While not shown in your post, I bet its top container is RelativeLayout
. 尽管您的帖子中未显示,但我敢打赌其顶部的容器是RelativeLayout
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.