我认为在尝试添加数据时app会出现制动并显示错误。
public class Employee extends AppCompatActivity {
EmployeeDatabase dbEmployee;
EditText editId, editIme, editPrezime, editCena ;
Button btnDodaj, btnPregledaj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee);
dbEmployee = new EmployeeDatabase(this);
editId = (EditText)findViewById(R.id.editId);
editIme = (EditText)findViewById(R.id.editUserName);
editPrezime = (EditText)findViewById(R.id.editPrezime);
editCena = (EditText)findViewById(R.id.editCena);
addData();
viewAll();
}
public void addData(){
btnDodaj.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean isInserted = dbEmployee.insertData(editIme.getText().toString(),
editPrezime.getText().toString(),
editCena.getText().toString());
if (isInserted ==true){
Toast.makeText(Employee.this, "Zaposleni je dodat", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(Employee.this, "Zaposleni nije dodat", Toast.LENGTH_SHORT).show();
}
}
}
);
}
public void viewAll(){
btnPregledaj.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = dbEmployee.getAllData();
if (res.getCount() == 0) {
//show message
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("id: " + res.getString(0)+"\n");
buffer.append("ime: " + res.getString(1)+"\n");
buffer.append("prezime: " + res.getString(2)+"\n");
buffer.append("cena: " + res.getString(3)+"\n");
}
// Show all data
showMessage("Zaposleni", buffer.toString());
}
}
);
}
public void showMessage(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
基本上我有一个表单,我填写信息,当按下按钮时,它应该添加到数据库。但是当我点击按钮进入表格时我收到错误,所以我甚至无法填写它并点击添加到数据库。
现在,当我点击添加到数据库时,我收到此错误:
08-31 06:38:21.234 9528-9528/student1.metropolitan.com.cs330_pz E/AndroidRuntime: FATAL EXCEPTION: main
Process: student1.metropolitan.com.cs330_pz, PID: 9528
java.lang.NullPointerException
at student1.metropolitan.com.cs330_pz.Employee$1.onClick(Employee.java:44)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
好吧,看起来Button btnDodaj, btnPregledaj
从未与任何观点相关联。
你为EditTexts正确地做了,就像你在这里做的那样:
editId = (EditText)findViewById(R.id.editId);
但你的按钮在哪里?
您的按钮应该与您对EditTexts的操作类似。
btnDodaj = (Button) findViewById(R.id.___);
btnPregledaj = (Button) findViewById(R.id.___);