我試圖爲應用程序創建窗體。該表單包含兩個edittext字段和一個提交按鈕。無論如何,我得到的「方法getText必須調用從ui線程」錯誤下的doInBackground()。Android Studio方法gettext必須從當前推斷的UI線程調用線程是工作
這是我的代碼:
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工作室說,法的getText()必須從指令的UI線程調用:editTextName.getText()的toString()。和editTextUname.getText()。toString();可能的解決方案??
我是這個領域的新手,請給我一個解決方案。
在此論壇中有類似的問題。可能是http://stackoverflow.com/questions/32568340/android-studio-error-method-gettext-must-be-called-from-the-ui-thread-curre這將幫助你 – Shriram
你正在創建一個類該方法,你應該在方法外部創建SendPostReqAsyncTask。您無法訪問doInBackground方法中的UI組件。因爲此方法在工作線程中運行。 – USKMobility
你已經傳遞'name'和'uname'並訪問了,那麼爲什麼你需要再次使用'getText()。toString()'? –