Issue
I have a problem passing the selected Item of my ListView to another fragment.
I have a TabbedActivity with 2 tabs. 1st is called OngletCours and the 2nd is called OngletNotes.
I'm getting an error while passing the Item I clicked on.
I have tried the whole weekend but without sucess to transfer the Item I clicked on to the 2nd tab/fragment.
Here is the code from my 1st Fragment/Tab OngletCours (I'm only showing you the setOnItemClickListener :
l1.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
OngletNotes fragment = ((Onglets)getActivity()).getOngletNotes();
if(fragment == null) {
fragment = OngletNotes.newInstance();
}
//récupération de la position convertie en String de l'item que j'ai choisi
String item = l1.getItemAtPosition(i).toString();
Bundle args = new Bundle();
args.putString("Item",item);
fragment.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, fragment).addToBackStack(null).commit();
((Onglets)getActivity()).goToFragment(1);
}
});
My 2nd tab/Fragment OngletNotes looks like this :
public class OngletNotes extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.ongletnotes, container, false);
//where i want to insert the selectedItem
TextView Cours = (TextView)rootView.findViewById(R.id.TVCours);
Bundle bundle=getArguments();
String cours="";
//ERROR !
cours = bundle.getString("Item");
//Retrieve the value
Cours.setText(cours);
return rootView;
}
public static OngletNotes newInstance() {
OngletNotes fragment = new OngletNotes();
// put values which you want to pass to fragment
// Bundle args = new Bundle();
// fragment.setArguments(args);
return fragment;
}
I have the following error :
03-06 12:48:13.959 1033-1033/com.example.dasilvadd.students E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.example.dasilvadd.students.OngletNotes.onCreateView(OngletNotes.java:23)
Line 23 is the following one :
Bundle bundle=getArguments();
Please help me solving this, I really need to advance in my project. Thank you in advance !
Solution
Use shared preferences, create a shared preference in OngletCours then read from in OngletNotes. there is a single instance of this class that all clients share , so in this case it makes sense.Go to this link to refresh it code syntax.https://developer.android.com/training/basics/data-storage/shared-preferences.html
hey just remember this for future purposes, serialize your data whenever your store it. a great library is gson. Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.Just something to think about still.
Answered By - Remario
Answer Checked By - Mary Flores (JavaFixing Volunteer)