没有移动到下一个活动

时间:2014-09-29 08:36:54

标签: android android-activity

您好在我的应用程序登录表单中,用户名和密码检查数据库中是否存在用户名和密码。如果存在,则表示显示一条用户找到的短信,我想转到下一个活动。否则找不到这样的用户。

现在,点击登录按钮,显示用户已找到但未转到下一个活动。

任何人都可以帮我解决这个问题。

Login.java

  public class Login extends Activity {
    Button login;
    private static final Pattern USERNAME_PATTERN = Pattern
            .compile("[a-zA-Z0-9]{1,250}");
    private static final Pattern PASSWORD_PATTERN = Pattern
            .compile("[a-zA-Z0-9+_.]{4,16}");
    EditText usname,pword;
    TextView tv;

    String username,password;
    HttpPost httppost;
    StringBuffer buffer;
    String data="";
    HttpResponse response;
    HttpClient httpclient;
    CheckBox mCbShowPwd;
    List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        login = (Button)findViewById(R.id.login);  
        usname = (EditText)findViewById(R.id.username);
        pword= (EditText)findViewById(R.id.password);
        tv = (TextView)findViewById(R.id.tv);
        mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);

       mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (!isChecked) {

                    pword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {

                     pword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });

       login.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               String username = usname.getText().toString();
               String password = pword.getText().toString();
               if (username.equals("") || password.equals("")) {
                   if (username.equals("")) {
                       Toast.makeText(Login.this, "ENTER USERNAME",
                               Toast.LENGTH_LONG).show();

                   }
                   if (password.equals("")) {
                       Toast.makeText(Login.this, "ENTER PASSWORD",
                               Toast.LENGTH_LONG).show();

                   }

               } else if (!CheckUsername(username) && !CheckPassword(password)){
                       Toast.makeText(Login.this, "ENTER VALID USERNAME & PASSWORD",
                             Toast.LENGTH_LONG).show();
               }
               else{
                   final String queryString = "username=" + username + "&password="
                    + password;
                   final String data = DatabaseUtility.executeQueryPhp("login",queryString);

            System.out.println("data :: "+data);
         tv.setText("Response from PHP : " + data);
            if(data.equalsIgnoreCase("User Found"))
            {
               Toast.makeText(Login.this,"Login Success", Toast.LENGTH_SHORT).show();
               Intent i = new Intent(Login.this, Home.class);
               startActivity(i);           
            }
            else
            {
               Toast.makeText(getApplicationContext(),"User not found, check query", Toast.LENGTH_SHORT).show();
            }
               }                    

            }
         });


    }

            private boolean CheckPassword(String password) {

                return PASSWORD_PATTERN.matcher(password).matches();
            }

            private boolean CheckUsername(String username) {

                return USERNAME_PATTERN.matcher(username).matches();
            }

    }

2 个答案:

答案 0 :(得分:0)

我认为你错过了

<activity
  android:name="package_name.Home"></activity>

AndroidManifest.xml

您必须在AndroidManifest.xml中定义所有活动。

修改

System.out.println("data :: "+data);
if(data.equalsIgnoreCase("User Found"))
{
   Toast.makeText(getApplicationContext(),"Login Success", Toast.LENGTH_SHORT).show();
   Intent i = new Intent(getApplicationContext(), Home.class);
   startActivity(i);           
}
else
{
   Toast.makeText(getApplicationContext(),"User not found, check query", Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

我认为你在这里犯了错误:

if(data.equalsIgnoreCase(&#34; User Found&#34;)){

                            Toast.makeText(getApplicationContext(),"Login Success", Toast.LENGTH_SHORT).show();
                            Intent i = new Intent(getApplicationContext(), Home.class);

                            startActivity(i);           

        }

代替getApplicationContext()写:  if(data.equalsIgnoreCase(&#34; User Found&#34;)){

                            Toast.makeText(Login.this,"Login Success", Toast.LENGTH_SHORT).show();
                            Intent i = new Intent(getApplicationContext(), Home.class);

                            startActivity(i);