这个问题在这里已有答案:
自从我开始使用android studio已经有一个月了,此时我已经陷入了这样的境地:我必须将<EditText/>
中的一组MainActivity
值传递给UpdateActivity
,然后将这些值发送到Sqlite数据库进行更新表。
我所取得的成就:
成功地将值从MainActivity
的<EditText/>
发送到UpdateActivity
的<EditText/>
我是如何从MainActivity
那样做的:
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editName, editSurname, editMarks;
Button btnUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
editName = (EditText) findViewById(R.id.UpdName);
editSurname = (EditText) findViewById(R.id.editTextSurname);
editMarks = (EditText) findViewById(R.id.UpdMarks);
btnUpdate = (Button)findViewById(R.id.btnUpdate);
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String MainName = editName.getText().toString();
String MainSurname = editSurname.getText().toString();
String MainMarks = editMarks.getText().toString();
Intent UpdateAct = new Intent(MainActivity.this, UpdateActivity.class);
UpdateAct.putExtra("Name", MainName);
UpdateAct.putExtra("SurName",MainSurname );
UpdateAct.putExtra("Marks", MainMarks);
startActivity(UpdateAct);
}
});
}
然后在UpdateActivity
内:
public class UpdateActivity extends AppCompatActivity {
DatabaseHelper myDb;
Button btnUpd;
EditText editTextId, editUpdtName, editUpdSurname, editUpdMarks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
Intent getData = getIntent();
String Name = getData.getStringExtra("Name");
String SurName = getData.getStringExtra("SurName");
String Marks = getData.getStringExtra("Marks");
editTextId = (EditText)findViewById(R.id.editTextID);
editUpdtName = (EditText)findViewById(R.id.UpdName);
editUpdSurname = (EditText)findViewById(R.id.UpdSurname);
editUpdMarks = (EditText)findViewById(R.id.UpdMarks);
btnUpd = (Button)findViewById(R.Id.btnUpd);
editUpdtName.setText(Name);
editUpdSurname.setText(SurName);
editUpdMarks.setText(Marks);
}
我坚持的是将这些值发送到一个名为updateData();
的函数。为此我试图将MainActivity
中的值直接用于UpdateActivity
但失败了,然后我尝试投射那些<EditText/>
并在函数内使用它但又失败了。
在我的UpdateActivity
里面:
..//
editUpdtName.setText(Name);
editUpdSurname.setText(SurName);
editUpdMarks.setText(Marks);
//updateData(Name, SurName, Marks);
updateData();
}
//function definiton
// public void updateData(final String Name, final String SurName, final String Marks)
public void updateData()
{
btnUpd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// boolean isUpdated = myDb.updateData(editTextId.getText().toString(), Name,SurName, Marks);
boolean isUpdated = myDb.updateData(editTextId.getText().toString(), editUpdtName.getText().toString(),
editUpdSurname.getText().toString(), editUpdMarks.getText().toString());
if(isUpdated == true){
Toast.makeText(UpdateActivity.this, "Data Updated Successfully!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(UpdateActivity.this, "Data NOT Updated!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
每次调用函数updateData();
时应用程序崩溃,以确保这是我甚至评论过的真正原因,并且还尝试将其放入处理程序中以在3000ms之后运行它,因为它已经在时间段之后崩溃了。
通过初始化db来解决它。
myDb = new DatabaseHelper(this);
你忘了初始化myDb
。在你的onCreate()
方法中初始化它:
myDb = new DatabaseHelper(this);
为什么不将值传递给updateData()
?
editUpdtName.setText(Name);
editUpdSurname.setText(SurName);
editUpdMarks.setText(Marks);
updateData(Name, SurName, Marks);
你有没有初始化btnUpd?我在你的onCreate()中看不到这个
btnUpd = (Button) findViewById(...)