TextView无法强制转换为EditText

时间:2015-08-03 18:21:06

标签: java android android-studio

第一次向本网站发帖提问,所以我可能会犯一些错误!

我是新手编程并在Android Studio中运行应用程序时遇到以下错误:

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

导致此问题的代码是

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

我试图删除R.java文件并清理项目但它没有用,任何帮助解决问题都将非常感谢:)

我可以添加可能是问题因素的任何其他文件或相关代码。

编辑:将代码从文本视图更改为编辑文本后,我收到了新错误。

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)

xml文件,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>

给出错误的文件,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);
    }
}

3 个答案:

答案 0 :(得分:3)

在你的xml中, 变化

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

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

希望这有帮助。

修改:

你没有在意图中加入'text'。 :)

尝试更改

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

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

答案 1 :(得分:0)

如果包含TextEdit的视图没有显示(换句话说,它没有显示在屏幕上),它将抛出一个空指针。包含此EditText的XML是否名为activity_note_editor?

答案 2 :(得分:0)

如果所有解决方案均无效。然后请检查您使用的是TextView还是纯文本。使用TextView并这样做可能是原因。