Android按钮单击仅工作一次

时间:2013-06-29 13:02:20

标签: android android-intent

我制作了一个注册表单,其中包含一组文本字段和一个注册按钮。单击注册按钮时,它会检查任何空文本字段,如果存在,则返回0并再次启动活动。但问题是按钮点击事件只能工作一次。下次单击该按钮时,应用程序将关闭。

这是代码,

    public class Register extends Activity implements OnClickListener { 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        Button btnRegister=(Button) findViewById(R.id.btnRegisterMe);

        btnRegister.setOnClickListener(Register.this);


    }

@Override

    public void onClick(View v) {
        if(v.getId()==R.id.btnRegisterMe)
        {
            int controlsCheck=CheckForEmptyTextBox();
            if(controlsCheck==0)
            {
                Toast toastSome=Toast.makeText(Register.this, "Check the             error", Toast.LENGTH_SHORT);
                toastSome.show();


            }

        }

    }

        protected int CheckForEmptyTextBox()
    {
        int check;
        EditText txtMobile=(EditText) findViewById(R.id.txtMobile);
        EditText txtUsername=(EditText) findViewById(R.id.txtUsername);
        EditText txtPassword=(EditText) findViewById(R.id.txtPassword);



        if(txtMobile.length()==0)
        {
            txtMobile.setBackgroundColor(Color.RED);

            Toast toastMobile=Toast.makeText(Register.this, "Enter Mobile Number", Toast.LENGTH_SHORT);
            toastMobile.show();

            check=0;

        }

        else if(txtUsername.length()==0)
        {
            txtUsername.setBackgroundColor(Color.RED);
            Toast toastUsername=Toast.makeText(Register.this, "Enter Username", Toast.LENGTH_SHORT);
            toastUsername.show();

            check=0;

        }

        else if(txtPassword.length()==0)
        {
            txtPassword.setBackgroundColor(Color.RED);
            Toast toastPassword=Toast.makeText(Register.this, "Enter Password", Toast.LENGTH_SHORT);
            toastPassword.show();
            check=0;

        }

                else
        {
            check=1;
        }

        if(check==0)
        {

            return check;
        }
        else

        {
            return check;
        }

    }

}

我想向用户保证他填写了所有字段。请告诉我如何处理android中相同按钮的多次点击事件。

记录数据::

06-29 18:16:51.997: E/AndroidRuntime(5523): FATAL EXCEPTION: main
06-29 18:16:51.997: E/AndroidRuntime(5523): java.lang.NullPointerException
06-29 18:16:51.997: E/AndroidRuntime(5523):     at com.example.restaurants.Register.CheckForEmptyTextBox(Register.java:74)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at com.example.restaurants.Register.onClick(Register.java:37)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at android.view.View.performClick(View.java:3534)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at android.view.View$PerformClick.run(View.java:14263)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at android.os.Handler.handleCallback(Handler.java:605)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at android.os.Looper.loop(Looper.java:137)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at android.app.ActivityThread.main(ActivityThread.java:4441)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at java.lang.reflect.Method.invoke(Method.java:511)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-29 18:16:51.997: E/AndroidRuntime(5523):     at dalvik.system.NativeStart.main(Native Method)
06-29 18:23:27.667: D/TextLayoutCache(5704): Using debug level: 0 - Debug Enabled: 0
06-29 18:23:27.737: D/libEGL(5704): loaded /system/lib/egl/libGLES_android.so
06-29 18:23:27.797: D/libEGL(5704): loaded /system/lib/egl/libEGL_adreno200.so
06-29 18:23:27.867: D/libEGL(5704): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
06-29 18:23:27.877: D/libEGL(5704): loaded /system/lib/egl/libGLESv2_adreno200.so
06-29 18:23:28.117: D/OpenGLRenderer(5704): Enabling debug mode 0

1 个答案:

答案 0 :(得分:0)

Register.java中的第74行存在空指针异常。先解决这个问题。可能由于此异常CheckForEmptyTextBox()可能返回非零值。