我已经开始使用Android Studio两天了,我也没有太多的java经验。我做了一个简单的登录应用程序连接到Sql server作为数据库进行检查。我写了这个代码来自http://seotoolzz.com/android/android-login-app-with-mssql-server.php。但是在下一行中它显示了一个错误:必须从ui线程调用方法gettext。请帮忙,因为我非常新的Android应用程序开发。非常感谢。
String usernam = username.getText()。toString();
String passwordd = password.getText()。toString();
public class MainActivity extends AppCompatActivity
{
Button login;
EditText username,password;
ProgressBar progressBar;
Connection con;
String un,pass,db,ip;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.button);
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
ip = "1IP";
db = "ADB";
un = "your username for that database here";
pass = "your password for that database here";
login.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CheckLogin checkLogin = new CheckLogin();
checkLogin.execute("");
}
});
}
public class CheckLogin extends AsyncTask<String,String,String>
{
String z = "";
Boolean isSuccess = false;
@Override
protected void onPreExecute()
{
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String r)
{
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
if(isSuccess)
{
Toast.makeText(MainActivity.this , "Login Successfull" , Toast.LENGTH_LONG).show();
//finish();
}
}
@Override
protected String doInBackground(String... params)
{
String usernam = username.getText().toString();
String passwordd = password.getText().toString();
if(usernam.trim().equals("")|| passwordd.trim().equals(""))
z = "Please enter Username and Password";
else
{
try
{
con = connectionclass(un, pass, db, ip);
if (con == null)
{
z = "Check Your Internet Access!";
}
else
{
String query = "select * from Login where UserName= '" + usernam.toString() + "' and Password = '"+ passwordd.toString() +"' ";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
{
z = "Login successful";
isSuccess=true;
con.close();
}
else
{
z = "Invalid Credentials!";
isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = ex.getMessage();
}
}
return z;
}
}
@SuppressLint("NewApi")
public Connection connectionclass(String user, String password, String database, String server)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:sqlserver://" + server + database + ";user=" + user+ ";password=" + password + ";";
connection = DriverManager.getConnection(ConnectionURL);
}
catch (SQLException se)
{
Log.e("error here 1 : ", se.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("error here 2 : ", e.getMessage());
}
catch (Exception e)
{
Log.e("error here 3 : ", e.getMessage());
}
return connection;
}
}
答案 0 :(得分:1)
您正在尝试的问题是访问外部类中的当前视图元素(编辑:或者更确切地说是另一个非UI线程)。
解决方法是使用Activity.runOnUIThread(Runnable),如下所示:
String usernam, passwordd;
getActivity().runOnUIThread(new Runnable(){
usernam = username.getText().toString();
passwordd = password.getText().toString();
}
你也可以在调用它时将Strings作为输入传递给AsyncTask:
@Override
public void onClick(View v)
{
CheckLogin checkLogin = new CheckLogin();
checkLogin.execute(usernam.getText(),passwordd.getText());
}
并在AsyncTask.doInBackground中访问它:
@Override
protected String doInBackground(String... params)
{
usernam = params[0];
passwordd = params[1];
....
}
希望这有用。
答案 1 :(得分:0)
Asyntask是一个单独的线程,无法访问UI线程元素。不能将值存储为全局成员变量并访问它们。
String usernam,password;
然后在你的onclickListener中存储值。
login.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
usernam=username.getText().toString();
password=password.getText().toString();
CheckLogin checkLogin = new CheckLogin();
checkLogin.execute("");
}
});