TextView无法强制转换为EditText [英] TextView cannot be cast to EditText

查看:1615
本文介绍了TextView无法强制转换为EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

造成此问题的代码是

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

我试图删除R.java文件并清理项目但它不起作用,任何帮助解决问题的人都会非常感激:)

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.

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

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)

xml文件,activity_note_editor:

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>

给出错误的文件,NoteEditorActivity:

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:

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


推荐答案

In你的xml,
更改

In your xml, change

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

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

希望这会有所帮助。

编辑:

您没有在意图中加入文字。 :)

You didn't put 'text' in the intent. :)

尝试更改

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

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

这篇关于TextView无法强制转换为EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆