我在将SQL值插入SQLite数据库时遇到问题。我没有在Monitor(Logcat)中看到错误。当我点击按钮插入数据时,应用程序就会冻结。
以下是我打开数据库的方法:
newdb = openOrCreateDatabase("Count_DB",MODE_PRIVATE,null);
newdb.execSQL("CREATE TABLE IF NOT EXISTS Count(Name VARCHAR(50),Description VARCHAR(200),NoC VARCHAR(1000),Time VARCHAR(100));");
以下是我保存数据的onClick
事件:
cr.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Vibrator n = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
n.vibrate(500);
DateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
Date now = Calendar.getInstance().getTime();
cDT = df.format(now);
customD = new Dialog(nCounter.this);
customD.setContentView(R.layout.custom_dialog);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
//int height = metrics.heightPixels;
customD.getWindow().setLayout((6 * width)/7, RelativeLayout.LayoutParams.WRAP_CONTENT);
customD.setTitle("Save Counter");
Button s = (Button)customD.findViewById(R.id.button);
s.setText(Integer.toString(x));
e1 = (EditText)customD.findViewById(R.id.editText);
e2 = (EditText)customD.findViewById(R.id.editText2);
save = (Button)customD.findViewById(R.id.button3);
cancel = (Button)customD.findViewById(R.id.button2);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vv) {
name = e1.getText().toString();
desc = e2.getText().toString();
if (!name.equals("") && !desc.equals("")){
InsertintoCount();
while(true){
Snackbar.make(vv, "Countings Saved!", Snackbar.LENGTH_LONG).show();
}
}
else {
Toast.makeText(getApplicationContext(), "Please fill in the fields!", Toast.LENGTH_LONG).show();
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customD.dismiss();
}
});
customD.show();
return true;
}
});
这是InsertintoCount()
方法:
public boolean InsertintoCount(){
String sqlc = "INSERT INTO Count VALUES('"+name+"','"+desc+"','"+x+"','"+cDT+"');";
newdb.execSQL(sqlc);
while(true){
return true;
}
}
答案 0 :(得分:2)
您在onClick
事件中运行了无限循环。毫无疑问,你的应用程序没有回应。
删除此while循环:
while(true){
Snackbar.make(vv, "Countings Saved!", Snackbar.LENGTH_LONG).show();
}
答案 1 :(得分:0)
我不知道冻结你的意思,你怎么注意到它?但是,我会做出以下更改
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vv) {
name = e1.getText().toString();
desc = e2.getText().toString();
if (!name.isEmpty() && !desc.isEmpty()){
InsertintoCount();
} else {
Toast.makeText(getApplicationContext(), "Please fill in the fields!", Toast.LENGTH_LONG).show();
}
}
});
然后在InsertingCount()
中(同时确保初始化x
和cDT
):
public void InsertintoCount(){
String sqlc = "INSERT INTO Count VALUES('"+name+"','"+desc+"','"+x+"','"+cDT+"');";
newdb.execSQL(sqlc);
}
尝试并告诉我们它是如何进行的!