2012-05-19 68 views
-3

可能重複:
java.lang.NullPointerException when trying to access SQLite database登錄頁面閱讀細節不起作用

我不能讓我的登錄屏幕工作。用戶應輸入他們的登錄名和密碼,然後與包含以前註冊的數據庫進行比較。有人可以告訴我我哪裏出錯了,爲什麼代碼不工作?

**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> 

回答

0

你永遠不打開數據庫(和它的代碼並不像你甚至創建一個),所以當你嘗試運行一個查詢返回null。

我建議你瞭解更多有關數據庫,如何創建和使用它們...

Tutorial

並學習一些關於StackOverflow的......這是不好的方式,以保持張貼同樣的問題一遍又一遍(並會很快關閉你的問題)。

StackOverflow FAQ

+0

非常感謝你,我會做這個,後來發佈更新。 Muchos gracias再次amigo :) – Richy