我正在尝试为应用程序创建表单。该表单包含两个edittext字段和一个提交按钮。无论如何我得到"方法getText必须从ui线程调用" doInBackground()下的错误。
这是我的代码:
package com.example.jinson.myapplication;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SubmitUname extends AppCompatActivity {
private EditText editTextName;
private EditText editTextUname;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.submit_uname);
editTextName = (EditText) findViewById(R.id.editName);
editTextUname = (EditText) findViewById(R.id.editUserName);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
}
public void insert(View view){
String name = editTextName.getText().toString();
String uname = editTextUname.getText().toString();
insertToDatabase(name,uname);
}
private void insertToDatabase(String name, String uname){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String paramUsername = params[0];
String paramAddress = params[1];
String name = editTextName.getText().toString();
String uname = editTextUname.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("uname", uname));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://localhost/insert-db.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, uname);
}
}
Android工作室说必须在说明中从UI线程调用方法getText():editTextName.getText()。toString();和editTextUname.getText()。toString();可能的解决方案??
我是这个领域的新手,请给我一个解决方案。
答案 0 :(得分:1)
Android工作室说必须从UI线程
调用方法getText()
正确。如果用户旋转屏幕或以其他方式触发配置更改,则当您从doInBackground()
调用该代码时,您的活动可能会消失。
可能的解决方案?
向SendPostReqAsyncTask
添加构造函数,并将您需要的任何值传入其中,例如EditText
在SendPostReqAsyncTask
或EditText
之前,将设置者添加到execute()
并将所需的任何值传入其中,例如来自executeOnExecutor()
的文字
另请注意:
多年来一直不建议使用HttpClient,在Android 5.1中已弃用,并已从Android 6.0中的SDK中删除
使用AsyncTask
的嵌套类实现(与SendPostReqAsyncTask
一样)将导致设备进行配置更改时发生内存泄漏(例如,用户旋转设备)从肖像到风景)
答案 1 :(得分:0)
>> heroku ps:scale web=1
方法不在UI(主)线程上运行,因此您无法从那里更新或使用doInBackground
。但好消息是,您可以使用getText()
方法进行处理。
修改强>
一个好的选择可能是,在AsyncTask之前获取editText的值,然后将其发送到SendPostReqAsyncTask构造函数。