Skip to content
Advertisement

TextView cannot be cast to EditText

First time posting a question to this site so I might make some mistakes!

I’m new to programming and getting the following error when running an application in Android Studio:

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

The code that’s causing this issue is

EditText et = (EditText) findViewById(R.id.noteText);

I’ve tried to delete the R.java files and clean the project but it didn’t work, any help solving the problem would be greatly appreciated 🙂

I can add any other files or relevant code that might be a factor in the problem.

Edit: After changing the code from text view to edit text I’m getting a new error.

Logcat:

08-03 20:08:04.413  29662-29662/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.toshiba.notetakingapp/com.example.toshiba.notetakingapp.NoteEditorActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
            at android.app.ActivityThread.access$700(ActivityThread.java:134)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4867)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.toshiba.notetakingapp.NoteEditorActivity.onCreate(NoteEditorActivity.java:33)
            at android.app.Activity.performCreate(Activity.java:5047)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
            at android.app.ActivityThread.access$700(ActivityThread.java:134)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4867)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
            at dalvik.system.NativeStart.main(Native Method)

The xml file, activity_note_editor:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="New Text"
        android:id="@+id/noteText"
        android:singleLine="false"
        android:gravity="top"
        android:inputType="textMultiLine"
        />
</RelativeLayout>

The file that’s giving the error, NoteEditorActivity:

public class NoteEditorActivity extends Activity {

        private NoteItem note;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_note_editor);




            Intent intent = this.getIntent();
            note = new NoteItem();
            note.setKey(intent.getStringExtra("key"));
            note.setText(intent.getStringExtra("text"));

            EditText et = (EditText) findViewById(R.id.noteText);
            et.setText(note.getText());
            et.setSelection(note.getText().length());
        }
    }

Mainactivity.java:

package com.example.toshiba.notetakingapp;

import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import data.NoteItem;
import data.NotesDataSource;

import java.util.List;

public class MainActivity extends ListActivity {

    private NotesDataSource datasource;

    List<NoteItem> notesList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datasource = new NotesDataSource(this);

        refreshDisplay();

    }

    private void refreshDisplay() {
        notesList=datasource.findAll();
        ArrayAdapter<NoteItem> adapter =
                new ArrayAdapter<NoteItem>(this,R.layout.list_item_layout,notesList);
        setListAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_create) {
            createNote();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void createNote() {
        NoteItem note = NoteItem.getNew();
        Intent intent = new Intent(this,NoteEditorActivity.class);
        intent.putExtra("key",note.getKey());
        intent.putExtra("key",note.getText());
        startActivityForResult(intent,1001);
    }
}

Advertisement

Answer

In your xml, change

<TextView
android:id="@+id/noteText"
.../>

to

<EditText
android:id="@+id/noteText"
.../>

Hope this helps.

Edit:

You didn’t put ‘text’ in the intent. 🙂

Try changing

intent.putExtra("key",note.getKey());
intent.putExtra("key",note.getText());

to

intent.putExtra("key",note.getKey());
intent.putExtra("text",note.getText());
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement