I'm working on a mobile app on Android Studio for a university project. However, I'm not able to modify the table name in my CRUD, and if I do, it will cause the app to crash.
Here is my code:
AdminMathsActivity.java:
package com.example.schoolapp.Admin;
import android.app.AlertDialog;
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 com.example.schoolapp.R;
public class AdminMathsActivity extends AppCompatActivity {
DataBaseHelper2 peopleDB;
Button btnAddData, btnViewData,btnUpdateData,btnDelete;
EditText etName,etEmail,etTVShow,etID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_maths);
peopleDB = new DataBaseHelper2(this);
etID = (EditText) findViewById(R.id.etID);
etName = (EditText) findViewById(R.id.etNewName);
etEmail = (EditText) findViewById(R.id.etNewEmail);
etTVShow = (EditText) findViewById(R.id.etNewTVShow);
btnAddData = (Button) findViewById(R.id.btnAddData);
btnViewData = (Button) findViewById(R.id.btnViewData);
btnUpdateData = (Button) findViewById(R.id.btnUpdateData);
btnDelete = (Button) findViewById(R.id.btnDelete);
AddData();
ViewData();
UpdateData();
DeleteData();
}
public void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
String tvShow = etTVShow.getText().toString();
boolean insertData = peopleDB.addData(name, email, tvShow);
if (insertData == true) {
Toast.makeText(AdminMathsActivity.this, "Vos données ont bien été insérez!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AdminMathsActivity.this, "Vos données n’ont pas été insérez, veuillez réessayer.", Toast.LENGTH_LONG).show();
}
}
});
}
public void ViewData(){
btnViewData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor data = peopleDB.showData();
if (data.getCount() == 0) {
display("Erreur", "Aucune données n'a été insérez.");
return;
}
StringBuffer buffer = new StringBuffer();
while (data.moveToNext()) {
buffer.append("ID Note: " + data.getString(0) + "\n");
buffer.append("ID Eleve: " + data.getString(1) + "\n");
buffer.append("Intitule: " + data.getString(2) + "\n");
buffer.append("Note: " + data.getString(3) + "\n");
display("Toutes les données insérez :", buffer.toString());
}
}
});
}
public void display(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void UpdateData(){
btnUpdateData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int temp = etID.getText().toString().length();
if (temp > 0) {
boolean update = peopleDB.updateData(etID.getText().toString(), etName.getText().toString(),
etEmail.getText().toString(), etTVShow.getText().toString());
if (update == true) {
Toast.makeText(AdminMathsActivity.this, "Vos données ont bien été modifiés!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AdminMathsActivity.this, "Vos données n’ont pas été modifiés, veuillez réessayer.", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(AdminMathsActivity.this, "Veuillez entrer un ID pour effectuer une modification!", Toast.LENGTH_LONG).show();
}
}
});
}
public void DeleteData(){
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int temp = etID.getText().toString().length();
if(temp > 0){
Integer deleteRow = peopleDB.deleteData(etID.getText().toString());
if(deleteRow > 0){
Toast.makeText(AdminMathsActivity.this, "Vos données ont bien été supprimé!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(AdminMathsActivity.this, "Vos données n’ont pas été supprimé, veuillez réessayer.", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(AdminMathsActivity.this, "Veuillez entrer un ID pour effectuer une suppression!", Toast.LENGTH_LONG).show();
}
}
});
}
}
DataBaseHelper2.java code:
package com.example.schoolapp.Admin;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper2 extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "people.db";
public static final String TABLE_NAME = "people_table";
public static final String COL1 = "ID";
public static final String COL2 = "NAME";
public static final String COL3 = "EMAIL";
public static final String COL4 = "TVSHOW";
public DataBaseHelper2(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
" NAME TEXT, EMAIL TEXT, TVSHOW TEXT)";
db.execSQL(createTable);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String name, String email, String tvShow){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2,name);
contentValues.put(COL3,email);
contentValues.put(COL4, tvShow);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
public Cursor showData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
public boolean updateData(String id, String name, String email, String tvShow){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL1,id);
contentValues.put(COL2,name);
contentValues.put(COL3,email);
contentValues.put(COL4,tvShow);
db.update(TABLE_NAME, contentValues, "ID = ?", new String[] {id});
return true;
}
public Integer deleteData(String id){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?", new String[] {id});
}
}
activity_admin_maths.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Admin.AdminMathsActivity">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnAddData"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="ID :"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/etID"
android:layout_width="391dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_alignEnd="#+id/btnUpdateData"
android:layout_alignRight="#+id/btnUpdateData"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_toRightOf="#+id/textView1"
android:hint="Utilisation pour modifier et supprimer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="ID de l'eleve"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etID" />
<EditText
android:id="#+id/etNewName"
android:layout_width="391dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView6"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_toRightOf="#+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Intitule"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etNewName" />
<EditText
android:id="#+id/etNewEmail"
android:layout_width="391dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Note"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etNewEmail" />
<EditText
android:id="#+id/etNewTVShow"
android:layout_width="391dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5" />
<Button
android:id="#+id/btnAddData"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="20dp"
android:text="Ajouter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etNewTVShow" />
<Button
android:id="#+id/btnViewData"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:layout_toEndOf="#+id/btnAddData"
android:layout_toRightOf="#+id/btnAddData"
android:text="Visualiser"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etNewTVShow" />
<Button
android:id="#+id/btnUpdateData"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_below="#+id/etNewEmail"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="4dp"
android:layout_toEndOf="#+id/btnViewData"
android:layout_toRightOf="#+id/btnViewData"
android:text="Modifier"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnAddData" />
<Button
android:id="#+id/btnDelete"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:text="Supprimer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btnViewData" />
</android.support.constraint.ConstraintLayout>
And here is the error log :
2019-03-01 15:41:51.277 10663-10663/com.example.schoolapp E/SQLiteLog: (1) no such table: people_table2
2019-03-01 15:41:51.282 10663-10663/com.example.schoolapp E/SQLiteDatabase: Error inserting EMAIL=Fonction TVSHOW=15/20 NAME=1
android.database.sqlite.SQLiteException: no such table: people_table2 (code 1 SQLITE_ERROR): , while compiling: INSERT INTO people_table2(EMAIL,TVSHOW,NAME) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
at com.example.schoolapp.Admin.DataBaseHelper2.addData(DataBaseHelper2.java:43)
at com.example.schoolapp.Admin.AdminMathsActivity$1.onClick(AdminMathsActivity.java:52)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Basically if I change the table name to people_table2 for example, data won't be inserted in SQLite.
Typically you would not modify a table name, that is unless you were modifying the structure and eventually renaming the renamed table back to it's original name.
However, you can use ALTER TABLE to rename a table. However, note that in doing so you would have to modify the App accordingly so that the changed table name is used throughout.
If you just want to change the table name used when the table is first created (simply change public static final String TABLE_NAME = "people_table"; to public static final String TABLE_NAME = "people_table2";), i.e. in the onCreate method, but after the App has been previously run using the older table name. Then the simplest way is to :-
Make the code changes,
Do 1 of the following
delete/clear the App's data or
uninstall the App
or in your case increase the database version (e.g. change super(context, DATABASE_NAME, null, 1); to super(context, DATABASE_NAME, null, 2);)
rerun the App.
Note that doing 2.1 or 2.2 above will lose any data that has been stored in the database. To preserve data is possible but is more complex. Using 2.3 will result in a new table being created with the old one remaining.
The reason is that the onCreate method only runs when the database itself is created.
Additional
As the syntax for the DROP TABLE statement is wrong. If you change the table name AND increase the version then the App will crash with an error like :-
2019-03-01 16:25:12.956 5963-5963/aaa.adminmaths E/AndroidRuntime: FATAL EXCEPTION: main
Process: aaa.adminmaths, PID: 5963
android.database.sqlite.SQLiteException: near "IF": syntax error (code 1 SQLITE_ERROR): , while compiling: DROP IF TABLE EXISTS people_table
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1769)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1698)
at aaa.adminmaths.DataBaseHelper2.onUpgrade(DataBaseHelper2.java:33)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:398)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
at aaa.adminmaths.DataBaseHelper2.showData(DataBaseHelper2.java:54)
at aaa.adminmaths.AdminMathsActivity$2.onClick(AdminMathsActivity.java:65)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
That is because the syntax should be DROP TABLE IF EXISTS the_table_name_to_drop
Related
While working on a school project that uses SQLite, I have been having a multidute of problems with it, but after getting most of them working, the main problem I am facing right not is twofold, that as far as I know, it is not seeing all of the content that is stored in the sql, getting four main errors: "SQLiteLog: (1) table customers has no column named international in "INSERT INTO customers(salary,skills,userType,age,name,international) VALUES (?,?,?,?,?,?)", "SQLiteDataBase: Error inserting salary=123 skills=testing,coding,stuff userType=0 age=18 name=Josh international=N android.database.sqlite.SQLiteException: table customers has no column named international (code 1 SQLITE_ERROR): , while compiling: INSERT INTO customers(salary,skills,userType,age,name,international) VALUES (?,?,?,?,?,?)", "SQLiteLog (1) no such table: security in "INSERT INTO security(username,password) VALUES (?,?)" and "SQLiteDatabase Error inserting username=Josher password=password
android.database.sqlite.SQLiteException: no such table: security (code 1 SQLITE_ERROR): , while compiling: INSERT INTO security(username,password) VALUES (?,?)". The even more confusing thing is that for some of the errors they list the very thing that the error says is missing, which I find even weirder. While there was a post about someone having a similar issue, not only is the code seem very different, but was happening while updating the database, which I haven't even attempting, and has an answer that I don't know how to apply to my current code. What exactly is causing these four error, as they seem to be connected, and have no idea what is wrong? Is there a problem with my code? Are you not allowed to have two tables at once with SQLite or something? The code below is the DBHelper,java code, and xml for the section that is having the issue.
DBHelper:
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "Customers.db";
public DBHelper(Context context){
super(context,DBNAME,null,1);
}
#Override
public void onCreate(SQLiteDatabase Dat) {
Dat.execSQL("create Table customers(name TEXT primary key, age INTEGER, skills TEXT,salary INTEGER, international TEXT,userType INTEGER)");
Dat.execSQL("create Table security(username TEXT primary key, password TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase Dat, int i, int i1) {
Dat.execSQL("drop Table if exists customers");
}
public Boolean insertData(String name, int age, String skills, int salary,String international, int userType){
SQLiteDatabase Dat = this.getWritableDatabase();
ContentValues content = new ContentValues();
content.put("name",name);
content.put("age",age);
content.put("skills",skills);
content.put("salary",salary);
content.put("international",international);
content.put("userType",userType);
long result = Dat.insert("customers",null,content);
if(result == -1){
return false;
}
else {
return true;
}
}
public Boolean insertSecurity(String username, String password){
SQLiteDatabase Dat = this.getWritableDatabase();
ContentValues content = new ContentValues();
content.put("username",username);
content.put("password",password);
long result = Dat.insert("security",null,content);
if(result == -1){
return false;
}
else {
return true;
}
}
Java file Second Fragment (main activity and first fragment have no issues as far as I know)
public class SecondFragment extends Fragment {
private FragmentSecondBinding binding;
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
binding = FragmentSecondBinding.inflate(inflater, container, false);
return binding.getRoot();
}
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
view.getId();
super.onViewCreated(view, savedInstanceState);
EditText StudentName = (EditText) view.findViewById(R.id.editTextStudentName);
EditText StudentsAge = (EditText) view.findViewById(R.id.editTextStudentAge);
EditText StudentSalary = (EditText) view.findViewById(R.id.editTextStudentSalary);
EditText StudentInternational = (EditText) view.findViewById(R.id.editTextStudentInternational);
EditText StudentUsername = (EditText) view.findViewById(R.id.editTextStudentUsername);
EditText StudentPassword = (EditText) view.findViewById(R.id.editTextStudentPassword);
EditText StudentSkills = (EditText) view.findViewById(R.id.editTextStudentSkills);
DBHelper Dat = new DBHelper(getContext());
binding.buttonBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NavHostFragment.findNavController(SecondFragment.this)
.navigate(R.id.action_SecondFragment_to_FirstFragment);
}
});
binding.buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = StudentName.getText().toString();
String age = StudentsAge.getText().toString();
String salary = StudentSalary.getText().toString();
int savedSalary = tryParse(salary);
String international = StudentInternational.getText().toString();
String user = StudentUsername.getText().toString();
String pass = StudentPassword.getText().toString();
String skills = StudentSkills.getText().toString();
int savedAge = tryParse(age);
if(name.equals("")|| age.equals("") || salary.equals("") || international.equals("") || user.equals("") || pass.equals("") || skills.equals("")) {
Toast.makeText(getContext(), "A field is empty", Toast.LENGTH_SHORT).show();
}
else if(savedSalary == -1 || savedAge == -1){
Toast.makeText(getContext(), "Salary and Age should be integers", Toast.LENGTH_SHORT).show();
}
else{
Boolean try1 = Dat.insertData(name,savedAge,skills,savedSalary,international,0);
Boolean try2 = Dat.insertSecurity(user, pass);
NavHostFragment.findNavController(SecondFragment.this)
.navigate(R.id.action_SecondFragment_to_FourthFragment);
}
}
});
}
public Integer tryParse(Object obj) {
Integer retVal;
try {
retVal = Integer.parseInt((String) obj);
} catch (NumberFormatException nfe) {
retVal = -1; // or null if that is your preference
}
return retVal;
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
XML code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondFragment">
<TextView
android:id="#+id/textview_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="Enter Student Info"
app:layout_constraintBottom_toTopOf="#id/button_back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="450dp"
android:text="#string/previous"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textview_second" />
<Button
android:id="#+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="450dp"
android:text="Next"
app:layout_constraintStart_toEndOf="#+id/button_back"
app:layout_constraintTop_toBottomOf="#+id/textview_second" />
<EditText
android:id="#+id/editTextStudentName"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="3dp"
android:ems="10"
android:hint="Student Name"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview_second" />
<EditText
android:id="#+id/editTextStudentSalary"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="start|top"
android:hint="Enter minimum accepted salary"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextStudentAge" />
<EditText
android:id="#+id/editTextStudentAge"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="1dp"
android:ems="10"
android:gravity="start|top"
android:hint="Enter Student Age"
android:inputType="textMultiLine"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextStudentName" />
<EditText
android:id="#+id/editTextStudentInternational"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="start|top"
android:hint="Enter if Internation student, use Y or N"
android:inputType="textMultiLine"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextStudentSalary" />
<EditText
android:id="#+id/editTextStudentUsername"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="5dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextStudentInternational" />
<EditText
android:id="#+id/editTextStudentPassword"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="5dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextStudentUsername" />
<EditText
android:id="#+id/editTextStudentSkills"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginTop="5dp"
android:ems="10"
android:gravity="start|top"
android:hint="Enter skills seperated by commas"
android:inputType="textMultiLine"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextStudentPassword" />
</androidx.constraintlayout.widget.ConstraintLayout>
Yeah, turns out that as #forpas said, the problem with the app was a matter of resetting the device. However, as I had no idea how to do that, or even that it was necessary, I kept on trying even though nothing worked, even though a better term would be "wiping" the app, as the way to do it seems to be the "wipe data" setting.
I am a newbie to android studio and have replicated sample code samples to fit my application. The purpose is to read all my company records. I have tried a couple variations without success. The error I get indicates Customers() can not be applied to: with a list of the fields beneath. I have 12 tables with CRUD methods for each. The same error for all tables is the same.
I have all the individual table.java files built and want to finish all the CRUD functions without errors before moving on to the main activity.java file.
// Get All CompanyData =========================================================
public List<CompanyData> GetAllCompanyData() {
List<CompanyData> compdataList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_CompanyData;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
CompanyData compdata;
compdata = new CompanyData();
compdata.setcompanyid(Long.parseLong(cursor.getString(0))); // <==============================================================
compdata.setcompanyname(cursor.getString(1));
compdata.setcompanyaddress1(cursor.getString(2));
compdata.setcompanyaddress2(cursor.getString(3));
compdata.setcompanycity(cursor.getString(4));
compdata.setcompanystate(cursor.getString(5));
compdata.setcompanyzipcode(cursor.getString(6));
compdata.setcompanyphone(cursor.getString(7));
compdata.setcompanyemail(cursor.getString(8));
compdata.setcompanybusinesslicense(cursor.getString(9));
compdata.setcompanytype(cursor.getString(10));
compdataList.add(compdata);
} while (cursor.moveToNext());
}
return compdataList;
}
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="Username"
android:textSize="18sp"
android:textStyle="bold|italic" />
<EditText
android:id="#+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView"
android:ems="10"
android:gravity="center_vertical|center"
android:hint="Enter Name"
android:inputType="textPersonName"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/editName"
android:layout_marginTop="13dp"
android:gravity="center"
android:hint="Enter Password"
android:text="password"
android:textSize="18sp"
android:textStyle="bold|italic" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_alignEnd="#+id/button4"
android:layout_alignRight="#+id/button4"
android:onClick="viewdata"
android:text="view data"
android:textSize="18sp"
android:textStyle="bold|italic" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/editPass"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp"
android:layout_marginTop="23dp"
android:onClick="addUser"
android:text="add user"
android:textSize="18sp"
android:textStyle="bold|italic" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button4"
android:layout_alignStart="#+id/button4"
android:layout_below="#+id/editText3"
android:layout_marginTop="13dp"
android:onClick="update"
android:text="Update"
android:textStyle="normal|bold" />
<EditText
android:id="#+id/editText6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button2"
android:layout_toStartOf="#+id/button2"
android:ems="10"
android:freezesText="false"
android:hint="Enter Name to Delete Data"
android:inputType="textPersonName" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="41dp"
android:layout_marginEnd="21dp"
android:layout_marginRight="21dp"
android:onClick="delete"
android:text="delete"
android:textStyle="normal|bold"
tools:ignore="RelativeOverlap" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button"
android:layout_marginLeft="7dp"
android:layout_marginStart="7dp"
android:layout_marginTop="47dp"
android:ems="10"
android:hint="Current Name"
android:inputType="textPersonName"
android:textSize="14sp"
android:textStyle="bold|italic" />
<EditText
android:id="#+id/editPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="11dp"
android:ems="10"
android:gravity="center_vertical|center"
android:hint="Enter Password"
android:inputType="textPassword"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="normal|bold" />
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_alignStart="#+id/editText3"
android:layout_alignTop="#+id/button3"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="New Name"
android:inputType="textPersonName"
android:textSize="14sp"
android:textStyle="bold|italic" />
</RelativeLayout>'
main activityjava
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText Name, Pass , updateold, updatenew, delete;
DbAdapter helper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name= (EditText) findViewById(R.id.editName);
Pass= (EditText) findViewById(R.id.editPass);
updateold= (EditText) findViewById(R.id.editText3);
updatenew= (EditText) findViewById(R.id.editText5);
delete = (EditText) findViewById(R.id.editText6);
helper = new DbAdapter(this);
}
public void addUser(View view)
{
String t1 = Name.getText().toString();
String t2 = Pass.getText().toString();
if(t1.isEmpty() || t2.isEmpty())
{
Message.message(getApplicationContext(),"Enter Both Name and Password");
}
else
{
long id = helper.insertData(t1,t2);
if(id<=0)
{
Message.message(getApplicationContext(),"Insertion Unsuccessful");
Name.setText("");
Pass.setText("");
} else
{
Message.message(getApplicationContext(),"Insertion Successful");
Name.setText("");
Pass.setText("");
}
}
}
public void viewdata(View view)
{
String data = helper.getData();
Message.message(this,data);
}
public void update( View view)
{
String u1 = updateold.getText().toString();
String u2 = updatenew.getText().toString();
if(u1.isEmpty() || u2.isEmpty())
{
Message.message(getApplicationContext(),"Enter Data");
}
else
{
int a= helper.updateName( u1, u2);
if(a<=0)
{
Message.message(getApplicationContext(),"Unsuccessful");
updateold.setText("");
updatenew.setText("");
} else {
Message.message(getApplicationContext(),"Updated");
updateold.setText("");
updatenew.setText("");
}
}
}
public void delete( View view)
{
String uname = delete.getText().toString();
if(uname.isEmpty())
{
Message.message(getApplicationContext(),"Enter Data");
}
else{
int a= helper.delete(uname);
if(a<=0)
{
Message.message(getApplicationContext(),"Unsuccessful");
delete.setText("");
}
else
{
Message.message(this, "DELETED");
delete.setText("");
}
}
}
}'
DbAdapterjava
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbAdapter extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myDatabase"; // Database Name
private static final String TABLE_NAME = "myTable"; // Table Name
private static final int DATABASE_Version = 1; // Database Version
private static final String UID = "_id"; // Column I (Primary Key)
private static final String NAME = "Name"; //Column II
private static final String MyPASSWORD = "Password"; // Column III
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
" (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(255) ," + MyPASSWORD + " VARCHAR(225));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private Context context;
public DbAdapter(Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.context = context;
}
public long insertData(String name, String pass) {
SQLiteDatabase dbb = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME, name);
contentValues.put(MyPASSWORD, pass);
long id = dbb.insert(TABLE_NAME, null, contentValues);
return id;
}
public String getData() {
SQLiteDatabase db = getWritableDatabase();
String[] columns = {UID, NAME, MyPASSWORD};
Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()) {
int cid = cursor.getInt(cursor.getColumnIndex(UID));
String name = cursor.getString(cursor.getColumnIndex(NAME));
String password = cursor.getString(cursor.getColumnIndex(MyPASSWORD));
buffer.append(cid + " " + name + " " + password + " \n");
}
return buffer.toString();
}
public int delete(String uname) {
SQLiteDatabase db = this.getWritableDatabase();
String[] whereArgs = {uname};
int count = db.delete(TABLE_NAME, NAME + " = ?", whereArgs);
return count;
}
public int updateName(String oldName, String newName) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME, newName);
String[] whereArgs = {oldName};
int count = db.update(TABLE_NAME, contentValues, NAME + " = ?", whereArgs);
return count;
}
#Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (Exception e) {
Message.message(context, "" + e);
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Message.message(context, "OnUpgrade");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (Exception e) {
Message.message(context, "" + e);
}
}
}'
Messagejava
import android.content.Context;
import android.widget.Toast;
public class Message {
public static void message(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}'
After reaching the registration page and entering details the app crashes with a null pointer exception. I did search for this question on the site, but I wasn't able to find a suitable solution. It would be amazing if someone could help me sort out this issue.
This is my registration activity Java file:
package universe.sk.syndriveapp;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class RegistrationActivity extends AppCompatActivity {
private EditText etName,etEmailsign,etPassign,etBloodgroup,etDate;
private Button btn_register;
private TextView tvExist;
private FirebaseAuth firebaseAuth;
String name,email,password,bloodgrp,date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("SIGN UP");
setupUIViews();
firebaseAuth = FirebaseAuth.getInstance();
btn_register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(validate())
{
String user_email = etEmailsign.getText().toString().trim();
String user_password = etPassign.getText().toString().trim();
//store in database:to be done after filling the contacts
firebaseAuth.createUserWithEmailAndPassword(user_email,user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
adduser();
Toast.makeText(RegistrationActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();}
else
Toast.makeText(RegistrationActivity.this, "Registration Failed!", Toast.LENGTH_SHORT).show();
}
});
startActivity(new Intent(RegistrationActivity.this,NavigationActivity.class));
}
}
});
tvExist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(RegistrationActivity.this,MainActivity.class));
}
});
}
private void setupUIViews()
{
etName = (EditText)findViewById(R.id.etName);
etEmailsign =(EditText) findViewById(R.id.etEmailsign);
etPassign =(EditText) findViewById(R.id.etPassign);
btn_register = (Button)findViewById(R.id.btn_register);
tvExist = (TextView)findViewById(R.id.tvExist);
etDate =(EditText)findViewById(R.id.etDate);
etBloodgroup =(EditText)findViewById(R.id.etBloodgroup);
}
private Boolean validate()
{
Boolean result = false;
name = etName.getText().toString();
password = etPassign.getText().toString();
email = etEmailsign.getText().toString();
bloodgrp = etBloodgroup.getText().toString().trim();
date = etDate.getText().toString().trim();
if(name.isEmpty() || password.isEmpty() || email.isEmpty())
{
Toast.makeText(this, "Please enter all the details!", Toast.LENGTH_SHORT).show();
}
else
result=true;
return result;
}
private void adduser(){
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseusers= firebaseDatabase.getReference(firebaseAuth.getUid());
Userinfo user = new Userinfo(name,bloodgrp,date,email,password);
databaseusers.setValue(user);
}
}
This is my Userinfo Java file:
package universe.sk.syndriveapp;
public class Userinfo {
public String username;
public String uemail;
public String udate;
public String upassword;
public String bloodgroup;
public Userinfo(String username, String uemail, String udate, String upassword, String bloodgroup) {
this.username = username;
this.uemail = uemail;
this.udate = udate;
this.upassword = upassword;
this.bloodgroup = bloodgroup;
}
public String getUsername() {
return username;
}
public String getUemail() {
return uemail;
}
public String getUdate() {
return udate;
}
public String getUpassword() {
return upassword;
}
public String getBloodgroup() {
return bloodgroup;
}
}
This is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegistrationActivity">
<EditText
android:id="#+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="#string/user_name"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.106" />
<EditText
android:id="#+id/etDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/date_of_birth"
android:inputType="date" />
<EditText
android:id="#+id/etBloodGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/blood_group"
android:inputType="text" />
<EditText
android:id="#+id/etEmailsign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etDate" />
<EditText
android:id="#+id/etPassign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etEmailsign"
app:layout_constraintVertical_bias="0.08" />
<EditText
android:id="#+id/etConfirmPassign"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/confirm_password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etPassign"
app:layout_constraintVertical_bias="0.08" />
<Button
android:id="#+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorPrimary"
android:text="#string/next"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etConfirmPassign"
app:layout_constraintVertical_bias="0.132" />
<TextView
android:id="#+id/tvExist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="Already an existing member? Login"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_register" />
</LinearLayout>
error :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at universe.sk.syndriveapp.RegistrationActivity.validate(RegistrationActivity.java:96)
at universe.sk.syndriveapp.RegistrationActivity.access$000(RegistrationActivity.java:22)
at universe.sk.syndriveapp.RegistrationActivity$1.onClick(RegistrationActivity.java:44)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22295)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
You didn't initialize etBloodgroup. That's why you are getting NullPointerException.
Initialize your views properly:
etBloodgroup =(EditText) findViewById(R.id.etBloodGroup);
There is another EditText with id etConfirmPassign. You have to initialize all the views that you want to access.
I'm a complete newbie to Android development. My intention is to create an app which stores user inputs collected in EditText into an SQLite database and then displays them in next activity as a ListView. There are also some buttons involved, some of them are redundant as the app is still in its early stages.
When building the Gradle, there were no errors, also the logcat seemed to be completely OK with the whole thing. The first sign of trouble was checking the /data/data/[projectname] folder which didn't include any .db file or database directory. I have managed to make the transition between activities flawless, but since apparently there's no database in my project, nothing can populate the ListView.
I'm asking for some help determining what's wrong with my code.
MainActivity:
package com.example.peroalex.trackofworkinghours;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
MyDatabaseHelper databaseHelper;
Button ADD, DISPLAY, DELETE, MODIFY;
EditText DESCRIPTION, LOCATION, START, FINISH, COMMENT, ID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ADD = (Button) findViewById(R.id.addButton);
DISPLAY = (Button) findViewById(R.id.displayButton);
DELETE = (Button) findViewById(R.id.deleteButton);
MODIFY = (Button) findViewById(R.id.modifyButton);
DESCRIPTION = (EditText) findViewById(R.id.descriptionEdit);
LOCATION = (EditText) findViewById(R.id.locationEdit);
START = (EditText) findViewById(R.id.startEdit);
FINISH = (EditText) findViewById(R.id.finishEdit);
COMMENT = (EditText) findViewById(R.id.commentEdit);
ID = (EditText) findViewById(R.id.idEdit);
databaseHelper = new MyDatabaseHelper(this, null, null, 1);
ADD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
databaseAddData();
}
});
}
public void databaseAddData() {
String addDSC = DESCRIPTION.getText().toString();
String addLOC = LOCATION.getText().toString();
String addSTR = START.getText().toString();
String addFNS = FINISH.getText().toString();
String addCOM = COMMENT.getText().toString();
databaseHelper.addData(addDSC, addLOC, addSTR, addFNS, addCOM);
}
public void Action (View view) {
Intent intent = new Intent(MainActivity.this, showRecords.class);
startActivity(intent);
}
}
MyDatabaseHelper:
package com.example.peroalex.trackofworkinghours;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by peroalex on 4/1/18.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper {
//Define database properties
private static final int database_version = 1;
private static final String database_name = "DatabaseRecords.db";
private static final String table_name = "tasks";
private static final String column_id = "_id";
private static final String column_desc = "description";
private static final String column_loc = "location";
private static final String column_strt = "start";
private static final String column_fnsh = "finish";
private static final String column_comm = "comment";
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, database_name, factory, database_version);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//This code will be executed when creating a database, it includes the SQLite query which initiates a table
String query = " CREATE TABLE " + table_name + " ( "
+ column_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ column_desc + " TEXT "
+ column_loc + " TEXT "
+ column_strt + " TEXT "
+ column_fnsh + " TEXT "
+ column_comm + " TEXT " + ");";
sqLiteDatabase.execSQL(query);
}
//This method is used for deleting data according to given ID
public void removeData(Integer ID) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
sqLiteDatabase.execSQL("DELETE FROM " + table_name + " WHERE " + column_id + " = " + ID + ";");
sqLiteDatabase.close();
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + table_name);
onCreate(sqLiteDatabase);
}
//This method is used for adding data to the SQLite database
public void addData(String dsc, String loc, String st, String fi, String comm) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(column_desc, dsc);
values.put(column_loc, loc);
values.put(column_strt, st);
values.put(column_fnsh, fi);
values.put(column_comm, comm);
sqLiteDatabase.insert(table_name, null, values);
sqLiteDatabase.close();
}
public Cursor getData() {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor data = sqLiteDatabase.rawQuery("SELECT * FROM " + table_name, null);
return data;
}
}
showRecords:
package com.example.peroalex.trackofworkinghours;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
/**
* Created by peroalex on 4/2/18.
*/
public class showRecords extends AppCompatActivity {
ListView listView;
MyDatabaseHelper myDatabaseHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_records);
listView = (ListView) findViewById(R.id.displayDBListview);
myDatabaseHelper = new MyDatabaseHelper(this, null, null, 1);
displayDB();
}
public void displayDB() {
Cursor data = myDatabaseHelper.getData();
ArrayList<String> listData = new ArrayList<>();
while (data.moveToNext()) {
listData.add(data.getString(1));
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
listView.setAdapter(adapter);
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.peroalex.trackofworkinghours.MainActivity">
<EditText
android:id="#+id/descriptionEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_description_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/description"
android:inputType="text"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.052" />
<EditText
android:id="#+id/locationEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_location_on_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/location"
android:inputType="text"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.14" />
<EditText
android:id="#+id/startEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_work_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/start"
android:inputType="time"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.249" />
<EditText
android:id="#+id/finishEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_work_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/finish"
android:inputType="time"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.337" />
<EditText
android:id="#+id/commentEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:drawableStart="#drawable/ic_comment_black_24dp"
android:ems="10"
android:fontFamily="serif"
android:hint="#string/comments"
android:inputType="textMultiLine"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.448" />
<Button
android:id="#+id/addButton"
style="#android:style/Widget.Holo.Button"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.297"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.628" />
<Button
android:id="#+id/deleteButton"
style="#android:style/Widget.Holo.Button.Borderless"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/delete"
android:textColor="#android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.291"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.934" />
<Button
android:id="#+id/displayButton"
style="#android:style/Widget.Holo.Button"
android:layout_width="90sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/display"
android:onClick="Action"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.702"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.628" />
<Button
android:id="#+id/modifyButton"
style="#android:style/Widget.Holo.Button.Borderless"
android:layout_width="80sp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/modify"
android:textColor="#android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.708"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.934" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/insert_an_id_to_either_modify_or_delete_the_query"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.731" />
<EditText
android:id="#+id/idEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:drawableStart="#drawable/ic_update_black_24dp"
android:fontFamily="serif"
android:hint="#string/id"
android:inputType="text"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.829" />
</android.support.constraint.ConstraintLayout>
activity_show_records:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/displayDBListview"
android:layout_width="344dp"
android:layout_height="551dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Your query to create the database is incorrect.You have forgotten to add , after TEXT, Hence your database was never created. Have a look at the code below
String query = " CREATE TABLE " + table_name + " ("
+ column_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ column_desc + " TEXT,"
+ column_loc + " TEXT,"
+ column_strt + " TEXT,"
+ column_fnsh + " TEXT,"
+ column_comm + " TEXT" + ");";
sqLiteDatabase.execSQL(query);
}
Here's the main activity
public class MainActivity extends Activity {
///Lot of code here which does work
Cursor pscod = db.rawQuery("SELECT * FROM myTable", null);
Cursor phoneCode=db.rawQuery("SELECT * FROM myPhone", null);
if(!(pscod.getCount()==0)&&!(phoneCode.getCount()==0))
{
Toast.makeText(getApplicationContext(),"hai",Toast.LENGTH_LONG).show();
Intent dialogboxintent = new Intent(getApplicationContext(),FAQ.class);
startActivity(dialogboxintent);
}
I know that the compiler does check the if condition and gets inside since the toast does occur. The activity is defined in the manifest file as
<activity android:name="com.example.profilechanger.FAQ"></activity>
The new activity just crashes without showing the layout I have set it to view.
Here's the FAQ class
public class FAQ extends AppCompatActivity {
ImageButton keyimage;
SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faq);
db = openOrCreateDatabase("myDataBase.db", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS myTable(passcode VARCHAR);");
db.execSQL("CREATE TABLE IF NOT EXISTS masterPass(masterName VARCHAR,masterPassword VARCHAR);");
db.execSQL("CREATE TABLE IF NOT EXISTS myPhone(lockPassword VARCHAR);");
keyimage = (ImageButton) findViewById(R.id.keybutton);
keyimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Cursor pscod = db.rawQuery("SELECT * FROM myTable", null);
Cursor phoneCode=db.rawQuery("SELECT * FROM myPhone", null);
if (pscod.getCount()==0&&phoneCode.getCount()==0)
{
Toast.makeText(FAQ.this, "SET PASSCODE AND LOCKCODE", Toast.LENGTH_SHORT).show();
}
else
{
final Dialog masterDialog = new Dialog(FAQ.this);
masterDialog.setContentView(R.layout.mastercode_enter_layout);
masterDialog.setCancelable(false);
masterDialog.setTitle("ENTER MASTER PASSWORD");
final EditText master_edt_pass = (EditText) masterDialog.findViewById(R.id.master_editTextPasswordToLogin);
Button master_btn_submit = (Button) masterDialog.findViewById(R.id.master_buttonSignIn);
master_btn_submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// get The User name and Password
String masterPASSWORD = master_edt_pass.getText().toString();
// check if the Stored password matches with Password entered by user
if (masterPASSWORD.trim().length()==0) {
showMessage("Error", "Please enter PASSWORD");
return;
}
Cursor c = db.rawQuery("SELECT * FROM masterPass WHERE masterPassword ='" + masterPASSWORD + "'", null);
if (c.moveToFirst()) {
showMessage("Success","LOGIN SUCCESS");
Intent profileIntent = new Intent(getApplicationContext(),Profile.class);
startActivity(profileIntent);
masterDialog.dismiss();
} else {
showMessage("Error", "Invalid attempt");
finish();
}
}
});
masterDialog.show();
}
}
});
}
public void showprofilemode(View view) {
android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
MyDialog myDialog=new MyDialog();
myDialog.show(manager,"Profile");
}
public void callbackbutton(View view) {
android.support.v4.app.FragmentManager manager2=getSupportFragmentManager();
Callbackdialog calldialog=new Callbackdialog();
calldialog.show(manager2,"Call Back");
}
public void locknowbutton(View view) {
android.support.v4.app.FragmentManager manager3=getSupportFragmentManager();
Locknowdialog locknowdialog=new Locknowdialog();
locknowdialog.show(manager3,"Lock Now");
}
public void alerttonebutton(View view) {
android.support.v4.app.FragmentManager manager4=getSupportFragmentManager();
Alerttone alertnow=new Alerttone();
alertnow.show(manager4,"Play Alert");
}
public void locationbutton(View view) {
android.support.v4.app.FragmentManager manager5=getSupportFragmentManager();
Location1 location=new Location1();
location.show(manager5,"Get Location");
}
public void simchangebutton(View view) {
android.support.v4.app.FragmentManager manager6=getSupportFragmentManager();
Simchangedialog simchange=new Simchangedialog();
simchange.show(manager6,"Sim Change");
}
public void showMessage(String title, String message) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
The activity doesn't even show the view FAQ layout
Here's faq.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/faq"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.profilechanger.FAQ">
<Button
android:id="#+id/angry_btn"
android:text="Changing the ring mode of the device"
android:textColor="#2E3CFF"
android:textSize="15sp"
android:textAllCaps="false"
android:onClick="showprofilemode"
android:layout_width="1000dp"
android:layout_height="60dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A7A6"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
<Button
android:id="#+id/callbackbutton"
android:layout_below="#+id/angry_btn"
android:text="Getting a call back from the lost phone"
android:textColor="#2E3CFF"
android:textSize="15sp"
android:textAllCaps="false"
android:onClick="callbackbutton"
android:layout_width="1000dp"
android:layout_height="60dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A7A6"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
<Button
android:id="#+id/locknowbutton"
android:layout_below="#+id/callbackbutton"
android:text="To Lock the phone"
android:textColor="#2E3CFF"
android:textSize="15sp"
android:textAllCaps="false"
android:onClick="locknowbutton"
android:layout_width="1000dp"
android:layout_height="60dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A7A6"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
<Button
android:id="#+id/alertonebutton"
android:layout_below="#+id/locknowbutton"
android:text="To play alert tone"
android:textColor="#2E3CFF"
android:textSize="15sp"
android:textAllCaps="false"
android:onClick="alerttonebutton"
android:layout_width="1000dp"
android:layout_height="60dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A7A6"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
<Button
android:id="#+id/locationbutton"
android:layout_below="#+id/alertonebutton"
android:text="To Track the Phone's location"
android:textColor="#2E3CFF"
android:textSize="15sp"
android:textAllCaps="false"
android:onClick="locationbutton"
android:layout_width="1000dp"
android:layout_height="60dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A7A6"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
<Button
android:id="#+id/simchangebutton"
android:layout_below="#+id/locationbutton"
android:text="What happens when your sim is changed?"
android:textColor="#2E3CFF"
android:textSize="15sp"
android:textAllCaps="false"
android:onClick="simchangebutton"
android:layout_width="1000dp"
android:layout_height="60dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A7A6"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/><LinearLayout
android:layout_below="#id/locationbutton"
android:layout_marginTop="55dp"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:background="#drawable/change_passcode"
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/keybutton"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>
Logcat doesn't even show anything. Please help.