Singleton state is not accessible due to context is NULL - android

We are facing issue with Android application.There is one class named SingleTon which Extends Application and we are using it for State manager.when Application running in background and if I open push notification I am not able to access the Singleton class due o context is null, and that's why application is crashed.Same thing happens if application is on stand by mode.
Here, I have mentioned my SingleTon class Code:
public class StateManager extends Application {
public String FirstName;
public String LastName;
private static StateManager instance;
public static synchronized StateManager getInstance() {
return instance;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
#Override
public void onCreate() {
super.onCreate();
instance = this;
Parse.enableLocalDatastore(this);
Parse.initialize(this, "xxxx", "xxxxx");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
#Override
public void onTerminate() {
instance =this;
super.onTerminate();
}
}

just place this line in onCreate of Application class
instance= new StateManager();

May be your instance is not correct please try below code
public static StateManager getInstance(){
if(instance== null){
instance= new StateManager();
}
return instance;
}

Related

Realm IllegalStateException: Schema validation failed

I'm try to use Realm Database in My project
Here are my code snippets
In Application java code
public class CoreApplication extends Application {
private static CoreApplication sInstance;
public static final String F_PREFERENCE = "K_PREFERENCES";
public static Realm realm;
#Override
public void onCreate() {
super.onCreate();
sInstance = this;
LocaleManager.setLocale(this);
realm.init(this);
realm = Realm.getDefaultInstance();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name("mydb.realm")
.schemaVersion(2)
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
#Override
public void onTerminate() {
Realm.getDefaultInstance().close();
super.onTerminate();
}
}
RealmObject java class
public class CashierTable extends RealmObject implements Serializable {
#Index
#PrimaryKey
private long id;
private String name = "";
private String password = "";
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
When I try to call this function when App has started.I have a exception
public static CashierTable getSingleCashierTable() {
CoreApplication.realm = Realm.getDefaultInstance();
AtomicReference<CashierTable> cashierTable=new AtomicReference<>();
CoreApplication.realm .executeTransaction(realm -> {
cashierTable.set(realm.where(CashierTable.class).findFirst());
});
return cashierTable.get();
}
Here is a my log result
java.lang.RuntimeException: Unable to create application com.unipay.posApp.CoreApplication: java.lang.IllegalStateException: Schema validation failed due to the following errors:
- Type 'CashierTable' appears more than once in the schema.
I cleared cash,but still have same issue.
Can anyone explain me what's wrong in my code?
You cannot have two RealmObject classes with the same class name twice unless you use Realm 5.0+ and use #RealmClass(name="... to specify a different table name for at least one of your classes.
package io.package.first;
public class Dog extends RealmObject {
}
package io.package.second;
#RealmClass(name="SecondDog")
public class Dog extends RealmObject {
}

How to make the changes made to the model effective with DataBinding after setModel?

DataBinding: how can I make sure that as a result of a modification of the data model the view is updated accordingly?
Eg:
public class MyActivity extends AppCompatActivity {
private MyActivityBinding mBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.my_activity);
mBinding.setMyModel(new MyModel());
}
public void onClickAnItem(View view) {
MyModel model = mBinding.getMyModel();
model.setField1 = "Jhon";
model.setField2 = "Dho";
mBinding.executePendingBindings();
}
}
In this case the model "MyModel" has been modified but view is not updated; what did I miss?
Reading documentation I found a solution, first of all:
Any plain old Java object (POJO) may be used for data binding, but modifying a POJO will not cause the UI to update!
To give MyModel data object the ability to notify when data changes I made this modifications:
private class MyModel extends BaseObservable {
private String field1;
private String field2;
#Bindable
public String getField1() {
return this.field1;
}
#Bindable
public String getField2() {
return this.field2;
}
public void setField1(String firstName) {
this.field1 = firstName;
notifyPropertyChanged(BR.field1);
}
public void setField2(String lastName) {
this.field2 = lastName;
notifyPropertyChanged(BR.field2);
}
}
I hope this can help someone else
Documentation here

Combining 2 RealmList object in realm android

How to use realm relationships using 2 RealmList, here the example.
Class Menu
public class Menu extends RealmObject {
#SerializedName("name")
private String name;
#SerializedName("module")
private String module;
#SerializedName("controller")
private String controller;
#SerializedName("parent_module")
private String parentModule;
#SerializedName("status")
private Boolean status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
public String getController() {
return controller;
}
public void setController(String controller) {
this.controller = controller;
}
public String getParentModule() {
return parentModule;
}
public void setParentModule(String parentModule) {
this.parentModule = parentModule;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
}
Class Privilege
public class Privilege extends RealmObject {
private String module;
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
}
and i'm using this method to save them.
#Override
public void saveMenuPrivilege(RealmList<Menu> menu, RealmList<Privilege> privileges) {
}
now what makes me confuse is, there is a condition where if the module in class Menu has the same module in class Privilege, then set the active field for that module in class Menu to be "true". How to do that? or i'm doing it wrong using the code above?
Thanks in advance
All ready have the answer, just testing all by myself. :D

How to add data to list and use its data on next page and show in listview

I have 2 class one is login_class second is student_class which is showing details of student.
What i have to do is when i login. In response i get the students details like
student_name,student_srno,student_father_name,student_mother_name etc.
This is my bean(Getter Setter) class
package com.smartschoolapp;
public class Bean {
String username, password, srno, studentname, classname, sec, contact,
father_name, mother_name, dob;
public Bean(String username,String password,String srno,String studentname,String classname,String sec,String contact,String father_name,String mother_name,String dob) {
this.username=username;
this.password= password;
this.srno=srno;
this.studentname=studentname;
this.classname=classname;
this.sec=sec;
this.contact=contact;
this.father_name=father_name;
this.mother_name=mother_name;
this.dob =dob;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSrno() {
return srno;
}
public void setSrno(String srno) {
this.srno = srno;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getSec() {
return sec;
}
public void setSec(String sec) {
this.sec = sec;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getFather_name() {
return father_name;
}
public void setFather_name(String father_name) {
this.father_name = father_name;
}
public String getMother_name() {
return mother_name;
}
public void setMother_name(String mother_name) {
this.mother_name = mother_name;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public Bean() {
}
}
And this is my login_class where i add data of the student in list.
class login_class extends activity{
Bean beanobj;
List student_detail_list;
.....
{
String name = getting data from response;
......... so on
beanobj = new Bean();
beanobj.studentname(name);
beanobj.student_parent_name(parentname);
.............so on
}
order_list.add(beanobj); // the details to list
}
Now the next activity student_class where i want to show the student in listview
public class student_class extends Activity {
ListView student_listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_list);
student_listview = (ListView) findViewById(R.id.student_listview);
}
}
*==>> in login_class i have created List student_detail_list how can i get it in my student_class class and retrieve data .
Thanks
There are a lot a ways to do that. 1.Recommended Solution : How to pass an object from one activity to another on Android you can serialize your objects and put it into the intent. This is the recommended solution but there are some problems here. If your data contains a lot of memory, it might crash your application in runtime.
2. You can save your array into the file, send file path to other activity as with URL. In the second activity read the file and create the array in second activity as well.
3. My favourite solution is create a singleton class for student array, reach the class in any activity you want. But you should be careful because singleton pattern has some issues by design also it will stay in memory all the time
Realize the "class Bean implements Parcelable". When go to "student_class" from "login_class", just put the "student bean data" inside the Intent. After going to student_class, you can get the "student bean data" from the intent, here the sample code:
Intent intent = new Intent(login_class.this, student_class.class)
intent.putParcelableArrayListExtra("content", student_detail_list);
startActivity(intent);
In "student_class":
student_detail_list = getIntent().getParcelableArrayListExtra("content");

How to send user details collected from different activities during signup to the final activity?

I am busy adding a signup feature to my app and I have 4 activities which I use to collect the user's information. The activities are supposed to collect the details input by the user and send them all to the final activity where they will be sent to the database.
The first activity asks users to choose whether they want to use their email or phone number to sign up.
The second activity asks users to choose their username and password.
The third activity asks the user to add more personal details as well as to add a profile picture to their account.
The final activity asks the users to input their geographic details and then sends all the information sent from the other activities to the database.
The problem I am facing is that if i use Intents, i need to send data from activity to activity. Which is giving me a lot of errors, how can I the information collected in each activity to the final one and then send them all in one go.
You can use Singleton design pattern which preserves single object between all activities.
For example:
public class SignUpSingleton {
private int emailOrPhone;
private String username;
private String password;
private String firstName;
private String lastName;
private String country;
private String city;
//remaining fields here
private static SignUpSingleton instance = new SignUpSingleton();
private SignUpSingleton(){}
public static SignUpSingleton getInstance(){
return instance;
}
public int getEmailOrPhone() {
return emailOrPhone;
}
public void setEmailOrPhone(int emailOrPhone) {
this.emailOrPhone = emailOrPhone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
In the first activity:
SignUpSingleton.getInstance().setEmailOrPhone(1); //1 or 2
In the second activity:
SignUpSingleton.getInstance().setUsername("Tom");
SignUpSingleton.getInstance().setPassword("pass");
And so on for third and forth.
In the last activity you can send all data to the database at once, e.g:
storeInDb(
SignUpSingleton.getInstance().getEmailOrPhone(),
SignUpSingleton.getInstance().getUsername(),
SignUpSingleton.getInstance().getPassword(),
SignUpSingleton.getInstance().getFirstName(),
SignUpSingleton.getInstance().getLastName(),
//remaining params here
);
Assuming you have a method called storeInDb or alike, and inside it the database code.
Please use my code i am sure to help you
i am giving demo class of three activity
is this your FirstActivity
public class FirstActivity extends Activity
{
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main);
}
public void onResume()
{
super.onResume();
//put this code as per your requirement
// i am just giving idea
Intent i=new Intent(this,SecondActivity.class);
i.putExtra("name","Riyaz Parasara");
i.putExtra("email","riyazparasara#gmail.com");
i.putExtra("phone","+918955094537");
i.putExtra("country","india");
startActivity(i);
}
}
is this your secondActivity
public class SecondActivity extends Activity
{
private String name,email,phone,county;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main);
//this is first activity data
//you can get firstactivity data in second activity
//and store data into varialbles
name=getIntent().getStringExtra("name");
email=getIntent().getStringExtra("email");
phone=getIntent().getStringExtra("phone");
country=getIntent().getStringExtra("country");
}
public void onResume()
{
super.onResume();
//put this code as per your requirement
// i am just giving idea
Intent i=new Intent(this,ThirdActivity.class);
//this is first activity data put in intent
i.putExtra("name",name);
i.putExtra("email",email);
i.putExtra("phone",phone);
i.putExtra("country",country);
//this is second activity data you also put on this intent
i.putExtra("sex","male");
i.putExtra("age","24");
i.putExtra("city","jaipur");
startActivity(i);
}
}
is this your FinalActivity please read code comments carefully
public class FinalActivity extends Activity
{
private String name,email,phone,county,sex,age,city;
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.main);
//this is first activity data and second activity data
//you can get firstactivity and secondactivity data in Final activity
//and store data into varialbles
name=getIntent().getStringExtra("name");
email=getIntent().getStringExtra("email");
phone=getIntent().getStringExtra("phone");
country=getIntent().getStringExtra("country");
sex=getIntent().getStringExtra("sex");
age=getIntent().getStringExtra("age");
city=getIntent().getStringExtra("city");
//all data are in instance variable please use this data whenever
}
public void onResume()
{
super.onResume();
//if you need to send data another activity
//please repeat again previous steps as per your requirement
}
}
Use a POJO class User, for instance, which will have all the attributes(name, email, location to name a few) you're likely be needing to complete the Sign up process.
Pass and update this User class object in every activity. The User class needs to implement Serializable or Percelable in order to be passable through Intent though.
public class User implements Serilizable {
private String usename;
private String email;
private Location location;
// Other attributes
// Getters and Setters
}

Categories

Resources