Android unfortunately has stopped hatası
Android studio da sql kullanarak bir proje yapmaya çalışıyorum ama ilk ekranda butona tıkladığımda unfortunately has stopped hatası alıyorum.
package com.example.companytodo;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
public class login extends AppCompatActivity {
DatabaseHelper dbHelper;
Context context = this;
EditText name, pass;
Button buttonLog;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_in);
dbHelper = new DatabaseHelper(context);
try {
dbHelper.createDataBase();
dbHelper.openDataBase();
} catch (IOException e) {
e.printStackTrace();
}
name = (EditText) findViewById(R.id.editTextName);
pass = (EditText) findViewById(R.id.editTextPass);
buttonLog = (Button) findViewById(R.id.buttonLog);
buttonLog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
cursor = dbHelper.getDatabase().query("register", new String[]{ "id","isAdmin","name","surname","username","password" },
"username = ?, password=? ", new String[]{String.valueOf(name), String.valueOf(pass)},
null, null, null);
if (name.getText().toString().equals("") || pass.getText().toString().equals("")) {
Toast.makeText(getApplicationContext(), "Username and Password can't be empty", Toast.LENGTH_LONG).show();
return;
}
if (cursor != null) {
if (cursor.getCount() > 0) {
Toast.makeText(login.this, "Logged In succesfully!", Toast.LENGTH_LONG).show();
if (cursor.getColumnIndex("isAdmin") == 0) {
Intent intent = new Intent(login.this, userToDo.class);
startActivity(intent);
} else {
Intent intent = new Intent(login.this, adminToDo.class);
startActivity(intent);
}
} else {
Toast.makeText(login.this, "Invalid username or password!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
package com.example.companytodo;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
@SuppressLint("SdCardPath")
private static final String DB_PATH = "/data/data/com.example.companytodo/databases/";
private static final String DB_NAME = "database";
private SQLiteDatabase sqLiteDatabase;
private Context mContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist){
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Veritabanı kopyalanamadı");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public SQLiteDatabase getDatabase(){
return sqLiteDatabase;
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
sqLiteDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (sqLiteDatabase != null)
sqLiteDatabase.close();
super.close();
}
}
Tesekkur ederim ben de oyle dusundum