Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用EditText和TextView即时显示文本会导致崩溃_Java_Android - Fatal编程技术网

Java 使用EditText和TextView即时显示文本会导致崩溃

Java 使用EditText和TextView即时显示文本会导致崩溃,java,android,Java,Android,我从他那里得到了暗示 而且似乎有些人也用了同样的方法,没有人撞上飞机。我有如下java文件: package com.example.instantinput; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.wid

我从他那里得到了暗示

而且似乎有些人也用了同样的方法,没有人撞上飞机。我有如下java文件:

package com.example.instantinput;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private EditText m_et;
    private TextView m_tv;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        m_tv = (TextView) findViewById(R.id.tv);
        m_et = (EditText) findViewById(R.id.et);
        m_et.setHint("please enter name");
        m_et.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {

                m_tv.setText(m_et.getText().toString());

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }
        });
    }
}
但它在运行时崩溃,并给出

E/AndroidRuntime(6015): java.lang.NullPointerException
就在我输入第二个字母之后。有人说
posterextchanged
中的
setText
将进行无休止的迭代,因此我提出了一个“如果有希望,它可以停止这种无休止的迭代”。不知道下一步该怎么办。 谢谢 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <EditText
        android:id="@+id/et" 
        android:inputType="text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView        
        android:text="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        />
</LinearLayout>

看起来您的崩溃发生在这条线上:

m_tv.setText(m_et.getText().toString());

它必须是
m_tv
,即
null
。(
m_et
是连接了TextWatcher的视图,因此在第二次按键时它不能为
null


因此,请检查
activity\u main.xml
layout文件,以确保正确定义了
R.id.tv
视图。如果它在那里,请对项目进行干净的构建,以防有什么东西收集到过时的数据。

它必须是
m\u tv
,即
null
m_et
是连接了TextWatcher的视图,因此在第二次按键时它不能为
null
。@GrahamBorland如果我删除if(…),它将在任何输入后崩溃,而其他一些人似乎使用相同的代码,但没有看到崩溃。知道为什么吗?也许清理一下你的项目?有时,如果进行了更改,R.jave类可能会失去同步。我刚刚注意到,在应用程序中,编辑文本的正下方,在textview的位置,它是“false”@Tiina你能发布所有错误吗?嗨,照你说的做了。仍然不知道为什么它仍然崩溃。我也粘贴了activity_main.xml。谢谢
m_tv.setText(m_et.getText().toString());