以下是我的代码;当我单击提交按钮时,我无法在按钮单击时插入数据,因为我的程序停止运行,我的代码如下:
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class insertData extends Activity {
/** Called when the activity is first created. */
SQLiteDatabase db;
ContentValues c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); setContentView(R.layout.main);
final EditText name=(EditText) findViewById(R.id.edtname);
final EditText address=(EditText) findViewById(R.id.edtaddress);
final EditText policyNo=(EditText) findViewById(R.id.edtpolicyno);
Button submit=(Button) findViewById(R.id.submit);
final TextView text = new TextView(getApplicationContext());
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
setContentView(R.layout.main);
c = new ContentValues();
db = openOrCreateDatabase(
"TestingData.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
// String qry="CREATE TABLE test1(name text,address text,policy text);";
// db.execSQL(qry);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"heloo",Toast.LENGTH_SHORT).show();
c.put("Name",name.getText().toString().trim());
c.put("Address",address.getText().toString().trim());
c.put("PolicyNo",policyNo.getText().toString().trim());
db.insert("test1", null, c);
Cursor retrive = db.query("test11",null ,null, null, null, null, null);
retrive.moveToFirst();
for(int i=0;i<retrive.getCount();i++)
{
String nm = retrive.getString(1);
String add=retrive.getString(2);
String polno=retrive.getString(3);
text.setText(nm+" "+add+" "+polno);
}//for ends/......
// retrive.close();
}
});
}
}
我不知道这里发生了什么;请帮帮我。
答案 0 :(得分:2)
你还没有发布你的logcat转储,但我注意到的第一件事是你正在创建一个名为“test1”的表并将数据插入/查询到“test11”。
答案 1 :(得分:0)
我的按钮点击事件未分配内存因此声明按钮不同如下所示我的点击事件被检测并解雇
public class insertData extends Activity {
/** Called when the activity is first created. */
SQLiteDatabase db;
ContentValues c;
Button submit; <-------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); sss
final EditText name=(EditText) findViewById(R.id.edtname);
final EditText address=(EditText) findViewById(R.id.edtaddress);
final EditText policyNo=(EditText) findViewById(R.id.edtpolicyno);
submit=(Button) findViewById(R.id.submit);
我不知道为什么,但这解决了我的问题。