gpt4 book ai didi

java - TextChangedListener 在我身上崩溃了

转载 作者:行者123 更新时间:2023-12-02 06:00:05 25 4
gpt4 key购买 nike

我尝试仅在填充表单中的每个字段后启用按钮。为此,我使用了 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

最佳答案

您的activity_add_grade不包含id为editTitle的EditText

关于java - TextChangedListener 在我身上崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22750738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com