本文介绍了getText()方法必须从UI线程调用(Android的工作室)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个登录名的应用程序。不过,我有一个问题。
I'm trying to create a login for an application. However I have a problem.
这是我的code:
package com.forgetmenot.loginregister;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText uname, password;
Button submit;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
private static final String TAG = "Login";
JSONObject json;
private static String url_login = "http://localhost:8080/ForgetMeNotApplication/Login";
//JSONArray incoming_msg = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewsById();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// execute method invokes doInBackground() where we open a Http URL connection using the given Servlet URL
//and get output response from InputStream and return it.
new Login().execute();
}
});
}
private void findViewsById() {
uname = (EditText) findViewById(R.id.txtUser);
password = (EditText) findViewById(R.id.txtPass);
submit = (Button) findViewById(R.id.login);
}
private class Login extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... args) {
// Getting username and password from user input
String username = uname.getText().toString();
String pass = password.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("u",username));
params.add(new BasicNameValuePair("p",pass));
json = jParser.makeHttpRequest(url_login, "GET", params);
String s=null;
try {
s= json.getString("info");
Log.d("Msg", json.getString("info"));
if(s.equals("success")){
Intent login = new Intent(getApplicationContext(), home.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Android的工作室表示,法的getText()
必须调用从UI线程在istructions: uname.getText()的toString() ;
和 password.getText()的toString();
可能的解决方案?
Android studio says that method getText()
must be called from the UI Thread at the istructions: uname.getText().toString();
and password.getText().toString();
Possible solutions??
推荐答案
试试这个:
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String username = uname.getText().toString();
String pass = password.getText().toString();
new Login().execute(username, pass);
}
});
}
private void findViewsById() {
uname = (EditText) findViewById(R.id.txtUser);
password = (EditText) findViewById(R.id.txtPass);
submit = (Button) findViewById(R.id.login);
}
private class Login extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... args) {
// Getting username and password from user input
String username = args[0];
String pass = args[1];
这篇关于getText()方法必须从UI线程调用(Android的工作室)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!