class data returning null after being set - java

I have a class that I assign string values through standard get/set methods. After setting the values it returns them as expected. When I call the class values in another fragment they return null. I can't seem to figure it out.
How I set up the class and set the values.
public class BasicInfo_Assets_Fragment extends Fragment {
View view
public static Store_Model store_model = null;
...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_basic_info, container, false);
...
zipCode = zip.getText().toString();
// set info
store_model = new Store_Model();
store_model.setZip(zipCode);
Log.v("STORE" , "Zip: " + store_model.getZip());
...
The Log.v prints out: "Zip: 47564" exactly as expected. However when I call the class in another fragment like this it gives me an error.
public class Options_ListFragment extends ListFragment {
View view;
public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;
....
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_options, container, false);
Log.v("STORE", "Zip: " + store_model.getZip()); <!--- ERROR
As noted in the code, trying to pull that value is giving me a null pointer exception. I do not understand how this class is losing its values. Maybe I just need another pair of eyes. Thanks for the help in advance
Edit
Thanks #Juan Jose Fidalgo and #SJuan76. I figured it out and I changed my code to the following in Options_ListFragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_options, container, false);
if (BasicInfo_Assets_Fragment.store_model != null) {
LinearLayout ll = (LinearLayout) view
.findViewById(R.id.assets_store_info);
ll.setVisibility(View.VISIBLE);
store = (TextView) view.findViewById(R.id.store);
phone = (TextView) view.findViewById(R.id.phone);
address = (TextView) view.findViewById(R.id.address);
city = (TextView) view.findViewById(R.id.city);
zip = (TextView) view.findViewById(R.id.zip);
state = (TextView) view.findViewById(R.id.state);
getStoreInfo();
}
public void getStoreInfo() {
String mStore = BasicInfo_Assets_Fragment.store_model.getStoreNum();
String mPhone = BasicInfo_Assets_Fragment.store_model.getPhoneNum();
String mAddress = BasicInfo_Assets_Fragment.store_model.getAddress();
String mCity = BasicInfo_Assets_Fragment.store_model.getCity();
String mZip = BasicInfo_Assets_Fragment.store_model.getZip();
String mState = BasicInfo_Assets_Fragment.store_model.getState();
store.setText("Store #: " + mStore);
phone.setText("Phone #: " + mPhone);
address.setText("Address: " + mAddress);
city.setText("City: " + mCity);
zip.setText("Zip: " + mZip);
state.setText("State: " + mState);
}

You are creating two variables. One as attribute in BasicInfo_Assets_Fragment, the other as attribute in Option_ListFragment. They are completely different variables.
If you have a reference to the BasicInfo_Assets_Fragment instance (lets call it biaf) in the Option_ListFragment, you can reference its view attribute as biaf.storeModel, and that will give what you want.
Best practice is to make storeModel private and add a getStoreModel() method to the BasicInfo_Assets_Fragment. Also, try to follow Java naming conventions.

The fragment
Options_ListFragment
public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;
is executed before than
BasicInfo_Assets_Fragment.onCreateView(...)
For this reason, when,
public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;
is executed, the variable
BasicInfo_Assets_Fragment.store_model
contains null value. So,
Options_ListFragment
public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;
get a null value.
The fragment
Options_ListFragment
public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;
is executed when the class Options_ListFragment is loaded by ClassLoader the first time that the class Options_ListFragment is invoked in any place of your code. You should look when Options_ListFragment is invoked.

Related

Android - Accessing views in a fragment

I have a navigation drawer activity where i am attaching different fragments
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, rp_fragment.OnFragmentInteractionListener, sensors_fragment.OnFragmentInteractionListener, stats_fragment.OnFragmentInteractionListener {
I'm trying to modify the text property of a textview contained within a fragment but i keep getting a null pointer exception on the view (java.lang.NullPointerException: Attempt to invoke virtual method 'void com.teicrete.alucard.sempi.sensors_fragment.ctv_tvst(int)' on a null object reference).
Mainactivity:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (id == R.id.nav_camera) {
ft.replace(R.id.fragment_placeholder, new rp_fragment());
ft.commit();
} else if (id == R.id.nav_gallery) {
ft.replace(R.id.fragment_placeholder, new sensors_fragment());
ft.commit();
gentempdata();
sensors_fragment sf = (sensors_fragment)fm.findFragmentById(R.id.fragment_sensors);
sf.ctv_tvst(1);
Fragment:
public class sensors_fragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
TextView ctv_tv;
TextView otv_tv;
View view;
...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors, container, false);
TextView ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
...
public void ctv_tvst(int mtext) {
ctv_tv.setText(mtext);
}
I've looked at similar posts here but i was not able to resolve my issue. The only place where i am able to modify the textview is within the onCreateView of the fragment (see code above). If i try to do it from the mainactivity or anywhere else i get the null pointer error.
Any insights?
Edit: Please see my response in android_griezmann's post.
For now, i am doing everything that i need to do, inside the fragment classes themselves and then i load them. I would still like to figure out why i can't access its methods or views externally.
Change those lines from onCreateView:
TextView ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)view.findViewById(R.id.otv_tv);
To:
ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
otv_tv = (TextView)view.findViewById(R.id.otv_tv);
Currently you are assigning reference to new object by TextView ctv_tv but actually you want this reference to be assigned to class field so you don't have to add TextView.
Your problem is Fragment itself not the TextView. You are getting fragment object as null not the textview.
So try to find the fragment like the below.
if (id == R.id.nav_gallery) {
ft.replace(R.id.fragment_placeholder, new sensors_fragment(),"YOUR_TAG_NAME");
ft.commit();
gentempdata();
sensors_fragment sf = (sensors_fragment) fm.findFragmentByTag("YOUR_TAG_NAME");
sf.ctv_tvst(1);
}
Also change this method too!
public void ctv_tvst(int mtext) {
ctv_tv.setText("" + mtext);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView ctv_tv = (TextView)getView().findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)getView().findViewById(R.id.otv_tv);
}
override onactivity created, and then refer your views.. Infact it is the best place to get reference to your views withot getting NullPointer Exception
If you want to pass data while replacing or adding then pass the data in your fragment constructor like below
else if (id == R.id.nav_gallery) {
ft.replace(R.id.fragment_placeholder, new sensors_fragment("your data"));
ft.commit();
Then in your fragmet change the textview value based on the constructor's data
Here is the problem. Just remove the TextView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors, container, false);
**TextView** ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
**TextView** otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
Replace the following code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors,container, false);
TextView ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
TextView otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
with
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sensors,container, false);
ctv_tv = (TextView)view.findViewById(R.id.ctv_tv);
otv_tv = (TextView)view.findViewById(R.id.otv_tv);
//ctv_tv.setText("Test"); Only this works
return view;
}
Create a variable String ctv_tv_string and assign the value in ctv_tvst() method do not assign to the TextView. it would not throw an error as you are not referring any ui element after this in oncreateview ctv_tv.setText(ctv_tv_string)

String doesn't update when passed to different Fragment?

I'm trying to send two strings from Fragment A to Fragment B. Currently, i've implemented an interface listener like this...
Fragment A Method:
String arrayTitle1 = arrayList.get(Index).get("ArrayTitle1");
String arrayTitle2 = arrayList.get(Index).get("ArrayTitle2");
((TextFooterListener) getActivity()).onTextFooterListener(arrayTitle1, arrayTitle2);
I pass these strings to the Activity which sends them to Fragment B like this...
Activity:
#Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
FragmentB.arrayTitle1 = arrayTitle1;
FragmentB.arrayTitle2 = arrayTitle2;
}
Fragment B:
They are then received and stored in Fragment B as public strings.
public String arrayTitle1;
public String arrayTitle2;
And in the onCreateView of Fragment B, i try to assign these strings.
arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);
But unfortunately, whenever Fragment A's method gets called, the onTextFooterListener doesn't seem to update the strings.
Any suggestions?
You change the values in Fragment B but never actually reassign the new values into the TextViews.
Change
#Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
FragmentB.setTitles(arrayTitle1, arrayTitle2);
}
And in Fragment B :
public void setTitles(String title1, String title2) {
arrayTitle1 = title1;
arrayTitle2 = title2;
arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);
}
There is a mechanism you need to use to pass data to a Fragment. Please consider the following approach:
#Override
public void onTextFooterListener(String arrayTitle1, String arrayTitle2) {
Bundle data = new Bundle();
data.putString("array1", arrayTitle1);
data.putString("array2", arrayTitle2);
FragmentB b = FragmentB();
b.setArguments(data);
}
Then in your onCreateView of FragmentB:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
arrayTitle1 = getArguments().getString("array1");
arrayTitle2 = getArguments().getString("array2");
arrayTitle1Footer.setText(arrayTitle1);
arrayTitle2Footer.setText(arrayTitle2);
return inflater.inflate(R.layout.fragment, container, false);
}

Attempt to invoke virtual method on a null object reference

This is the error i get:
03-11 08:27:48.513: E/AndroidRuntime(23647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.plan.yeahimin/com.plan.yeahimin.PlanDetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference
I understand its due to a variable having a null value but I can't workout why. It looks like it's 'EXTRA_NEW_PLAN' in the getSerializable() method in the DetailsFragment but other than that I don't know. I'm new to Android so forgive me if it's obvious but any help would be greatly appreciated.
Here is my code for the ListFragment;
public class PlanListFragment extends ListFragment {
public final static String TAG = "com.plan.yeahimin.PlanListFragment";
public final static String EXTRA_NEW_PLAN = "com.plan.yeahimin.plan_id";
private Button mAddPlan;
private ArrayList<Plan> mPlansList;
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.empty_view_or_list_view, parent, false);
ListView view = (ListView) v.findViewById(android.R.id.list);
view.setEmptyView(v.findViewById(android.R.id.empty));
mAddPlan = (Button) v.findViewById(R.id.add_a_plan);
mAddPlan.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Log.d(TAG, "add plan clicked");
Plan plan = new Plan();
Log.d(TAG, "new plan created");
PlanArrayList.get(getActivity()).addPlans(plan);
Log.d(TAG, "plan added to mPlansList");
Intent i = new Intent(getActivity(), PlanDetailsActivity.class);
i.putExtra(PlanDetailsFragment.EXTRA_NEW_PLAN, plan.getId());
startActivity(i);
return;
}
});
return v;
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mPlansList = PlanArrayList.get(getActivity()).getPlans();
//ArrayList<Plan> mPlansList = new ArrayList<Plan>();
PlanArrayAdapter paa = new PlanArrayAdapter(mPlansList);
setListAdapter(paa);
}
public class PlanArrayAdapter extends ArrayAdapter<Plan>{
public PlanArrayAdapter(ArrayList<Plan> planList){
super(getActivity(), 0, planList);
}
public View getView(int position, View convertView, ViewGroup parent){
// Get the plan item for this position
Plan plan = getItem(position);
//If layout doesnt exist, inflate one
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.plan_list_fragment, parent, false);
}
TextView planTitle = (TextView) convertView.findViewById(R.id.plan_title);
planTitle.setText(plan.getTitle());
TextView planDate = (TextView) convertView.findViewById(R.id.plan_date);
planDate.setText(plan.getDate().toString());
return convertView;
}
}
}
and here is my code for the DetailsFragment which opens from add button;
public class PlanDetailsFragment extends Fragment {
private static final String TAG = "com.plan.yeahimin.PlanDetailsFragment";
public static final String EXTRA_NEW_PLAN = "com.plan.yeahimin.plan_id";
private EditText mTitleField;
private Button mDateButton;
private Button mTimeButton;
private EditText mLocationField;
private EditText mAttendeesField;
private EditText mDescriptionField;
private Plan mPlan;
private ArrayList<Plan> mPlansList;
public static PlanDetailsFragment newInstance(UUID planId){
Bundle args = new Bundle();
args.putSerializable(EXTRA_NEW_PLAN, planId);
PlanDetailsFragment f = new PlanDetailsFragment();
f.setArguments(args);
Log.d(TAG, "newInstance created");
return f;
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
UUID planId = (UUID)getArguments().getSerializable(EXTRA_NEW_PLAN);
mPlan = PlanArrayList.get(getActivity()).getPlan(planId);
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.plan_details_fragment, parent, false);
mTitleField = (EditText)v.findViewById(R.id.plan_title);
mLocationField = (EditText)v.findViewById(R.id.plan_location);
mAttendeesField = (EditText)v.findViewById(R.id.plan_attendees);
mDescriptionField = (EditText)v.findViewById(R.id.plan_description);
mDateButton = (Button)v.findViewById(R.id.plan_date);
mTimeButton = (Button)v.findViewById(R.id.plan_time);
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.main_to_do, menu);
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.save_button:
Log.d(TAG, "save button pressed");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I think you cannot send any arguments to your Fragment with .newInstance(), because this method does not accept any parameters according to the documentation. So even if you have overloaded .newInstance(UUID), the system calls .newInstance() (if calls at all, I have some doubts). Also please be aware that you put the parameter to Intent with .putExtra(), but do not recall it from the Intent.
In fact the right way to send arguments to a Fragment is as follows:
In the caller (usually it is an Activity, but maybe with another Fragment, like in your example, it would also work, I cannot say for sure):
PlanDetailsFragment fragment = new PlanDetailsFragment();
Bundle args = new Bundle();
args.putSerializable(PlanDetailsFragment.TAG_NEW_PLAN, plan.getID());
fragment.setArguments(args);
getFragmentManager().beginTransaction().add(fragment, TAG_DETAILS_FRAGMENT).commit();
In the fragment:
Bundle args = getArguments();
UUID planID = (UUID) args.getSerializable(TAG_NEW_PLAN);
It is not a ready-to-use code, you should adapt it to your classes and variables names, to where your tags are places, etc. The calls of Activity methods may also require some change if you prefer to work from another fragment. It is just an overall description.
My answer applies to the situation when both fragments are inside one activity. Your using of Intents make me have doubts in this, but I do not fully understand it.
You sent the arguments to an Activity(PlanDetailsActivity).
You should send the arguments to a Fragment through newInstance() method.
In your PlainDetailsActivity, you should create the fragment instance like:
UUID uuid = getIntent().getSerializableExtra(PlanDetailsFragment.EXTRA_NEW_PLAN);
PlanDetailsFragment f = PlanDetailsFragment.newInstance(uuid);

Android fragment onCreateView() never called

I've been searching for a while now, but I can't seem to find an answer to the following question:
Why does onCreateView never get triggered? This code is the code generated by Android Studio when creating a new MainActivity plus some of the code I wrote.
I want to use setTextConfig(...,...,...) to set the text in the text fields. But I can't seem to get them using findviewbyid(R.id....); because v is always null, even getView(); returns null.
public static class PlaceholderFragment extends Fragment {
private View v;
private EditText etCustomerName, etDeviceID;
private Spinner select;
public PlaceholderFragment() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_main, container, false);
return v;
}
public void setTextConfig(String customerName, String selectedOption, int deviceID){
View vv = getView();
//init form controls
etCustomerName = (EditText)vv.findViewById(R.id.etCustomerName);
etDeviceID = (EditText)v.findViewById(R.id.etDeviceId);
select = (Spinner)v.findViewById(R.id.select);
etCustomerName.setText(customerName);
ArrayAdapter adapter = (ArrayAdapter)select.getAdapter();
int index = adapter.getPosition(selectedOption);
select.setSelection(index);
etDeviceID = (EditText)getView().findViewById(R.id.etDeviceId);
etDeviceID.setText(Integer.toString(deviceID), TextView.BufferType.EDITABLE);
}
}
If you need more detailed info, just ask :)

Saving fragmentstate with savedInstanceState

saveInstanceState() doesn't work as expected.
I'm trying to read the data so I don't have to do the query again when a user reselects the tab. The fragment is a tab in a actionbar. Everything works properly, I just can't get savedInstanceState to be anything else then NULL, anyone has any idea?
This is my code:
public class SpecsFragment extends Fragment {
private String mText;
private ArrayList<HashMap> result;
private Converter c;
private View fragView;
public SpecsFragment() {
mText = "specs";
c = new Converter();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragView = inflater.inflate(R.layout.product_info_fragment, container, false);
return fragView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState == null) {
Bundle args = getArguments();
int id = Integer.parseInt(args.getString("id"));
String query = "SELECT * FROM products WHERE id = " + id;
try {
ZoekQueryTask zoekQueryTask = new ZoekQueryTask(query);
result = zoekQueryTask.getResults();
}
catch (Exception e) {
Log.e("Afgevangen error", e.getMessage());
}
}
else {
result = new ArrayList();
result.add((c.BundleToHashMap(savedInstanceState)));
}
TextView text = (TextView) fragView.findViewById(R.id.textView1);
text.setText(mText);
text.append("\n\n");
text.append(result.get(0).get("ean").toString());
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState = c.HashmapToBundle(result.get(0));
}
}
Using
savedInstance.putAll(c.HashmapToBundle(result.get(0)));
Still does not work.
savedInstance.putBundle("results", c.HashmapToBundle(result.get(0)));
Doesn't work either, anyone have any idea?
I solved the problem by working around it, instead of trying to store savedInstanceState. I wrote functions to get and set a Bundle from the fragments parent activity, and then get the activity by using:
YourActivity parent = (YourActivity) this.getactivity();
And just call the function for getting and setting a bundle which you write yourself.
I solved the problem by working around it, instead of trying to store savedInstanceState. I wrote functions to get and set a Bundle from the fragments parent activity, and then get the activity by using:
YourActivity parent = (YourActivity) this.getactivity();
And just call the function for getting and setting a bundle which you write yourself
If your Fragment is defined in an XML layout, ensure that you have specified an android:id!

Categories