Question

I want to toast the username out when the use log in. However, when I tried to click on the login button, it gave me force close. I look at the logcat,but nothing shows up.

This coding is saying. it will toast the username depending on what name I've input in the login screen. there wouldn't be any password.

I don't know what is wrong with my code. Can someone help me out?

homeactvity.java:

public class homeActivity extends Activity{
    Button btnLogIn;
    Button btnAbout;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.homescreen);
         // create a instance of SQLite Database

      // Get The Reference Of Buttons
         btnLogIn=(Button)findViewById(R.id.buttonLogIn);

         btnAbout=(Button)findViewById(R.id.buttonAbout);

         // Set OnClick Listener on SignUp button

            // set OnClick Listener on About button
            btnAbout.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    /// Create Intent for About  and start activity
                    Intent intentAbout=new Intent(getApplicationContext(),about.class);
                    startActivity(intentAbout);
                    }
                });


        }
    // Methods to handleClick Event of Sign In Button
        public void LogIn(View V)
           {
                final Dialog dialog = new Dialog(homeActivity.this);
                dialog.setContentView(R.layout.login);
                dialog.setTitle("Login");

                // get the Refferences of views
                final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);

                Button btnLogIn=(Button)dialog.findViewById(R.id.buttonLogIn);

                // Set On ClickListener
                btnLogIn.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // get The User name and Password
                        String userName=editTextUserName.getText().toString();

                        // fetch the Password form database for respective user name
                // check if the Stored password matches with  Password entered by user
                        if(userName.equals(userName))
                        {
                            Toast.makeText(homeActivity.this, "Welcome," + userName, Toast.LENGTH_LONG).show();
                            dialog.dismiss();
                            Intent mainact=new Intent(getApplicationContext(),MainActivity.class);
                            startActivity(mainact);


                        }
                        else
                        {
                            Toast.makeText(homeActivity.this, "No Such Username", Toast.LENGTH_LONG).show();
                        }
                    }
                });

                dialog.show();          

        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            // Close The Database
        }

    }

homescreen.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    android:gravity="center_vertical"
    >


    <Button
        android:id="@+id/buttonLogIn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:text="Login" 
         android:onClick="LogIn"/>

    <Button
        android:id="@+id/buttonAbout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="About" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/home" />

</LinearLayout>

login.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editTextUserNameToLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="User Name"
        android:ems="10" >
        <requestFocus />
    </EditText>


    <Button
        android:id="@+id/buttonLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Log In" />
</LinearLayout>
Was it helpful?

Solution

You have to use

Button btnLogIn=(Button)dialog.findViewById(R.id.buttonLogin);

instead of

Button btnLogIn=(Button)dialog.findViewById(R.id.buttonLogIn);

in your LogIn method as your Button in login.xml contains id buttonLogin not buttonLogIn

OTHER TIPS

Try this..

Your Button id in your login.xml is buttonLogin but your given as id as buttonLogIn

    final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);

    Button btnLogIn=(Button)dialog.findViewById(R.id.buttonLogin);

You can also show Toast message like this

 runOnUiThread(new Runnable() 
{
 public void run() {
 Toast.makeText(getApplicationContext(), "Your message", Toast.LENGTH_LONG).show();
  }
                                });

setContentView(R.layout.login); instead of setContentView(R.layout.homescreen);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top