Note that there are some explanatory texts on larger screens.

plurals
  1. POButton when clicked application got Crash in android fragment
    primarykey
    data
    text
    <p>I have fragment class that use button, the button have to login the user if exist or have to show text user not valid. I have use sqlite database. But the problem is when i click on the login button my application got crash. Here is the attach code of Frag</p> <pre><code>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; } </code></pre> <p>My Db class "LoginDataBaseAdapter" i have one function that check the password from where we have passed the username. Here is the code </p> <pre><code> public String getSinlgeEntry(String userName) { Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null); if (cursor.getCount() &lt; 1) // UserName Not Exist { cursor.close(); return "NOT EXIST"; } cursor.moveToFirst(); String password = cursor.getString(cursor.getColumnIndex("PASSWORD")); cursor.close(); return password; } </code></pre> <p>Now i cant figure it out that where i am missing. When i click the login button my application gets crash. Thank you in advance.</p> <p>my logcat:</p> <pre><code> 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) </code></pre> <p>My LoginDataBaseAdapter class code:</p> <pre><code>package com.example.betatestregister; </code></pre> <p>/** * Created by Rohan on 24/05/13. */</p> <pre><code>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() &lt; 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}); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload