Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.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
Java 从另一个函数调用具有setContentView(R.layout)的函数?_Java_Android - Fatal编程技术网

Java 从另一个函数调用具有setContentView(R.layout)的函数?

Java 从另一个函数调用具有setContentView(R.layout)的函数?,java,android,Java,Android,当我通过onclick线程和可运行代码调用dbconctfunview v方法时,它是在执行dbconctfunview v方法中的其他代码之后最后执行的。在控制台中,它显示: out of loop out of loop test####### multiple class.forname queryexct queryexct 我的代码: package com.example.loginandroid; import java.sql.Connection; impo

当我通过onclick线程和可运行代码调用dbconctfunview v方法时,它是在执行dbconctfunview v方法中的其他代码之后最后执行的。在控制台中,它显示:

out of loop   out of loop
test#######   multiple class.forname
queryexct     queryexct
我的代码:

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");
             Log.v("test#######","multiple class.forname");
        } 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;
                        } 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();
                        }
                    }
                }
            }

        });
        Log.v("out of loop","out of loop");
        if(rs!=null){
            if (rs.next()) {
                Log.v("test#######","errorrrrrrrrrrr1");
                Looper.prepare();
                Thread.interrupted();
                setContentView(R.layout.activity_main1);
                Log.v("test#######","errorrrrrrrrrrr1");
            }
            else{
                Log.v("test#######","errorrrrrrrrrrr2");
                Thread.interrupted();
                Looper.prepare();

                dialog.setMessage("Your username and password are not valid");
                dialog.show();


            }
        }
        temqfag=true;
    }
}

setContentView是活动的一个功能

所以您需要像这样从其他函数调用它:

 MainActivity.this.setContentView(R.layout.activity_main1);

我不知道你到底想做什么,但据我所知,如果你试图使用线程更改布局,那么你将无法这样做,因为这只能在主线程上进行。

你能不能重写你的问题,而不使用所有大写字母?我对你的代码有两条与你的问题无关的注释。在您定义queryexct的那一行,您正在向SQL注入敞开大门。此外,看起来您正在以明文形式在数据库中存储密码。这些都是安全问题。SQL注入允许任何人在您的数据库上运行任何SQL:存储纯文本密码意味着安全漏洞将泄露您用户的密码:当我调用dbconctfunview v方法时,它首先执行代码的最后一行,然后执行此方法的最后一行。我猜您的第一行代码正在使线程休眠,这就是原因它正在执行后面的部分。我不能停止代码,在线程处于休眠状态时不执行剩余的代码,然后将整个代码放在同一个线程中。我无法更改布局并将对话框消息放在线程中。显示错误您无法使线程中的视图保持可运行状态