-1

我想在使用登录时将用户名敬酒。但是,当我尝试单击登录按钮时,它让我强制关闭。我看了一下logcat,但什么也没显示。

这个编码是在说。它会根据我在登录屏幕中输入的名称来祝酒用户名。不会有任何密码。

我不知道我的代码有什么问题。有人可以帮我吗?

家庭活动.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
        }

    }

主屏幕.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>

登录.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>
4

4 回答 4

3

你必须使用

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

代替

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

在您的LogIn方法中,因为您的Buttoninlogin.xml包含 idbuttonLoginbuttonLogIn

于 2014-01-21T06:39:39.910 回答
0

尝试这个..

您的 Button id login.xmlisbuttonLogin但您的 id 为buttonLogIn

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

    Button btnLogIn=(Button)dialog.findViewById(R.id.buttonLogin);
于 2014-01-21T06:40:02.553 回答
0

setContentView(R.layout.login);代替setContentView(R.layout.homescreen);

于 2014-01-21T06:58:40.907 回答
0

您也可以像这样显示 Toast 消息

 runOnUiThread(new Runnable() 
{
 public void run() {
 Toast.makeText(getApplicationContext(), "Your message", Toast.LENGTH_LONG).show();
  }
                                });
于 2014-01-21T06:46:17.400 回答