I'm new to android. And I'm trying to manipulate the TextView from aother class out of the GUI class which is the MainActivity class. But it throws an exception.
Following is the code i used...
Context ctx;
private SharedPreferences pref;
LayoutInflater inflater = LayoutInflater.from(ctx);
View myView = inflater.inflate(R.layout.activity_main, null);
TextView tv_datePick = (TextView) myView.findViewById(R.id.pickDate);
tv_datePick.setText(pref.getString(Constants.SELECTED_DATE,""));
I'm getting a date stored in SharedPreference
and setting it to the TextView
Following is the Exception i get..
FATAL EXCEPTION: main
Process: srt.com.srtnew_nisal, PID: 25351
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:219)
at srt.com.srtnew_nisal.DatePickerFragment.onDateSet(DatePickerFragment.java:61)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:134)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5290)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
Following is the full code of the class that I'm using the LayoutInflator
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
Context ctx;
private SharedPreferences pref;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
pref = this.getActivity().getSharedPreferences("Users",0);
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String new_month = "";
if(Integer.parseInt(String.valueOf(month))<10) {
new_month="0"+String.valueOf(month);
}else{
new_month = String.valueOf(month);
}
Log.d("month new",year+"-"+new_month+"-"+dayOfMonth);
SharedPreferences.Editor editor = pref.edit();
editor.putString(Constants.SELECTED_DATE,year+"-"+new_month+"-"+dayOfMonth);
editor.apply();
Log.d("shared pref",pref.getString(Constants.SELECTED_DATE,""));
LayoutInflater inflater = LayoutInflater.from(this);
View myView = inflater.inflate(R.layout.activity_main, null);
TextView tv_datePick = (TextView) myView.findViewById(R.id.pickDate);
tv_datePick.setText(pref.getString(Constants.SELECTED_DATE,""));
}
}
What have i done wrong here? Am I doing the right way?