Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在可运行线程中设置对话框或布局?_Android - Fatal编程技术网

Android 如何在可运行线程中设置对话框或布局?

Android 如何在可运行线程中设置对话框或布局?,android,Android,我正在检查数据库表中的天气用户名和密码。如果用户名和密码存在,我将设置layout setContentView(R.layout.activity_main1),如果未显示dialog message dialog.setMessage(“您的用户名和密码无效”)。它显示错误您无法在线程中设置视图。它们是否有显示布局的替代方法,或者我编写的代码有误 package com.example.loginandroid; import java.sql.Connection; import

我正在检查数据库表中的天气用户名和密码。如果用户名和密码存在,我将设置layout setContentView(R.layout.activity_main1),如果未显示dialog message 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();
                        }
                    }
                }
            }

        });



    }


}

在线程中使用make处理程序和sendtomessage。
若处理程序在线程被发送时收到消息,则使用如下代码

LinearLayout contentsLayout = ( LinearLayout )findViewById( R.id.contentsLayout ); LayoutInflater inflater = ( LayoutInflater )getSystemService( Context.LAYOUT_INFLATER_SERVICE ); inflater.inflate( R.layout.button, contentsLayout, true );
您必须更改此代码,但如果需要,视图将被更改。

您可以使用活动的类方法

这里是处理程序类的一个小示例

 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);
}
您可以查看教程