小编典典

方法getText()必须从UI线程调用

java

方法getText()必须从UI线程调用,请提供帮助。我是android studio的初学者,可以在网上找到这些代码,但无法弄清楚,我真的很感激。

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();
        }
    }

}

阅读 274

收藏
2020-11-30

共1个答案

小编典典

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();
        }
    }
2020-11-30