So I have an app with a TabLayout. In there, I created 2 tabs (fragments).My first fragment is OngletCours and the 2nd one OngletNotes.
I have a ListView in the first tab/fragment. I would like to transfer the Item I clicked in that ListView to the 2nd Fragment to a TextView.
I have already tried something but I get a NullPointerException at line 23:
03-06 09:38:32.874 23677-23677/com.example.dasilvadd.students E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.example.dasilvadd.students.OngletNotes.onCreateView(OngletNotes.java:23)
here's the code from my 1st tab OngletCours (Only the setOnItemClickListener):
final ListView l1 = (ListView) rootView.findViewById(R.id.ListCours);
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) {
l1.getItemAtPosition(i);
//Put the value
OngletNotes onotes = new OngletNotes();
Bundle args = new Bundle();
args.putString("Item","l1.getItemAtPosition(i)");
onotes.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, onotes).commit();
}
((Onglets)getActivity()).goToFragment(1);
}
});
return rootView;
}
Here's the code from my 2nd tab OngletNotes :
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);
//Retrieve the value
//THE ERROR IS HERE !
String cours = getArguments().getString("Item");
Cours.setText(cours);
return rootView;
}
public static OngletNotes newInstance() {
OngletNotes fragment = new OngletNotes();
return fragment;
}
So, I don't know how to transfer the selected Item into the 2nd tab. Do I have to create methodes in my TabbedActivity called Onglets ? Please tell me if you need the code for that class as well.
Thank you in advance.
I think you got a bit confused in your logic. You are checking for null and then not doing anything if the fragment is null.
You can simplify it like this: First check if fragment is null , if so create a new instance, then put the arguments outside the if/else so you make sure you get them every time. Also put your string item in a variable.
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
OngletNotes fragment = ((Onglets)getActivity()).getOngletNotes ();
if(fragment == null) {
fragment = OngletNotes.getInstance();
}
String item = adapterView.getItemAtPosition(i).toString();
//Put the value
Bundle args = new Bundle();
args.putString("Item",item);
fragment.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.