0

I have created a login app which login id and password from sql server it works properly for correct login id, password and also gives toast for empty login field

But if I enter incorrect fields. It is not showing desired toast. Perhaps I am using toast in wrong place. My code is

public void onClick(View v) {

            // TODO Auto-generated method stub
            ResultSet rs=null;
            String id=uid.getText().toString();
            String upass=pass.getText().toString();
            final String uid1=null;
            final ProgressDialog pd=new ProgressDialog(MainActivity.this);
            pd.setTitle("Processing");
            pd.setMessage("Processing is going on..plz be patient...");         
            pd.show();
            if(id.equals("")&&upass.equals("")){

                Toast.makeText(getApplicationContext(), "empty id or password", Toast.LENGTH_SHORT).show();
            }


            new Thread(){
                public void run(){
                    //defining task for progress dialog for busy.
                    try{
                        ResultSet rs=null;
                        String id=uid.getText().toString();
                        String upass=pass.getText().toString();
                        String uid1=null;


                        if(!id.isEmpty()&&!upass.isEmpty())
                            try{
                            initilize();
                            Statement statement=connect.createStatement();
                            rs=statement.executeQuery("LOGIN '"+id+"', '"+upass+"'");
                            List<String>data=new ArrayList<String>();
                            if(rs.next()){

                                uid1=rs.getString("PA_NAME");
                                pd.dismiss();
                                Intent i=new Intent(getApplicationContext(),SecondActivity.class);
                                i.putExtra("a", uid1);
                                startActivity(i);
                            }






                            }catch(Exception e){
                                e.printStackTrace();
                            }

                        else
                            Toast.makeText(MainActivity.this, "invalid id or password", Toast.LENGTH_LONG).show();pd.dismiss();





                    }catch(Exception e){                        
                    }
                    pd.dismiss();
                }}.start();

If some one suggest me where to place that toast for incorrect login id. In conditional statement. It's urgent.

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
12345
  • 143
  • 3
  • 4
  • 13

5 Answers5

1

You can only create UI objects on the main thread. This includes toasts. Easiest way for you is to use Activity#runOnUiThread to show your toast. You should also check out the Handler class as well.

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
0

Using runOnUiThread method:

new Thread() {
    public void run() {
        activity.runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(MainActivity.this, "invalid id or password", Toast.LENGTH_LONG).show();
            }
        });
    }
}.start();

Using Handler:

final Handler mHandler = new Handler();
final Runnable mUpdateResults = new Runnable() {
    public void run() {
        Toast.makeText(MainActivity.this, "invalid id or password", Toast.LENGTH_LONG).show();
    }

new Thread() {
    public void run() {
        mHandler.post(mUpdateResults);
    }
}.start();
Community
  • 1
  • 1
Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
0

you should show toast in UIthread always. Modify the code as shown below:

runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "invalid id or password", Toast.LENGTH_LONG).show();
                pd.dismiss();

            }
        });
Suji
  • 6,044
  • 2
  • 19
  • 17
0

Try doing it like that:

MainActivity.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(MainActivity, "Toast on the main Activity", Toast.LENGTH_SHORT).show();
    }
});
g00dy
  • 6,752
  • 2
  • 30
  • 43
0

best way would be to write a handler in your main activity (UI thread). in the handler show the toast. from thread post a message to that handler.

Sushil
  • 8,250
  • 3
  • 39
  • 71