必须从UI线程调用方法getText()请帮忙。我是Android工作室的初学者在网上找到这些代码,但无法弄明白 我真的很感激。
public class MainActivity extends AppCompatActivity {
TableLayout table_layout;
EditText firstname_et, lastname_et;
Button addmem_btn;
SQLController sqlcon;
ProgressDialog PD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sqlcon = new SQLController(this);
firstname_et = (EditText) findViewById(R.id.fistname_et_id);
lastname_et = (EditText) findViewById(R.id.lastname_et_id);
addmem_btn = (Button) findViewById(R.id.addmem_btn_id);
table_layout = (TableLayout) findViewById(R.id.tableLayout1);
BuildTable();
addmem_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new MyAsync().execute();
}
});
}
private void BuildTable() {
sqlcon.open();
Cursor c = sqlcon.readEntry();
int rows = c.getCount();
int cols = c.getColumnCount();
c.moveToFirst();
// outer for loop
for (int i = 0; i < rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 0; j < cols; j++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(0, 5, 0, 5);
tv.setText(c.getString(j));
row.addView(tv);
}
c.moveToNext();
table_layout.addView(row);
}
sqlcon.close();
}
private class MyAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
table_layout.removeAllViews();
PD = new ProgressDialog(MainActivity.this);
PD.setTitle("Please Wait..");
PD.setMessage("Loading...");
PD.setCancelable(false);
PD.show();
}
@Override
protected Void doInBackground(Void... params) {
**String firstname = firstname_et.getText().toString();
String lastname = lastname_et.getText().toString();**
// inserting data
sqlcon.open();
sqlcon.insertData(firstname, lastname);
// BuildTable();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
BuildTable();
PD.dismiss();
}
}
}
答案 0 :(得分:0)
您正在从无法执行的后台线程(您的异步任务)访问UI线程。
String firstname = firstname_et.getText().toString();
String lastname = lastname_et.getText().toString();
在UI线程中添加不在异步任务中的theese行
这是一个例子
// ui thread
// call async task and send UI data
new checkLogInDetails(editEmail.getText().toString(), editPassword.getText().toString()).execute();
// you can change the params of async task as your requirement
private class checkLogInDetails extends AsyncTask<Void, String, Void> {
private final String userName;
private final String password;
//create a constructor an access UI thread data
public checkLogInDetails(String userName, String password) {
this.userName = userName;
this.password = password;
}
@Override
protected void onPreExecute()...
答案 1 :(得分:0)
您无法在 AsyncTask 中使用 getText 。 你必须像这样传递参数:
new MyAsync()。execute(firstname_et.getText()。toString(),lastname_et.getText()。toString());
然后在异步任务中你必须将错误行改为:
String firstname =(String)params [0]; String lastname =(String)params [1];
答案 2 :(得分:0)
获取onPreExecute()
其UI线程中的值不在后台线程中。
private class MyAsync extends AsyncTask<Void, Void, Void> {
String firstname,lastname;
@Override
protected void onPreExecute() {
super.onPreExecute();
table_layout.removeAllViews();
PD = new ProgressDialog(MainActivity.this);
PD.setTitle("Please Wait..");
PD.setMessage("Loading...");
PD.setCancelable(false);
// get values here its UI thread
firstname = firstname_et.getText().toString();
lastname = lastname_et.getText().toString();
PD.show();
}
@Override
protected Void doInBackground(Void... params) {
// **String firstname = firstname_et.getText().toString();
// String lastname = lastname_et.getText().toString();**
// inserting data
sqlcon.open();
sqlcon.insertData(firstname, lastname);
// BuildTable();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
BuildTable();
PD.dismiss();
}
}
答案 3 :(得分:0)
用此
替换您的代码 String firstname;
String secondname;
@Override
protected void onPreExecute(){
firstname = firstname_et.getText().toString();
lastname = lastname_et.getText().toString();
}
@Override
protected Void doInBackground(Void... params) {
// inserting data
sqlcon.open();
sqlcon.insertData(firstname, lastname);
// BuildTable();
return null;
}