无法登录(android应用程序)

无法登录(android应用程序),android,android-layout,android-intent,android-activity,android-fragments,Android,Android Layout,Android Intent,Android Activity,Android Fragments,我要创建一个注册页面和一个登录页面。注册后,当我尝试登录时,显示密码不匹配,我找不到任何错误。请帮助 LoginActivity1.java package com.example.app; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.database.sqlit

我要创建一个注册页面和一个登录页面。注册后,当我尝试登录时,显示密码不匹配,我找不到任何错误。请帮助

LoginActivity1.java

package com.example.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity1 extends Activity implements OnClickListener 
{

    Button mLogin;
    Button mRegister;

    EditText muname;
    EditText mpassword;

    DBHelper2 DB2 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mRegister = (Button) findViewById(R.id.register);
        mRegister.setOnClickListener(this);

        mLogin = (Button) findViewById(R.id.login);
        mLogin.setOnClickListener(this);
    }
        public void onClick(View v) {
            switch (v.getId()) {

            case R.id.register:
                Intent i = new Intent(getBaseContext(), Registration2.class);
                startActivity(i);
                break;

            case R.id.login:

                muname = (EditText) findViewById(R.id.Ledituname);
                mpassword = (EditText) findViewById(R.id.Leditpw);

                String username = muname.getText().toString();
                String password = mpassword.getText().toString();

                if (username.equals("") || username == null) {
                    Toast.makeText(getApplicationContext(),
                            "Please enter User Name", Toast.LENGTH_SHORT).show();
                } else if (password.equals("") || password == null) {
                    Toast.makeText(getApplicationContext(),
                            "Please enter your Password", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    boolean validLogin = validateLogin(username, password,
                            getBaseContext());
                    if (validLogin) {
                        // System.out.println("In Valid");
                        //Intent in = new Intent(getBaseContext(), TabBar.class);
                        // in.putExtra("UserName", muname.getText().toString());
                        //startActivity(in);
                        Intent i1 = new Intent(getBaseContext(), TabBar.class);
                        Toast.makeText(getApplicationContext(),
                                "Success Valid Login", Toast.LENGTH_SHORT)
                                .show();
                        startActivity(i1);
                        // finish();
                    }
                   /* else
                    {
                        Toast.makeText(getApplicationContext(),
                                "Register First", Toast.LENGTH_SHORT)
                                .show();
                    }*/
                }
                break;

            }

        }

        private boolean validateLogin(String username, String password,
                Context baseContext) {
            DB2 = new DBHelper2(getBaseContext());
            SQLiteDatabase db2 = DB2.getReadableDatabase();

            String[] columns = { "id" };

            String selection = "username=? AND password=?";
            String[] selectionArgs = { username, password };

            Cursor cursor = null;
            try {

                cursor = db2.query(DBHelper2.DATABASE_TABLE_NAME, columns, selection,
                        selectionArgs, null, null, null);
                startManagingCursor(cursor);
            } catch (Exception e)

            {
               System.out.println("Excetion in valid login"+e.getMessage());
            }
            int numberOfRows = cursor.getCount();

            if (numberOfRows <= 0) {

                Toast.makeText(getApplicationContext(),
                        "User Name and Password miss match.Register First.\nPlease Try Again",
                        Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getBaseContext(), MainActivity.class);
                startActivity(intent);
                return false;
            }

            return true;

        }

        public void onDestroy() {
            super.onDestroy();
            DB2.close();
        }
    }
Manifestfile.java

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name="com.example.app.LoginActivity1"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.app.Registration" >
        </activity>
        <activity android:name="com.example.app.TabBar" >
        </activity>
        <activity android:name="com.example.app.Tab1" >
        </activity>
        <activity android:name="com.example.app.Tab2" >
        </activity>
        <activity android:name="com.example.app.Tab3" >
        </activity>
        <activity
            android:name="com.example.app.Person"
            android:label="@string/title_activity_person" >
        </activity>
        <activity
            android:name="com.example.app.History"
            android:label="@string/title_activity_history" >
        </activity>
        <activity
            android:name="com.example.app.Medications"
            android:label="@string/title_activity_medications" >
        </activity>
        <activity
            android:name="com.example.app.AddMedRecords"
            android:label="@string/title_activity_add_med_records" >
        </activity>
        <activity
            android:name="com.example.app.OfficeVisit"
            android:label="@string/title_activity_office_visit" >
        </activity>
        <activity
            android:name="com.example.app.AddOfficeVisit"
            android:label="@string/title_activity_add_office_visit" >
        </activity>
        <activity
            android:name="com.example.app.OfficeInfo"
            android:label="@string/title_activity_office_info" >
        </activity>
        <activity
            android:name="com.example.app.AddOfficeInfo"
            android:label="@string/title_activity_add_office_info" >
        </activity>
        <activity
            android:name="com.example.app.OfficeInfoExtraDetails"
            android:label="@string/title_activity_office_info_extra_details" >
        </activity>
        <activity android:name="com.example.app.AddPerson" >
        </activity>
        <activity
            android:name="com.example.app.RenamePerson"
            android:label="@string/title_activity_rename_person" >
        </activity>
        <activity
            android:name="com.example.app.Weight"
            android:label="@string/title_activity_weight" >
        </activity>
        <activity
            android:name="com.example.app.WeightInfo"
            android:label="@string/title_activity_weight_info" >
        </activity>
        <activity
            android:name="com.example.app.BloodPressure"
            android:label="@string/title_activity_blood_pressure" >
        </activity>
        <activity
            android:name="com.example.app.AddBloodPressureInfo"
            android:label="@string/title_activity_add_blood_pressure_info" >
        </activity>
        <activity
            android:name="com.example.app.BloodSugarCharts"
            android:label="@string/title_activity_blood_sugar_charts" >
        </activity>
        <activity
            android:name="com.example.app.BloodSugar"
            android:label="@string/title_activity_blood_sugar" >
        </activity>
        <activity
            android:name="com.example.app.AddHistory"
            android:label="@string/title_activity_add_history" >
        </activity>
        <activity
            android:name="com.example.app.LabAndTestResults"
            android:label="@string/title_activity_lab_and_test_results" >
        </activity>
        <activity
            android:name="com.example.app.AddLabTestResult"
            android:label="@string/title_activity_add_lab_test_result" >
        </activity>
        <activity android:name="com.example.app.DataAnalysis" >
        </activity>
        <activity
            android:name="com.example.app.Welcome"
            android:label="@string/title_activity_welcome" >
        </activity>
        <activity
            android:name="com.example.app.SimpleListActivity"
            android:label="@string/title_activity_simple_list" >
        </activity>
        <activity
            android:name="com.example.app.WeightCharts"
            android:label="@string/title_activity_weight_charts" >
        </activity>
        <activity
            android:name="com.example.app.BMICharts"
            android:label="@string/title_activity_bmicharts" >
        </activity>
        <activity
            android:name="com.example.app.BloodSugarChart2"
            android:label="@string/title_activity_blood_sugar_chart2" >
        </activity>
        <activity android:name="org.achartengine.GraphicalActivity" />
        <activity
            android:name="com.example.app.BloodPressureCharts"
            android:label="@string/title_activity_blood_pressure_charts" >
        </activity>
        <activity
            android:name="com.example.app.Appointment"
            android:label="@string/title_activity_appointment" >
        </activity>
        <activity
            android:name="com.example.app.AddBloodSugarInfo"
            android:label="@string/title_activity_add_blood_sugar_info" >
        </activity>
        <activity
            android:name="com.example.app.AddAppointment"
            android:label="@string/title_activity_add_appointment" >
        </activity>
        <activity
            android:name="com.example.app.Registration1"
            android:label="@string/title_activity_registration1" >
        </activity>
        <activity
            android:name="com.example.app.MainActivity"
            android:label="@string/title_activity_login_activity1" >
        </activity>
        <activity
            android:name="com.example.app.Registration2"
            android:label="@string/title_activity_registration2" >
        </activity>
    </application>

</manifest>


我找不到错误..每次显示用户名密码不匹配..请帮助

您也可以使用共享首选项来保存和检索数据,而不是使用数据库。 存储像登录数据这样的简单名称-值对更简单

保存数据:

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String name=sharedpreferences.getString(Name, "")
检索数据:

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String name=sharedpreferences.getString(Name, "")
查看这些以了解更多详细信息。。

检查光标。moveToFirst()大于光标计数。如果返回true,则db record exists else not exists

运行调试器,检查您在注册表中使用的用户名和密码是否与保存在数据库中的用户名和密码相同。这边你会知道这是否是注册或登录上的问题。事实上,早些时候我做了相同的事情…它正在运行..但突然我得到了这个。我找不到我的错误。我得到了空指针异常当空指针异常发生时发布日志事实上早些时候我做了相同的事情…它正在运行..但突然我得到了这是。我找不到我的错误。我得到空指针异常非共享首选项…我发布的上述代码ID您找到解决此问题的方法了吗?