Void cannot be converted to string [duplicate] - android

This question already has an answer here:
What does "Incompatible types: void cannot be converted to ..." mean?
(1 answer)
Closed 4 years ago.
Im trying to create a simple login for my mobile app but im getiing stock with an error :
Login.java
public void onButtonClick(View v) {
if (v.getId() == R.id.BLogin) {
EditText a = (EditText) findViewById(R.id.TFusername);
String str = a.getText().toString();
EditText b = (EditText) findViewById(R.id.TFpassword);
String pass = b.getText().toString();
String password = helper.searchPass(str);
if (pass.equals(password)) {
Intent i = new Intent(LogIn.this, Display.class);
i.putExtra("Username", str);
startActivity(i);
} else {
Toast temp = Toast.makeText(LogIn.this, "Username and password don't match!", Toast.LENGTH_SHORT);
temp.show();
}
}
if (v.getId() == R.id.BSignup) {
Intent i = new Intent(LogIn.this, Signup.class);
startActivity(i);
}
}
and the DatabaseHandler
public void searchPass(String uname)
{
db = this.getReadableDatabase();
String query = " select uname, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
do {
a = cursor.getString(0);
if(a.equals(uname))
{
b = cursor.getString(1);
break;
}
}
while (cursor.moveToNext());
}
return b;
}
Im getting stuck with the error:
Error:(32, 48) error: incompatible types: void cannot be converted to String
String password = helper.searchPass(str);
anyone know what im missing?

Replace return type as String in your method searchPass
public String searchPass(String uname)
{
db = this.getReadableDatabase();
String query = " select uname, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
do {
a = cursor.getString(0);
if(a.equals(uname))
{
b = cursor.getString(1);
break;
}
}
while (cursor.moveToNext());
}
return b;
}

Related

Sqlite Database and Radio Buttons in Android Studio

UI for this
User interface
and database
Sqlite Database
In my Code I want to compare selected Radio button is equal to the account type in database.
If they equal it must start activity as stated in the if statement.
I hardcoded my account type and it works but now i want to grab the account type from database and compare it with the string of selected radio button
//Button To submit selected Radio button for a specicific account
public void onClickSubmitAccount(){
radio_accounts = (RadioGroup) findViewById(R.id.radioAccounts);
btnSubmitAccount = (Button) findViewById(R.id.btnSubmitAccount);
btnSubmitAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int selected_account = radio_accounts.getCheckedRadioButtonId();
radio_button_accounts = (RadioButton) findViewById(selected_account);
Toast.makeText(UserHomePageActivity.this, radio_button_accounts.getText().toString(), Toast.LENGTH_SHORT).show();
if ((radio_button_accounts.getText().toString()).equals("Savings Account")) {
Intent accountIntent = new Intent(UserHomePageActivity.this, SavingsTransactionsActivity.class);
startActivity(accountIntent);
}
else if ((radio_button_accounts.getText().toString()).equals("Credit Account")) {
Intent accountIntent = new Intent(UserHomePageActivity.this, CreditTransactionActivity.class);
startActivity(accountIntent);
}
else if ((radio_button_accounts.getText().toString()).equals("Cheque Account")) {
Intent accountIntent = new Intent(UserHomePageActivity.this, ChequeTransactionActivity.class);
startActivity(accountIntent);
}
else {
Toast.makeText(UserHomePageActivity.this, "Sorry, No accout has been selected", Toast.LENGTH_LONG).show();
}
}
});
}
//Retrieve Data from DataBase in Table Account
public ArrayList getAllAccount()
{
ArrayList<AccountClass> accountList = new ArrayList<AccountClass>();
Cursor cursor = bankingAppDB.query(Constants.tblAccount, null,null,null,null,null,null);
AccountClass account;
if(cursor.moveToFirst()){
while (cursor.moveToNext())
{
String acc_number = cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_NUMBER));
String account_type = cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_TYPE));
double balance = Double.parseDouble(cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_BALANCE)));
Log.d(" Info"," Account Number: " +acc_number+" type: "+account_type);
account = new AccountClass(acc_number);
accountList.add(account);
}
}
return accountList;
}
//Adding Account number, Account Type and Balance to Account Table
public void addAccount(AccountClass account){
ContentValues values = new ContentValues();
values.put(Constants.ACCOUNT_NUMBER, account.getAccountNumber());
values.put(Constants.ACCOUNT_BALANCE, account.getAccountBalance());
values.put(Constants.ACCOUNT_TYPE, account.getAccountType());
bankingAppDB.insert(Constants.tblAccount, null, values);
bankingAppDB.close();
}
First of all your AccountClass is only receiving the account number attribute, there for you are not storing the rest of the values in your object.
Change your constructor to get all the 3 attributes
account = new AccountClass(acc_number, account_type, balance);
Secondly, you should have a method that gets an Account by it's number
public List<AccountClass> getAccountsByNumber(String number){
ArrayList<AccountClass> accountList = new ArrayList<AccountClass>();
String query = "SELECT * FROM " + YOUR_TABLE + " WHERE " + ACCOUNT_NUMBER + " = " + number;
Cursor cursor = bankingAppDB.query(query, null,null,null,null,null,null);
if(cursor.moveToFirst()){
while (cursor.moveToNext())
{
String acc_number = cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_NUMBER));
String account_type = cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_TYPE));
double balance = Double.parseDouble(cursor.getString(cursor.getColumnIndex(Constants.ACCOUNT_BALANCE)));
Log.d(" Info"," Account Number: " +acc_number+" type: "+account_type);
AcountClass account = new AccountClass(number,account_type,balance);
accountList.add(account);
}
}
return accountList;
}
After that, assuming you know your user id, on your onClick method call
List<AccountClass> accountList = bankingAppDB.getAccountsByNumber(NUMBER_OF_THE_ACCOUNT);
and then
for(AccountClass a : accountList){
if ((radio_button_accounts.getText().toString()).equals(a.getAccountType()) && a.getAccountType().trim().equals("Savings Account")) {
Intent accountIntent = new Intent(UserHomePageActivity.this, SavingsTransactionsActivity.class);
startActivity(accountIntent);
}
else if ((radio_button_accounts.getText().toString()).equals(a.getAccountType())&& a.getAccountType().trim().equals("Credit Account")) {
Intent accountIntent = new Intent(UserHomePageActivity.this, CreditTransactionActivity.class);
startActivity(accountIntent);
}
else if ((radio_button_accounts.getText().toString()).equals(a.getAccountType())&& a.getAccountType().trim().equals("Cheque Account")) {
Intent accountIntent = new Intent(UserHomePageActivity.this, ChequeTransactionActivity.class);
startActivity(accountIntent);
}
else {
Toast.makeText(UserHomePageActivity.this, "Sorry, No accout has been selected", Toast.LENGTH_LONG).show();
}
}

Retrive my database values from my database and then using them on a programmatic SMS message?

I'm confused and cannot figure out how I can send an SMS message using values stored on my database.
The SMS would appear like this: ('NAME'... Message content, etc..), the message would then be sent using the contact numbers entered by the user on the sqlite database.
Here's the code I've used to get the data during signup.
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, NAME text, C1 integer, C2 integer); ";
// 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, String name, String cn1, String cn2) {
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("NAME",name);
newValues.put("C1", cn1);
newValues.put("C2", cn2);
// 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 getSingleEntry(String userName) {
Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) { // username doesn't exist
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public boolean isExist (String userName) {
boolean exists;
Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if (cursor.getCount()>0) { // username exists
exists = true;
cursor.close();
return exists;
}
return false;
}
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});
}}
And here is the SignUpActivity
public class SignUpActivity extends AppCompatActivity {
Button bSignup;
TextView tvSign;
EditText etUN, etPW, etPW2, etFN, etC1, etC2;
LoginDataBaseAdapter loginDataBaseAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
loginDataBaseAdapter = new LoginDataBaseAdapter(this);
loginDataBaseAdapter = loginDataBaseAdapter.open();
bSignup = (Button)findViewById(R.id.bSignup);
tvSign = (TextView)findViewById(R.id.tvSign);
etUN = (EditText)findViewById(R.id.etUN);
etPW = (EditText)findViewById(R.id.etPW);
etPW2 = (EditText)findViewById(R.id.etPW2);
etFN = (EditText)findViewById(R.id.etFN);
etC1 = (EditText)findViewById(R.id.etC1);
etC2 = (EditText)findViewById(R.id.etC2);
bSignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String username = etUN.getText().toString();
String password = etPW.getText().toString();
String password2 = etPW2.getText().toString();
String name = etFN.getText().toString();
String c1 = etC1.getText().toString();
String c2 = etC2.getText().toString();
// check if fields are vacant
if (username.equals("") || password.equals("") || password2.equals("") || name.equals("")
|| c1.equals("")|| c2.equals("")) {
Toast.makeText(getApplicationContext(), "Incomplete Data", Toast.LENGTH_SHORT).show();
return;
}
// check if passwords 1 and 2 match
if (!password.equals(password2)) {
Toast.makeText(getApplicationContext(), "Passwords don't match. Please try again.", Toast.LENGTH_LONG).show();
return;
}
//check is username is still available for use
if (loginDataBaseAdapter.isExist(username)){
Toast.makeText(getApplicationContext(),"Username already taken. Please try again.", Toast.LENGTH_LONG).show();
return;
}
else {
// allow data to be saved in the database
loginDataBaseAdapter.insertEntry(username, password, name, c1, c2);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
});
tvSign.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
}
Once I'm logged in, how can I get those values ("i.e. NAME, C1, and C2") and send an SMS by pushing a button?
Update
I've used this on my LoginDataBaseAdapter.
public HashMap<String, String> getUserDetails(){
HashMap <String,String> user = new HashMap <String,String> ();
String selectQuery = "SELECT * FROM " + "LOGIN";
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount()>0){
user.put("USERNAME", cursor.getString(1));
user.put("PASSWORD", cursor.getString(2));
user.put("NAME", cursor.getString(3));
user.put("C1", cursor.getString(4));
user.put("C2", cursor.getString(5));
}
cursor.close();
db.close();
// return user
return user;
}
Then this code at my HomeActivity:
tvHello = (TextView)findViewById(R.id.tvHello);
HashMap <String, String> details = loginDataBaseAdapter.getUserDetails();
String name_text = details.get("NAME");
tvHello.setText("Welcome " + name_text);
It seems that it can only get the first entry and not the current entry for the current user. Any ideas to fix this issue? Thank you very much.
Managed to get it right. So I'll answer my own question.
Create a editText area wherein you'll enter your name to retrieve. Then use this code to retrieve it
public String getData(String verif) {
Cursor cursor = db.query("LOGIN", null, " USERNAME=?", new String[]{verif}, null, null, null);
if(cursor.getCount()<1) {
cursor.close();
return "No records exist";
}
cursor.moveToFirst();
String get_name = cursor.getString(cursor.getColumnIndex("NAME"));
cursor.close();
return get_name;
}
Once retrieved, set the name in a TextView. Then convert it to string like so:
String myName = myTextView.getText().toString();
Then:
smsManager.sendTextMessage(number, null, "My name is "+ myName ,null,
null);
'number' is a String containing the contact number where you want to send your SMS

how to get the cursor data which is appended to another activity and split the appended data to get the Id of that row

I have a database where i have Unique id, Email id and Password. Im storing using SQlite database. I've to get the cursor which stores the result of the query, i have got the cloumn index of each cloumn, appended it using StringBuffer, but i dont know how to get those values in the other class? please help.
here is my code for Adapter class:
public String getData(String email,String pwd)
{
StringBuffer buffer=new StringBuffer();
SQLiteDatabase db =sciHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT _id FROM " + SciHelper.TABLE_NAME + " WHERE email=? AND password=?", new String[]{email, pwd});
while(cursor.moveToNext())
{
int index1=cursor.getColumnIndex(SciHelper.UID);
int index2=cursor.getColumnIndex(SciHelper.EMAIL);
int index3=cursor.getColumnIndex(SciHelper.PASSWORD);
String cid=cursor.getString(index1);
String mail=cursor.getString(index2);
String mailpass=cursor.getString(index3);
buffer.append(cid +" "+mail+" "+mailpass+"\n");
}
return buffer.toString();
}
code at login class:
public void checkTable(View view) {
email2 = emaillog.getText().toString();
pass2 = passlog.getText().toString();
String data = sciDataBaseAdapter.getData(email2, pass2);
String[] values=data.split("\\");
String cid=values(0);
if (TextUtils.isEmpty(email2)) {
emaillog.setError("Enter Email Id");
passlog.setError("Enter Password");
}
// if (email1 != null) {
Intent intent=new Intent(this,ResultActivity.class);
startActivity(intent);
Message.message(this, "Login succesful");
} else {
Message.message(this, "Invalid username/ or register");
}
}
}
When calling the ResultAcitvity
Intent intent=new Intent(this,ResultActivity.class);
intent.putExtra("key","value");
startActivity(intent);
and on ResultActivity, inside onCreate() method write
Intent intent=getIntent();
String value=intent.getStringExtra("key");
String[] values = value.split(" ");
String cid=values[0];
String mail=values[1];
String mailpass=values[2];
public void checkTable(View view) {
email2 = emaillog.getText().toString();
pass2 = passlog.getText().toString();
String data = sciDataBaseAdapter.getData(email2, pass2);
String[] values=data.split("\\");
String cid=values(0);
if (TextUtils.isEmpty(email2)) {
emaillog.setError("Enter Email Id");
passlog.setError("Enter Password");
}
// if (email1 != null) {
Bundle bundle=new Bundle();
b.putStringArray(some_key, values);
Intent intent=new Intent(this,ResultActivity.class);
intent.putExtra(bundle);
startActivity(intent);
Message.message(this, "Login succesful");
} else {
Message.message(this, "Invalid username/ or register");
}
}
and in your next activity get that using.
Bundle extras = getIntent().getExtras();
String[] some_variable= extras.getString("some_key");

Update Password using SQLite

I am developing an application using SQLite. My requirement is that I want to update a password in my table.
I wrote an update query but it is not working. Error:
android.database.sqlite.SQLiteException: unrecognized token: "' WHERE username = ?": ,
while compiling: UPDATE TABLE_USER SET password = god' WHERE username = ?` condition.
The code:
UserDaoImpl.java
public void updateEntry(String newPassword,String name)
{
Cursor cur= myDB.rawQuery("UPDATE "+UserDBHandler.USER_TABLE+" SET
"+UserDBHandler.PASSWORD+" = "+newPassword+"' WHERE "+
UserDBHandler.USER_NAME+" = ?",new String[]{name});
}
ChangePasswordActivity.java
public class ChngePasswordActivity extends Activity {
private EditText oldPwdEdit;
private EditText newPwdEdit;
private EditText cnfrmEdit;
private Button submitBtn;
private String oldPwd;
private String newPwd;
private String cnfrmPwd;
private UserDaoImpl userDetalsIml;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_changepassword);
//Bundle extra = this.getIntent().getExtras();
//String name= extra.getString("udp");
Intent intent2=getIntent();
final String name1=intent2.getStringExtra("val");
Log.e("prasad2",name1);
oldPwdEdit = (EditText)findViewById(R.id.oldEdt);
newPwdEdit = (EditText)findViewById(R.id.newpswEdt);
cnfrmEdit = (EditText)findViewById(R.id.pswEdt);
submitBtn = (Button)findViewById(R.id.cnfrmPwdBtn);
submitBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
oldPwd = oldPwdEdit.getText().toString().trim();
newPwd = newPwdEdit.getText().toString().trim();
cnfrmPwd=cnfrmEdit.getText().toString().trim();
if(oldPwd==null||"".equalsIgnoreCase(oldPwd)){
String header = "OLD PASSWORD REQUIRE";
Toast.makeText(getApplicationContext(),header,100).show();
}
else if(newPwd==null ||"".equalsIgnoreCase(newPwd)){
String header = "NEW PASSWORD IS REQUIRE";
Toast.makeText(getApplicationContext(), header,
100).show();
}
else if(cnfrmPwd==null ||"".equalsIgnoreCase(cnfrmPwd)){
String header = "COINFIRM PASSWORD IS REQUIRE";
Toast.makeText(getApplicationContext(), header,
100).show();
}
else if(!newPwd.equalsIgnoreCase(cnfrmPwd)){
String header = "PASSWORD DOES NOT MATCH";
Toast.makeText(getApplicationContext(), header,
100).show();
}
else{
userDetalsIml = new
UserDaoImpl(getApplicationContext());
userDetalsIml.updateEntry(newPwd,name1);
}
}
});
}
}
You are missing a ' in your query at here UserDBHandler.PASSWORD+" = "
Try this updated one
Cursor cur= myDB.rawQuery("UPDATE "+UserDBHandler.USER_TABLE+" SET
"+UserDBHandler.PASSWORD+" = '"+newPassword+"' WHERE "+
UserDBHandler.USER_NAME+" = ?",new String[]{name});
Ooops!! you have done a very little mistakes.
Please replece your code
Cursor cur= myDB.rawQuery("UPDATE "+UserDBHandler.USER_TABLE+" SET
"+UserDBHandler.PASSWORD+" = "+newPassword+"' WHERE "+
UserDBHandler.USER_NAME+" = ?",new String[]{name});
by
Cursor cur= myDB.rawQuery("UPDATE "+UserDBHandler.USER_TABLE+" SET
"+UserDBHandler.PASSWORD+" = '"+newPassword+"' WHERE "+
UserDBHandler.USER_NAME+" = ?",new String[]{name});
There is only missing ' before newPassword .

SQL query and force close challenge

here i tried to take name and password from database if the user have already an account, or sign up him by enter his data.
This is the first activity which has force close by clicking on first button to log into the data base
public class SelesMeter2Activity extends Activity implements OnClickListener {
EditText ed1;
EditText ed2;
Button b1;
Button b2;
SQLiteDatabase sql;
Cursor c;
Intent in;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.ed1);
ed2 = (EditText) findViewById(R.id.ed2);
b1 = (Button) findViewById(R.id.bt1);
b2 = (Button) findViewById(R.id.bt2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
sql = openOrCreateDatabase("db", 0, null);
sql.execSQL("CREATE TABLE if not exists "
+ "Employee2 (password integer NOT NULL PRIMARY KEY,name text NOT NULL)");
}
#Override
public void onClick(View arg0) {
// log in
if (arg0.getId() == R.id.bt1) {
int p = 0;
String name = ed1.getText().toString();
String sp = ed2.getText().toString();
try {
// Attempt to parse the number as an integer
p = Integer.parseInt(sp);
} catch (NumberFormatException nfe) {
// parseInt failed, so tell the user it's not a number
Toast.makeText(this,
"Sorry, " + sp + " is not a number. Please try again.",
Toast.LENGTH_LONG).show();
}
if (c.getCount() != 0) {
c = sql.rawQuery("select * from Employee", null);
while (c.moveToNext()) {
if (name.equals("c.getString(1)") && p == c.getInt(0)) {
in = new Intent(this, secondview.class);
startActivity(in);
break;
}
}
}
else {
Toast.makeText(this,
"please sign up first or enter " + "correct data", 2000)
.show();
}
} else if (arg0.getId() == R.id.bt2) {
// sign up
Intent in2 = new Intent(this, signup.class);
startActivity(in2);
}
}
}
the second class that enter the new user which is not working as expected,
the toast does not work :
public class signup extends Activity implements OnClickListener {
EditText e1;
EditText e2;
SQLiteDatabase sql;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.singupx);
Intent in = getIntent();
e1 = (EditText) findViewById(R.id.ed1s);
e2 = (EditText) findViewById(R.id.ed2s);
b = (Button) findViewById(R.id.bt1s);
b.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String n = e1.getText().toString();
String sp = e2.getText().toString();
try {
// Attempt to parse the number as an integer
int p = Integer.parseInt(sp);
// This insertion will *only* execute if the parseInt was successful
// sql.execSQL("insert into Employee2(password,name)values('"+n+"',"+p+")");
ContentValues values = new ContentValues();
values.put("password", p);
values.put("name", n);
sql.insert("Employee2", null, values);
Toast.makeText(this, "Data inserted", Toast.LENGTH_LONG).show();
Intent in2 = new Intent(this, secondview.class);
startActivity(in2);
} catch (NumberFormatException nfe) {
// parseInt failed, so tell the user it's not a number
Toast.makeText(this,
"Sorry, " + sp + " is not a number. Please try again.",
Toast.LENGTH_LONG).show();
}
}
}
In the SelesMeter2Activity you'll have a NullPointerException at the line:
if (c.getCount() != 0) {
as you don't initialize the Cursor before that line. Move the query before the above line:
c = sql.rawQuery("select * from Employee", null);
if (c.getCount() != 0) {
// ...
You should post the exception you get from the logcat.
Also regarding your signup activity please don't instantiate the first activity to access fields from it. Open the database again in the second activity and insert the values.
This is why you are getting error
// you are calling the `c.getCount();` before you are assigning
// It will throw null pointer exception
if (c.getCount() != 0) {
c = sql.rawQuery("select * from Employee", null);
while (c.moveToNext()) {
if (name.equals(c.getString(1)) && p == c.getInt(0)) {
in = new Intent(this, secondview.class);
startActivity(in);
break;
}
}
}
Change the logic like
c = sql.rawQuery("select * from Employee", null);
c.moveToFirst();
if(!c.isAfterLast()) {
do {
if (name.equals(c.getString(1)) && p == c.getInt(0)) {
in = new Intent(this, secondview.class);
startActivity(in);
break;
}
} while (c.moveToNext());
}
and name.equals("c.getString(1)") should be name.equals(c.getString(1))
EDIT
Example of insert method
ContentValues values = new ContentValues();
values.put("password", n);
values.put("name", p);
database.insert("Employee2", null, values);

Categories

Resources