2013-05-27 33 views
0

我有片段类使用按钮,该按钮必须登录用户,如果存在或必须显示文本用户无效。我有使用sqlite数据库。但问题是当我点击登录按钮我的应用程序崩溃。 以下是破片按钮被点击的应用程序得到崩溃在Android片段

public class FirstFragment extends Fragment { 

LoginDataBaseAdapter loginDataBaseAdapter; 
String userName; 
EditText testUser; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.firstfragment,container,false); 

    // create a instance of SQLite Database 
    loginDataBaseAdapter = new LoginDataBaseAdapter(getActivity()); 
    loginDataBaseAdapter = loginDataBaseAdapter.open(); 


    //get the reference of the design 


    final testUser = (EditText) view.findViewById(R.id.editTextUserNameToLogin); 
    final EditText testPassword = (EditText) view.findViewById(R.id.editTextPasswordToLogin); 
    final Button btnLogin = (Button) view.findViewById(R.id.buttonSignIn); 
    final Button btnCreate=(Button)view.findViewById(R.id.buttonCreateAccount); 

    //set OnClick Listener on login button 

    btnLogin.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      userName=testUser.getText().toString(); 

       EditText testPassword = (EditText) getActivity().findViewById(R.id.editTextPasswordToLogin); 
       String password = testPassword.getText().toString(); 


      //fetch the password from the database for respective user name 

      String storedPassword = loginDataBaseAdapter.getSinlgeEntry(userName); 
      Toast.makeText(getActivity(),storedPassword,1).show(); 

      // check if the Stored password matches with Password entered by user 
      if (password.equals(storedPassword)) { 
       Toast.makeText(getActivity(), "Congrats:Login Sucessfull", Toast.LENGTH_LONG).show(); 

      } else { 
      Toast.makeText(getActivity(), "User Name or Password does not match", Toast.LENGTH_LONG).show(); 
      } 




     } 
    }); 
return view; 
} 

我的DB类“LoginDataBaseAdapter”的附加代码我有一个功能检查的地方,我们已经通过的用户名密码。这里是代码

public String getSinlgeEntry(String userName) { 
    Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null); 
    if (cursor.getCount() < 1) // UserName Not Exist 
    { 
     cursor.close(); 
     return "NOT EXIST"; 
    } 
    cursor.moveToFirst(); 
    String password = cursor.getString(cursor.getColumnIndex("PASSWORD")); 
    cursor.close(); 
    return password; 
} 

现在我无法弄清楚,我失踪了。当我点击登录按钮时,我的应用程序会崩溃。 预先感谢您。

我的logcat:

java.lang.NullPointerException 
     at com.example.betatestregister.LoginDataBaseAdapter.getSinlgeEntry(LoginDataBaseAdapter.java:68) 
     at com.example.betatestregister.FirstFragment$1.onClick(FirstFragment.java:52) 
     at android.view.View.performClick(View.java:4204) 
     at android.view.View$PerformClick.run(View.java:17355) 
     at android.os.Handler.handleCallback(Handler.java:725) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5041) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
     at dalvik.system.NativeStart.main(Native Method) 

我LoginDataBaseAdapter类代码:

package com.example.betatestregister; 

/** * 通过创建罗汉上24/05/13。 */

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 

import android.widget.Toast; 




public class LoginDataBaseAdapter { 
static final String DATABASE_NAME = "login.db"; 
static final int DATABASE_VERSION = 1; 
public static final int NAME_COLUMN = 1; 
// TODO: Create public field for each column in your table. 
// SQL Statement to create a new database. 
static final String DATABASE_CREATE = "create table " + "LOGIN" + 
     "(" + "ID" + " integer primary key autoincrement," + "USERNAME text,PASSWORD 
text); "; 
// Variable to hold the database instance 
public SQLiteDatabase db; 
// Context of the application using the database. 
private final Context context; 
// Database open/upgrade helper 
private DataBaseHelper dbHelper; 

public LoginDataBaseAdapter(Context _context) { 
    context = _context; 
    dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

public LoginDataBaseAdapter open() throws SQLException { 

//  db = dbHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    db.close(); 
} 

public SQLiteDatabase getDatabaseInstance() { 
    return db; 
} 

public void insertEntry(String userName, String password) { 
    ContentValues newValues = new ContentValues(); 
    // Assign values for each row. 
    newValues.put("USERNAME", userName); 
    newValues.put("PASSWORD", password); 

    // Insert the row into your table 
    db.insert("LOGIN", null, newValues); 
    ///Toast.makeText(context, "Reminder Is Successfully Saved", 
Toast.LENGTH_LONG).show(); 
} 

public int deleteEntry(String UserName) { 
    //String id=String.valueOf(ID); 
    String where = "USERNAME=?"; 
    int numberOFEntriesDeleted = db.delete("LOGIN", where, new String[]{UserName}); 
    // Toast.makeText(context, "Number fo Entry Deleted Successfully : 
"+numberOFEntriesDeleted, Toast.LENGTH_LONG).show(); 
    return numberOFEntriesDeleted; 
} 

public String getSinlgeEntry(String userName) { 
    Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, 
null, null, null); 
    if (cursor.getCount() < 1) // UserName Not Exist 
    { 
     cursor.close(); 
     // Toast.makeText(context, "Number fo Entry Deleted Successfully : 
"+numberOFEntriesDeleted, Toast.LENGTH_LONG).show(); 
     return "NOT EXIST"; 
    } 
    cursor.moveToFirst(); 
    String password = cursor.getString(cursor.getColumnIndex("PASSWORD")); 
    cursor.close(); 
    return password; 
} 

public void updateEntry(String userName, String password) { 
    // Define the updated row content. 
    ContentValues updatedValues = new ContentValues(); 
    // Assign values for each row. 
    updatedValues.put("USERNAME", userName); 
    updatedValues.put("PASSWORD", password); 

    String where = "USERNAME = ?"; 
    db.update("LOGIN", updatedValues, where, new String[]{userName}); 
} 

} 
+2

请发布您的错误日志 –

+0

@Rohan Ale后用你的logcat输出行号..并显示它所在的行号例外。 – TheFlash

+0

请发布您的logcat ...你的数据库是空的,它似乎 – Pragnani

回答

1
loginDataBaseAdapter = loginDataBaseAdapter.open(); 

除去上面的代码。
只能使用

loginDataBaseAdapter.open(); 

你正在创建它的一个实例后,对矫正你的loginDataBaseAdapter。

+0

我收到的还是错误..... E/AndroidRuntime:致命异常:在com.example.betatestregister.LoginDataBaseAdapter.getSinlgeEntry(LoginDataBaseAdapter.java:68) 主要 显示java.lang.NullPointerException 在COM。 example.betatestregister.FirstFragment $ 1.onClick(FirstFragment.java:52) –

+0

检查游标是否为空 – vicky

1
public LoginDataBaseAdapter open() throws SQLException { 

//  db = dbHelper.getWritableDatabase(); 
    return this; 
} 

取消注释此行

//  db = dbHelper.getWritableDatabase(); 

此方法不起作用了,但应该得到数据库的可写实例。 它没有这样做,所以db是空的,你得到NPE。

+0

上述解决方案对我无效..... plz给我进一步的建议 –

1

在你的情况下,你得到一些返回null的游标。首先你必须检查它为什么是空的。但是,为了防止崩溃应用程序时,光标为空,你能做的财产以后这样的:

String storedPassword=""; 
    String checkPassowrd = loginDataBaseAdapter.getSinlgeEntry(userName); 

    if(checkPassword!=null){ 

    storedPassword = checkPassword; 

    } 

,我建议您设置getSingleEntry方法是这样的:

public String getSinlgeEntry(String userName) { 
    Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null); 
    if (cursor.getCount() < 1) // UserName Not Exist 
      { 
      cursor.close(); 
       return "NOT EXIST"; 
     }else{ 
     cursor.moveToFirst(); 
     String password = cursor.getString(cursor.getColumnIndex("PASSWORD")); 
     cursor.close(); 
     return password; 
     } 
    } 

什么让我感到愤怒,你真的想让你的光标在第一个条目上吗?没有cursor.MoveToNext()?

相关问题