Cannot populate ListView with SQLite database query - java

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);
}

Related

Sending a notification through a button by accepting data from both tabs in a tabbed activity in Android Studio

I am currently creating my very first application, it's a very dummy coffee shop application that includes multiple activities like a basic activity and a log in activity. However, I have currently created my first tabbed activity where the user can "order" coffee (first tab) and food (second tab) by selecting each CheckBox respectively as you can see in the images below.
TAB 1 - COFFEE
TAB 2 - FOOD
As you can see, there are two buttons on the bottom of the screen, the "GO BACK" button that sends the user back to the main activity and another button next to it called "ORDER NOW" which is where I am stuck. Both buttons are visible in both tabs and I have an onClick method for the first button "sendNotification". When I press that button it checks which check boxes are clicked and stores them accordingly inside the String order and then insert that value inside the notification. Everything works fine when I "order" coffees but no matter what selection I make in the second tab called "FOOD" it always sends an empty message like this "Here is your oder: , it will arrive in about 1 hour, thank you!".
Implementation for sendNotification:
package com.example.myapp;
public class OrderActivity extends AppCompatActivity {
private ActivityOrderBinding binding;
private PageViewModel pageViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityOrderBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = binding.viewPager;
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = binding.tabs;
tabs.setupWithViewPager(viewPager);
}
public void goBack(View v)
{
finish();
}
public void sendNotification(View v)
{
CheckBox ch1 = findViewById(R.id.cb1);
CheckBox ch2 = findViewById(R.id.cb2);
CheckBox ch3 = findViewById(R.id.cb3);
CheckBox ch4 = findViewById(R.id.cb4);
CheckBox ch5 = findViewById(R.id.cb5);
CheckBox ch6 = findViewById(R.id.cb6);
CheckBox ch7 = findViewById(R.id.cb7);
CheckBox ch8 = findViewById(R.id.cb8);
CheckBox ch9 = findViewById(R.id.cb9);
CheckBox ch10 = findViewById(R.id.cb10);
String order = "";
if (ch1.isChecked()) {
order += ch1.getText().toString() + ", ";
}
if (ch2.isChecked()) {
order += ch2.getText().toString() + ", ";
}
if (ch3.isChecked()) {
order += ch3.getText().toString() + ", ";
}
if (ch4.isChecked()) {
order += ch4.getText().toString() + ", ";
}
if (ch5.isChecked()) {
order += ch5.getText().toString() + ", ";
}
if (ch6.isChecked()) {
order += ch6.getText().toString() + ", ";
}
if (ch7.isChecked()) {
order += ch7.getText().toString() + ", ";
}
if (ch8.isChecked()) {
order += ch8.getText().toString() + ", ";
}
if (ch9.isChecked()) {
order += ch9.getText().toString() + ", ";
}
if (ch10.isChecked()) {
order += ch10.getText().toString() + ", ";
}
NotificationChannel channel;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = new NotificationChannel("1", "channel1", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "1")
.setSmallIcon(R.drawable.coffeeicon)
.setContentTitle("Order confirmation")
.setContentText("Here is your order: " + order + " it will arrive in about 1 hour, thank you!")
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManagerCompat notifyAdmin = NotificationManagerCompat.from(this);
notifyAdmin.notify(1, notification.build());
}
finish();
Toast.makeText(getApplicationContext(), "Thank you for ordering, you will now receive a confirmation for your order!", Toast.LENGTH_LONG).show();
}
}
My PageViewModel.java
package com.example.myapp.ui.main;
import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
public class PageViewModel extends ViewModel {
private MutableLiveData<Integer> mIndex = new MutableLiveData<>();
private LiveData<String> mText = Transformations.map(mIndex, new Function<Integer, String>() {
#Override
public String apply(Integer input) {
return "Hello world from section: " + input;
}
});
public void setIndex(int index) {
mIndex.setValue(index);
}
public LiveData<String> getText() {
return mText;
}
}
My PlaceHolderFragment.java
package com.example.myapp_csc392.ui.main;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.AsyncListUtil;
import com.example.myapp_csc392.R;
import com.example.myapp_csc392.databinding.FragmentOrderBinding;
import org.w3c.dom.Text;
/**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private PageViewModel pageViewModel;
private FragmentOrderBinding binding;
public static PlaceholderFragment newInstance(int index) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle bundle = new Bundle();
bundle.putInt(ARG_SECTION_NUMBER, index);
fragment.setArguments(bundle);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
int index = 1;
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER);
}
pageViewModel.setIndex(index);
}
#Override
public View onCreateView(
#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentOrderBinding.inflate(inflater, container, false);
View root = binding.getRoot();
ImageView img1 = root.findViewById(R.id.ivEspresso);
ImageView img2 = root.findViewById(R.id.ivCapuccino);
ImageView img3 = root.findViewById(R.id.ivLatte);
ImageView img4 = root.findViewById(R.id.ivMilkshake);
ImageView img5 = root.findViewById(R.id.ivWater);
ImageView img6 = root.findViewById(R.id.ivHamburger);
ImageView img7 = root.findViewById(R.id.ivSandwich);
ImageView img8 = root.findViewById(R.id.ivSalad);
ImageView img9 = root.findViewById(R.id.ivCake);
ImageView img10 = root.findViewById(R.id.ivChips);
CheckBox c1 = root.findViewById(R.id.cb1);
CheckBox c2 = root.findViewById(R.id.cb2);
CheckBox c3 = root.findViewById(R.id.cb3);
CheckBox c4 = root.findViewById(R.id.cb4);
CheckBox c5 = root.findViewById(R.id.cb5);
CheckBox c6 = root.findViewById(R.id.cb6);
CheckBox c7 = root.findViewById(R.id.cb7);
CheckBox c8 = root.findViewById(R.id.cb8);
CheckBox c9 = root.findViewById(R.id.cb9);
CheckBox c10 = root.findViewById(R.id.cb10);
TextView e1 = root.findViewById(R.id.tv1);
TextView e2 = root.findViewById(R.id.tv2);
TextView e3 = root.findViewById(R.id.tv3);
TextView e4 = root.findViewById(R.id.tv4);
TextView e5 = root.findViewById(R.id.tv5);
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1)
{
img6.setVisibility(View.INVISIBLE);
img7.setVisibility(View.INVISIBLE);
img8.setVisibility(View.INVISIBLE);
img9.setVisibility(View.INVISIBLE);
img10.setVisibility(View.INVISIBLE);
c6.setVisibility(View.INVISIBLE);
c7.setVisibility(View.INVISIBLE);
c8.setVisibility(View.INVISIBLE);
c9.setVisibility(View.INVISIBLE);
c10.setVisibility(View.INVISIBLE);
c1.setText("Espresso");
c2.setText("Capuccino");
c3.setText("Latte");
c4.setText("Milkshake");
c5.setText("Water");
e1.setText("2.50€");
e2.setText("3.50€");
e3.setText("4.50€");
e4.setText("4.00€");
e5.setText("1.00€");
}
else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2)
{
img1.setVisibility(View.INVISIBLE);
img2.setVisibility(View.INVISIBLE);
img3.setVisibility(View.INVISIBLE);
img4.setVisibility(View.INVISIBLE);
img5.setVisibility(View.INVISIBLE);
c1.setVisibility(View.INVISIBLE);
c2.setVisibility(View.INVISIBLE);
c3.setVisibility(View.INVISIBLE);
c4.setVisibility(View.INVISIBLE);
c5.setVisibility(View.INVISIBLE);
c6.setText("Hamburger");
c7.setText("Sandwich");
c8.setText("Salad");
c9.setText("Red Velvet Cake");
c10.setText("Salty Chips");
e1.setText("7.00€");
e2.setText("5.00€");
e3.setText("5.00€");
e4.setText("4.50€");
e5.setText("1.50€");
}
return root;
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
and the fragment_order.xml
<?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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">
<ImageView
android:id="#+id/ivEspresso"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023"
app:srcCompat="#drawable/espresso" />
<ImageView
android:id="#+id/ivHamburger"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023"
app:srcCompat="#drawable/hamburger" />
<ImageView
android:id="#+id/ivCapuccino"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.214"
app:srcCompat="#drawable/capuccino" />
<ImageView
android:id="#+id/ivSandwich"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.214"
app:srcCompat="#drawable/sandwich" />
<ImageView
android:id="#+id/ivLatte"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.394"
app:srcCompat="#drawable/latte" />
<ImageView
android:id="#+id/ivSalad"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.394"
app:srcCompat="#drawable/salad" />
<ImageView
android:id="#+id/ivMilkshake"
android:layout_width="51dp"
android:layout_height="61dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.588"
app:srcCompat="#drawable/milkshake" />
<ImageView
android:id="#+id/ivCake"
android:layout_width="50dp"
android:layout_height="62dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.585"
app:srcCompat="#drawable/cake" />
<ImageView
android:id="#+id/ivWater"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.775"
app:srcCompat="#drawable/water" />
<ImageView
android:id="#+id/ivChips"
android:layout_width="53dp"
android:layout_height="59dp"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.044"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.775"
app:srcCompat="#drawable/chips" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF9800"
android:onClick="goBack"
android:text="GO BACK"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.791"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.959" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF9800"
android:onClick="sendNotification"
android:text="ORDER NOW"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.239"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.959" />
<CheckBox
android:id="#+id/cb6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />
<CheckBox
android:id="#+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.409" />
<CheckBox
android:id="#+id/cb8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.409"
tools:ignore="DuplicateClickableBoundsCheck" />
<CheckBox
android:id="#+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.051" />
<CheckBox
android:id="#+id/cb10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.774" />
<CheckBox
android:id="#+id/cb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.347"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.774" />
<CheckBox
android:id="#+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238" />
<CheckBox
android:id="#+id/cb7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238" />
<CheckBox
android:id="#+id/cb9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<CheckBox
android:id="#+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#FF9800"
android:text="CheckBox"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.594" />
<TextView
android:id="#+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.093"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.824" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.144" />
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.48" />
<TextView
android:id="#+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.071"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.66" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.068"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.313" />
</androidx.constraintlayout.widget.ConstraintLayout>
If anyone could help out I would really appreciate it and please let me know if you need me to show more code.

Why are all my EditText.getText().toString() returning an empty String?

This problem occurs throughout the app but I am only posting my register activity.
Here is a screenshot of my register activity:
Here is the xml for my register activity. I don't think you need to read it.
<?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"
android:background="#6633ff"
tools:context=".RegisterActivity">
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/left_guideline_edit_text"
app:layout_constraintGuide_percent=".12077"
android:orientation="vertical"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/right_guideline_edit_text"
app:layout_constraintGuide_percent=".87923"
android:orientation="vertical"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_name"
app:layout_constraintGuide_percent=".09821"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_name"
app:layout_constraintGuide_percent=".17857"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_email"
app:layout_constraintGuide_percent=".21429"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_email"
app:layout_constraintGuide_percent=".29464"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_password"
app:layout_constraintGuide_percent=".33036"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_password"
app:layout_constraintGuide_percent=".41071"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_city"
app:layout_constraintGuide_percent=".44420"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_city"
app:layout_constraintGuide_percent=".52455"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_country"
app:layout_constraintGuide_percent=".56027"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/left_guideline_register_button"
app:layout_constraintGuide_percent=".27536"
android:orientation="vertical"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/right_guideline_register_button"
app:layout_constraintGuide_percent=".72222"
android:orientation="vertical"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_register_button"
app:layout_constraintGuide_percent=".70313"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_country"
app:layout_constraintGuide_percent=".64063"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_register_button"
app:layout_constraintGuide_percent=".78125"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_view_already_account"
app:layout_constraintGuide_percent=".80692"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_text_view_already_account"
app:layout_constraintGuide_percent=".84933"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_text_view_log_in"
app:layout_constraintGuide_percent=".84933"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_text_view_log_in"
app:layout_constraintGuide_percent=".89174"
android:orientation="horizontal"/>
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_name"
android:hint="Name"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textCapSentences"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_name"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_name"/>
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_email"
android:hint="Email"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textEmailAddress"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_email"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_email" />
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_password"
android:hint="Password"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textPassword"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_password"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_password" />
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_city"
android:hint="City"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textCapSentences"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_city"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_city" />
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_country"
android:hint="Country"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textCapSentences"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_country"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_country"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/register_button"
android:background="#drawable/button_shape"
android:fontFamily="#font/inter_extrabold"
android:textSize="28sp"
android:text="Register"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#6633ff"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_register_button"
app:layout_constraintRight_toRightOf="#+id/right_guideline_register_button"
app:layout_constraintTop_toTopOf="#+id/top_guideline_register_button"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_register_button"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view_already_account"
android:text="Already have an account?"
android:fontFamily="#font/inter_thin"
android:textSize="20sp"
android:textColor="#ffffff"
android:textAlignment="center"
android:textAllCaps="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_view_already_account"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_text_view_already_account"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view_log_in"
android:text="#string/LogIn"
android:fontFamily="#font/inter_semibold"
android:textSize="28sp"
android:textColor="#ffffff"
android:textAlignment="center"
android:textAllCaps="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/top_guideline_text_view_log_in"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_text_view_log_in"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And here is my code for the activity. The main part of the code is at the bottom in the goToRegisterAs() method.
package com.example.treeapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class RegisterActivity extends AppCompatActivity {
private EditText nameEditText;
private EditText emailEditText;
private EditText passwordEditText;
private EditText cityEditText;
private EditText countryEditText;
private Button registerButton;
private TextView alreadyAccountTextView;
private TextView logInTextView;
private String name;
private String email;
private String password;
private String city;
private String country;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
nameEditText = findViewById(R.id.edit_text_name);
emailEditText = findViewById(R.id.edit_text_email);
passwordEditText = findViewById(R.id.edit_text_password);
cityEditText = findViewById(R.id.edit_text_city);
countryEditText = findViewById(R.id.edit_text_country);
registerButton = findViewById(R.id.register_button);
alreadyAccountTextView = findViewById(R.id.text_view_already_account);
logInTextView = findViewById(R.id.text_view_log_in);
name = nameEditText.getText().toString();
email = emailEditText.getText().toString();
password = passwordEditText.getText().toString();
city = cityEditText.getText().toString();
country = countryEditText.getText().toString();
registerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
goToRegisterAs(view);
}
});
countryEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_ENTER) {
goToRegisterAs(view);
return true;
} else {
return false;
}
}
});
logInTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), MainActivity.class);
view.getContext().startActivity(intent);
}
});
}
void goToRegisterAs(View view) {
if (!(name.equals("") || email.equals("") || password.equals("") ||
city.equals("") || country.equals(""))) {
if (email.contains("#") && email.contains(".")) {
if (MainActivity.database.donorsDao().checkEmail(email) == 0 &&
MainActivity.database.plantersDao().checkEmail(email) == 0) {
Intent intent = new Intent(view.getContext(), RegisterAsActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("password", password);
intent.putExtra("city", city);
intent.putExtra("country", country);
view.getContext().startActivity(intent);
} else {
alreadyAccountTextView.setText("Already registered. Please log in.");
}
} else {
alreadyAccountTextView.setText("Please enter correct email address.");
}
} else {
alreadyAccountTextView.setText("Please enter all fields");
alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country);
}
}
}
The app was not working as I wanted it to. So after some trying, I figured out that my EditTexts are all returning an empty string. In the last line in the code alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country), the alreadyAccountTextView becomes empty. I wrote this line here because this block of code was being executed even though I had input text in every field. I have read many questions and answers on StackOverflow about getting text from EditTexts and all of them say that I should use EditText.getText().toString(). But it isn't working for me, due to some reason.
The problem is you initialise your Strings in your OnCreate function. This function is called only once, when your activity starts; therefore, at this point your EditTexts are all empty without any text in it.
To get the current text located in your EditTexts, you should move your initialisations in your goToRegisterAs() function.
void goToRegisterAs(View view) {
name = nameEditText.getText().toString();
email = emailEditText.getText().toString();
password = passwordEditText.getText().toString();
city = cityEditText.getText().toString();
country = countryEditText.getText().toString();
if (!(name.equals("") || email.equals("") || password.equals("") ||
city.equals("") || country.equals(""))) {
if (email.contains("#") && email.contains(".")) {
if (MainActivity.database.donorsDao().checkEmail(email) == 0 &&
MainActivity.database.plantersDao().checkEmail(email) == 0) {
Intent intent = new Intent(view.getContext(), RegisterAsActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("password", password);
intent.putExtra("city", city);
intent.putExtra("country", country);
view.getContext().startActivity(intent);
} else {
alreadyAccountTextView.setText("Already registered. Please log in.");
}
} else {
alreadyAccountTextView.setText("Please enter correct email address.");
}
} else {
alreadyAccountTextView.setText("Please enter all fields");
alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country);
}
}

CRUD Android Studio

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

nullpointexception:error while inserting data into firebase database

i have provided a intent from the registration page to the add contacts page and in the add contacts page tried to add the details into the firebase database using two functions addusers() and addcontacts()
can somebody pls help me
this is my registration.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.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.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class RegistrationActivity extends AppCompatActivity {
private EditText etName, etEmailsign, etPassign, etConfirmPassign, 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()) {
finish();
startActivity(new Intent(RegistrationActivity.this, AddContacts.class));
//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 = findViewById(R.id.etName);
etEmailsign = findViewById(R.id.etEmailsign);
etPassign = findViewById(R.id.etPassign);
btn_register = findViewById(R.id.btn_register);
tvExist = findViewById(R.id.tvExist);
etConfirmPassign = findViewById(R.id.etConfirmPassign);
etDate = (EditText) findViewById(R.id.etDate);
etBloodgroup = (EditText) findViewById(R.id.etBloodgroup);
}
private Boolean validate() {
Boolean result = false;
bloodgrp = etBloodgroup.getText().toString().trim();
date = etDate.getText().toString().trim();
name = etName.getText().toString();
password = etPassign.getText().toString();
email = etEmailsign.getText().toString();
String confirmpass = etConfirmPassign.getText().toString();
if (name.isEmpty() || password.isEmpty() || email.isEmpty()) {
Toast.makeText(this, "Please enter all the details!", Toast.LENGTH_SHORT).show();
} else {
if (password.equals(confirmpass))
result = true;
else
Toast.makeText(this, "Confirm password doesn't match with your password!", Toast.LENGTH_SHORT).show();
}
return result;
}
}
this is my addcontacts java file
package universe.sk.syndriveapp;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class AddContacts extends AppCompatActivity {
Button registerbtn;
EditText etName,etEmailsign,etPassign,etBloodgroup,etDate;
EditText name1, num1, name2, num2, name3, num3;
String emname1,emname2,emname3;
String emnum1,emnum2,emnum3;
String name,email,password,bloodgrp,date;
private FirebaseAuth firebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addcontacts);
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.contacts);
actionBar.setTitle(" Add Emergency Contacts");
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
registerbtn= findViewById(R.id.registerbtn);
name1 = findViewById(R.id.name1);
num1 = findViewById(R.id.num1);
name2 = findViewById(R.id.name2);
num2 = findViewById(R.id.num2);
name3 = findViewById(R.id.name3);
num3 = findViewById(R.id.num3);
setupUIViews();
firebaseAuth = FirebaseAuth.getInstance();
registerbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int value=checkData();
if(value==1) {
emname1 = name1.getText().toString();
emnum1 = num1.getText().toString();
emname2 = name2.getText().toString();
emnum2 = num2.getText().toString();
emname3 = name3.getText().toString();
emnum3 = num3.getText().toString();
bloodgrp = etBloodgroup.getText().toString().trim();
date = etDate.getText().toString().trim();
name = etName.getText().toString();
password = etPassign.getText().toString();
email = etEmailsign.getText().toString();
adduser();
addcontacts();
/*
if (emname1.isEmpty() || emname2.isEmpty() || emname3.isEmpty()){
startActivity(new Intent(AddContacts.this, RegistrationActivity.class));
Toast.makeText(AddContacts.this, "", Toast.LENGTH_SHORT).show();
}
*/
Toast.makeText(AddContacts.this, "Registration Success!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(AddContacts.this, NavigationActivity.class));
}
}
});
}
boolean isEmpty(EditText text){
CharSequence str = text.getText().toString();
return TextUtils.isEmpty(str);
}
int checkData(){
if(isEmpty(name1) || isEmpty(name2) || isEmpty(name3) || isEmpty(num1) || isEmpty(num2) || isEmpty(num3)){
Toast.makeText(AddContacts.this,"Please fill all contact details",Toast.LENGTH_SHORT).show();
return -1;
}
else
return 1;
}
private void setupUIViews()
{
etName = findViewById(R.id.etName);
etEmailsign = findViewById(R.id.etEmailsign);
etPassign = findViewById(R.id.etPassign);
etDate =(EditText)findViewById(R.id.etDate);
etBloodgroup =(EditText) findViewById(R.id.etBloodgroup);
}
private void addcontacts(){
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseusers = firebaseDatabase.getReference(firebaseAuth.getUid());
Contactdetails contactdetails;
contactdetails = new Contactdetails(emname1,emname2,emname3,emnum1,emnum2,emnum3);
databaseusers.setValue(contactdetails);
}
private void adduser(){
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseusers= firebaseDatabase.getReference(firebaseAuth.getUid());
Userinfo user;
user = new Userinfo(name,email,date,bloodgrp);
databaseusers.setValue(user);
}
}
this is my userinfo java file
package universe.sk.syndriveapp;
import android.net.Uri;
import com.google.android.gms.tasks.Task;
public class Userinfo {
public String username;
public String uemail;
public String udate;
public String bloodgroup;
// public Uri imageUri;
public Userinfo(){
}
public Userinfo(String username, String uemail, String udate, String bloodgroup) {
this.username = username;
this.uemail = uemail;
this.udate = udate;
this.bloodgroup = bloodgroup;
// this.imageUri = imageUri;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUemail() {
return uemail;
}
public void setUemail(String uemail) {
this.uemail = uemail;
}
public String getUdate(){
return udate;
}
public void setUdate(String udate) {
this.udate = udate;
}
public String getBloodgroup() {
return bloodgroup;
}
public void setBloodgroup(String bloodgroup) {
this.bloodgroup = bloodgroup;
}
/* public Uri getImageUri() {
return imageUri;
}
public void setImageUri(Uri imageUri) {
this.imageUri = imageUri;
} */
}
this is my contactdetails java file
package universe.sk.syndriveapp;
public class Contactdetails {
public String cname1;
public String cname2;
public String cname3;
public String no1;
public String no2;
public String no3;
public Contactdetails(){
}
public Contactdetails(String cname1, String cname2, String cname3, String no1, String no2, String no3) {
this.cname1 = cname1;
this.cname2 = cname2;
this.cname3 = cname3;
this.no1 = no1;
this.no2 = no2;
this.no3 = no3;
}
public void setCname1(String cname1) {
this.cname1 = cname1;
}
public void setCname2(String cname2) {
this.cname2 = cname2;
}
public void setCname3(String cname3) {
this.cname3 = cname3;
}
public void setNo1(String no1) {
this.no1 = no1;
}
public void setNo2(String no2) {
this.no2 = no2;
}
public void setNo3(String no3) {
this.no3 = no3;
}
public String getCname1() {
return cname1;
}
public String getCname2() {
return cname2;
}
public String getCname3() {
return cname3;
}
public String getNo1() {
return no1;
}
public String getNo2() {
return no2;
}
public String getNo3() {
return no3;
}
}
this is my addcontacts 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/hint_emname1"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_num1"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/name2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_emname2"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_num2"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/name3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_emname3"
android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<EditText
android:id="#+id/num3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint_num3"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/registerbtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginTop="25dp"
android:background="#color/colorPrimary"
android:text="#string/hint_registerbtn"
android:textColor="#FFFFFF" />
</LinearLayout>
this is my profile 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">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<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" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<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" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<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" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<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" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<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" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimary">
<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" />
</android.support.design.widget.TextInputLayout>
<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>
finally this is my error log
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at universe.sk.syndriveapp.AddContacts$1.onClick(AddContacts.java:67)
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)
Your addcontacts.xml doesn't have an EditText with ID etBloodgroup
The line
bloodgrp = etBloodgroup.getText().toString().trim();
causes the error.
You haven't done a findViewById() on the EditText bloodgroup before and it also does not exist in the addcontacts.xml file. If you have defined it in another xml file, you won't be able to call it in an Activity not associated with the xml source (and you cannot associate more than one xml layout file to an Activity).
So the answer is basically: Define the EditText in the xml file associated to the Activity where you are trying to handle it.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()'

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.

Categories

Resources