我收到here
的提示似乎有些人使用了相同的方法,没有人遇到崩溃。我有这样的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
在我输入第二封信之后。有人说setText
中的afterTextChanged
会给出无休止的迭代,所以如果希望它可以阻止这种无穷无尽的话,我会放一个。不知道接下来该做什么。
谢谢。
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>
我刚注意到textview的地方是“假”?
error from logcat right after the first input (of edittext):
06-04 09:04:51.249: E/AndroidRuntime(10410): FATAL EXCEPTION: main
06-04 09:04:51.249: E/AndroidRuntime(10410): java.lang.NullPointerException
06-04 09:04:51.249: E/AndroidRuntime(10410): at com.example.instantinput.MainActivity$1.afterTextChanged(MainActivity.java:26)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.widget.TextView.sendAfterTextChanged(TextView.java:6568)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:6757)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.text.SpannableStringBuilder.sendTextHasChanged(SpannableStringBuilder.java:897)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:353)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:654)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:415)
06-04 09:04:51.249: E/AndroidRuntime(10410): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:328)
06-04 09:04:51.249: E/AndroidRuntime(10410): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:79)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.os.Looper.loop(Looper.java:123)
06-04 09:04:51.249: E/AndroidRuntime(10410): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-04 09:04:51.249: E/AndroidRuntime(10410): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 09:04:51.249: E/AndroidRuntime(10410): at java.lang.reflect.Method.invoke(Method.java:507)
06-04 09:04:51.249: E/AndroidRuntime(10410): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-04 09:04:51.249: E/AndroidRuntime(10410): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-04 09:04:51.249: E/AndroidRuntime(10410): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
看起来你的崩溃就在这条线上:
m_tv.setText(m_et.getText().toString());
必须是m_tv
null
。 (m_et
是附加了TextWatcher的视图,因此在第二个按键上不能是null
。)
因此,请检查您的activity_main.xml
布局文件,以确保正确定义了R.id.tv
视图。如果它在那里,请做一个干净的项目构建,以防有些东西正在收集过时的数据。