单击addSubject按钮时出现错误,导致应用程序崩溃。 我一直试图找到一个解决方案,似乎找不到有用的东西。
任何帮助都将不胜感激。
代码:
public class Setup extends Activity {
EditText subjectTextField;
dataBaseTools2 dbTools = new dataBaseTools2(this);
String BLANK = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
final Button EditTextSubjectAdd = (Button) findViewById(R.id.buttonAddSub);
EditTextSubjectAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap<String, String> querySubjectsMap = new HashMap<String, String>();
subjectTextField = (EditText) findViewById(R.id.subjectField);
querySubjectsMap.put("subject", subjectTextField.getText()
.toString());
dbTools.insertSubject(querySubjectsMap);
subjectTextField.setText(BLANK);
// TODO Auto-generated method stub
}
});
final Button finishButton = (Button) findViewById(R.id.finishButton);
finishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.setup, menu);
return true;
}
}`
logcat的:
11-18 16:50:27.263: E/AndroidRuntime(1410): FATAL EXCEPTION: main
11-18 16:50:27.263: E/AndroidRuntime(1410): java.lang.NullPointerException
11-18 16:50:27.263: E/AndroidRuntime(1410): at com.compfour.homework.Setup$1.onClick(Setup.java:30)
11-18 16:50:27.263: E/AndroidRuntime(1410): at android.view.View.performClick(View.java:4240)
11-18 16:50:27.263: E/AndroidRuntime(1410): at android.view.View$PerformClick.run(View.java:17721)
11-18 16:50:27.263: E/AndroidRuntime(1410): at android.os.Handler.handleCallback(Handler.java:730)
11-18 16:50:27.263: E/AndroidRuntime(1410): at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 16:50:27.263: E/AndroidRuntime(1410): at android.os.Looper.loop(Looper.java:137)
11-18 16:50:27.263: E/AndroidRuntime(1410): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-18 16:50:27.263: E/AndroidRuntime(1410): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 16:50:27.263: E/AndroidRuntime(1410): at java.lang.reflect.Method.invoke(Method.java:525)
11-18 16:50:27.263: E/AndroidRuntime(1410): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-18 16:50:27.263: E/AndroidRuntime(1410): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-18 16:50:27.263: E/AndroidRuntime(1410): at dalvik.system.NativeStart.main(Native Method)
任何帮助将不胜感激。
答案 0 :(得分:0)
将subjectTextField = (EditText) findViewById(R.id.subjectField);
放在您活动的onCreate()方法中。