-3

可能重复:
尝试访问 SQLite 数据库时出现 java.lang.NullPointerException

我无法让我的登录屏幕正常工作。用户应输入他们的登录名和密码,然后将其与包含先前注册的数据库进行比较。有人可以告诉我哪里出错了,为什么代码不起作用?

**Login.java**

    package com.B00512756.angertwo;



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


    public class Login extends Activity implements OnClickListener{
        public static final String DATABASE_NAME = "login_database.db";
        public static final String USER_INFO_TABLE = "user_information";
        public static final String COLUMN_ID = "UserID";
        public static final String COLUMN_RATING = "UserName";
        public static final String COLUMN_NAME = "Password";








        public EditText txtUserName;
        public EditText txtPassword;
        public static Button btnLogin;
        public static Button btnCancel;



            /** Called when the activity is first created. */
            @SuppressWarnings("unused")
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.loginpage);


                txtUserName=(EditText)this.findViewById(R.id.txtUname);
                txtPassword=(EditText)this.findViewById(R.id.txtPwd);
                //btnLogin=(Button)this.findViewById(R.id.btnLogin);
                Button Login = (Button) findViewById(R.id.btnLogin);
                Button Register = (Button) findViewById(R.id.btnregister);
                Button Cancel = (Button) findViewById(R.id.btnCancel);



                Register.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        Intent myIntent = new Intent(view.getContext(), Registration.class);
                        startActivityForResult(myIntent, 0); 
                        finish();
                    }
                });


                Cancel.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });

                Login.setOnClickListener(new View.OnClickListener() {

                    @SuppressWarnings("null")
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        String name = txtUserName.getText().toString();
                        String pwd = txtPassword.getText().toString();
                        SQLiteDatabase regDB = null;



                        //String[] columns = new String[]{COLUMN_ID, COLUMN_RATING, COLUMN_NAME };

                        try {
                        Cursor c = regDB.query(USER_INFO_TABLE, new String[] {
                                COLUMN_ID, COLUMN_RATING, COLUMN_NAME}, null, null, null, null, null);
                        if(null!=c){

                              c.moveToFirst();

                             System.out.println("Cursor Size"+c.getCount());

                            }

                        String result = "";

                        int iRow = c.getColumnIndex(COLUMN_ID);
                        int iRating = c.getColumnIndex(COLUMN_RATING);
                        int iName = c.getColumnIndex(COLUMN_NAME);

                        c.moveToLast();
                        for (int i = c.getCount() - 1; i >= 0; i--) {

                            // Get the data
                            result = result + c.getString(iRow) + " " + c.getString(iRating) + " " + c.getString(iName) + "\n" ;
                            if("select * from USER_INFO_TABLE where UserName =" + "\""+ name + "\""+" and Password="+ "\""+ pwd != null );
                            { Intent j = new Intent();
                            j.setClassName("com.B00512756.angertwo",
                                    "com.B00512756.angertwo.AngerprototypetwoActivity");

                            startActivity(j);}

                            // Move the cursor
                            c.moveToPrevious();
                        }
                        c.close();
                        }
                            catch (Exception e) 
                            {

                            }

                        }





                    });

                }






            private SQLiteDatabase getWritableDatabase() {
                // TODO Auto-generated method stub
                return null;
            }






            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub


            }
    }

**AndroidMainfest:**

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.B00512756.angertwo"
      android:versionCode="1"
      android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="8" />

    <application android:name=".AppState" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Login"
                  android:label="@string/main_title">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".Login" android:label="@string/begin_label"
       android:theme="@android:style/Theme"></activity> 
      <activity android:name=".Question1" android:label="@string/question_one"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".Question2" android:label="@string/question_two"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".Question3" android:label="@string/question_three"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".Question4" android:label="@string/question_four"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".Question5" android:label="@string/question_five"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".AngerprototypetwoActivity" android:label="@string/main_title"
       android:theme="@android:style/Theme"></activity> 
      <activity android:name=".nextQ1" android:id="@+id/next_Q1_button"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".Strategies" android:label="@string/strategies_label"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".Contact" android:label="@string/begin_label"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".Strat_What_Is_Anger" android:label="@string/strategies_label_what_is_anger"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".Strat_Use_Distraction" android:label="@string/strategies_label_distraction"
       android:theme="@android:style/Theme"></activity>
      <activity android:name=".Registration" android:label="@string/registration"
       android:theme="@android:style/Theme"></activity>

    </application>

</manifest>
4

1 回答 1

0

你永远不会打开你的数据库(从代码来看,它看起来不像是你创建的),所以当你尝试运行查询时,它会返回 null。

我建议您了解有关数据库的更多信息,以及如何创建和使用它们...

教程

并了解更多有关 StackOverflow 的信息......一遍又一遍地发布相同的问题是不礼貌的(并且会很快解决您的问题)。

StackOverflow 常见问题解答

于 2012-05-19T20:32:40.720 回答