从另一个函数调用setContentView(R.layout)函数?

时间:2014-03-10 06:27:52

标签: java android

当我通过onclick线程和可运行代码调用dbconctfun(view v)方法时,它在dbconctfun(view 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;
    }
}

2 个答案:

答案 0 :(得分:0)

setContentView是您活动的一个功能。

所以你需要从其他函数调用它:

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

答案 1 :(得分:0)

我没有完全得到你想要的但是从我的理解如果你试​​图使用线程改变布局然后你将无法做到这一点,因为它只能在主线程上。