我尝试在填充表单中的每个字段后才启用按钮。为此,我使用了TextWatcher,但我一直收到错误。
我的contentView是activity_add_grade,而我的按钮和字段在我的fragment_add_grade xml中吗?我是新手,我不完全理解为什么这两个xml文件都是由ADT自动生成的。
代码:
public class AddClass extends ActionBarActivity {
int baseYear=2005;
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
DBAdapter db = new DBAdapter(this);
private EditText editText1;
private EditText editText2;
public TextWatcher watcher = new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
checkFieldsForEmptyValues();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_grade);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
editText1 = (EditText) findViewById(R.id.editTitle);
editText2 = (EditText) findViewById(R.id.editCredit);
// Program aborts as soon as I try to add the listener to editText1
editText1.addTextChangedListener(watcher);
editText2.addTextChangedListener(watcher);
}
我的LogCat:
03-30 18:23:44.963: E/AndroidRuntime(1961): FATAL EXCEPTION: main
03-30 18:23:44.963: E/AndroidRuntime(1961): Process: com.NsouthProductions.gradetrackerpro, PID: 1961
03-30 18:23:44.963: E/AndroidRuntime(1961): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.NsouthProductions.gradetrackerpro/com.NsouthProductions.gradetrackerpro.AddClass}: java.lang.NullPointerException
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.os.Handler.dispatchMessage(Handler.java:102)
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.os.Looper.loop(Looper.java:136)
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-30 18:23:44.963: E/AndroidRuntime(1961): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 18:23:44.963: E/AndroidRuntime(1961): at java.lang.reflect.Method.invoke(Method.java:515)
03-30 18:23:44.963: E/AndroidRuntime(1961): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-30 18:23:44.963: E/AndroidRuntime(1961): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-30 18:23:44.963: E/AndroidRuntime(1961): at dalvik.system.NativeStart.main(Native Method)
03-30 18:23:44.963: E/AndroidRuntime(1961): Caused by: java.lang.NullPointerException
03-30 18:23:44.963: E/AndroidRuntime(1961): at com.NsouthProductions.gradetrackerpro.AddClass.onCreate(AddClass.java:63)
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.app.Activity.performCreate(Activity.java:5231)
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-30 18:23:44.963: E/AndroidRuntime(1961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-30 18:23:44.963: E/AndroidRuntime(1961): ... 11 more
答案 0 :(得分:1)
您的activity_add_grade不包含带有id editTitle
的EditText答案 1 :(得分:1)
下面:
editText1 = (EditText) findViewById(R.id.editTitle);
看起来findViewById方法无法找到具有此ID的元素,因此返回null
。
这就是为什么以后会在此行引发 NullPointerException 的原因:
editText1.addTextChangedListener(watcher);
==>将ID属性editTitle
添加到布局activity_add_grade