我正在检查DB表中存在的天气用户名和密码。如果用户名和密码存在,我正在设置布局setContentView(R.layout.activity_main1),如果没有显示对话框消息dialog.setMessage(“您的用户名和密码是无效“)。它显示错误,你不能在线程中设置视图。是他们任何替代方式来显示布局或我写错了代码。
package com.example.loginandroid;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Looper;
public class MainActivity extends Activity{
String username,password; ResultSet rs =null;
boolean temcfag=false,temqfag=true;
public static String tag="Lifecycle activity";
EditText user,pass;
AlertDialog.Builder dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog=new AlertDialog.Builder(this);
dialog.setNeutralButton("OK", null);
user=(EditText)findViewById(R.id.editText1);
pass=(EditText)findViewById(R.id.editText2);
}
Thread thrd1,thrd2,thrd3;
Connection con;
String result ="",queryexct;
public void dbconctfun(View v ) throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
thrd1 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
}
if (con == null) {
try {
con = DriverManager.getConnection("jdbc:mysql://111.111.11.11:6666/dyne", "root1", "mysql");
} catch (SQLException e) {
e.printStackTrace();
con = null;
}
if ((thrd2 != null) && (!thrd2.isAlive()))
thrd2.start();
}
}
}
});
if ((thrd1 != null) && (!thrd1.isAlive())) thrd1.start();
thrd2 = new Thread(new Runnable() {
public void run() {
while (!Thread.interrupted()) {
if (con != null) {
if (temqfag) {
try {Statement st = con.createStatement();
username=user.getText().toString().trim();
password=pass.getText().toString().trim();
queryexct="SELECT * FROM `user_registration` WHERE `email_id` = '"+username+"' AND `password` = '"+password+"'";
rs = st.executeQuery(queryexct);
Log.v("queryexct",queryexct);
temqfag=false;
if(rs!=null){
if (rs.next()) {
Looper.prepare();
Thread.interrupted();
setContentView(R.layout.activity_main1);
}
else{
Thread.interrupted();
Looper.prepare();
dialog.setMessage("Your username and password are not valid");
dialog.show();
}}
} catch (SQLException e) {
e.printStackTrace();
con = null;
}
try {
Log.v("test#######","errorrrrrrrrrrr3");
if (temqfag) {Thread.sleep(10);}
} catch (InterruptedException e) {
e.printStackTrace();
}}
} else {
try {
Log.v("test#######","errorrrrrrrrrrr4");
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
}
}
答案 0 :(得分:0)
您在Thread中使用make Handler和sendtomessage 如果Handler在发送线程时收到消息,请使用此代码。
LinearLayout contentsLayout = ( LinearLayout )findViewById( R.id.contentsLayout ); LayoutInflater inflater = ( LayoutInflater )getSystemService( Context.LAYOUT_INFLATER_SERVICE ); inflater.inflate( R.layout.button, contentsLayout, true );
您必须更改此代码,但如果需要,则视图会更改。
答案 1 :(得分:0)
您可以使用活动的Handler类runOnUiThread方法
这里是处理程序类
的小例子 private final Handler handler = new Handler() { // put in somewhere in your activity
public void handleMessage(Message msg) {
if (msg.arg1 == 1) {
setContentView(R.layout.activity_main1);
} else {
dialog.setMessage("Your username and password are not valid");
dialog.show();
}
}
}
代码
if (rs.next()) {
Looper.prepare();
Thread.interrupted();
Message msgObj = handler.obtainMessage();
msgObj.arg1 = 1;
handler.sendMessage(msgObj);
} else {
Thread.interrupted();
Looper.prepare();
Message msgObj = handler.obtainMessage();
msgObj.arg1 = 0;
handler.sendMessage(msgObj);
}
您可以查看教程here