Reputation: 6961
i cant figure it out.
When sometimes clicks on an Item in my ListView, i want to open a Fragment and set 1 EditText and 3 RadioButtons according to the values of the list item that was clicked.
public void openEditTask(int position) {
if (newTaskFragment != null) closeNewTaskFragment();
if (editTaskFragment != null) closeEditTaskFragment();
editTaskFragment = new EditTaskFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right);
transaction.replace(R.id.edit_task_frame_xml, editTaskFragment);
//set EditText and RadioButtons according to clicked item
EditText editTaskName = (EditText) findViewById(R.id.edit_task_name_xml);
RadioButton easyButton = (RadioButton) findViewById(R.id.edit_radio_button_easy_xml);
RadioButton mediumButton = (RadioButton) findViewById(R.id.edit_radio_button_medium_xml);
RadioButton hardButton = (RadioButton) findViewById(R.id.edit_radio_button_hard_xml);
//Edit Text
editTaskName.setText(mTaskList.get(position).getName());
//Radio Buttons
if (mTaskList.get(position).getDifficultyNumber() == 1) {
easyButton.isChecked();
} else if (mTaskList.get(position).getDifficultyNumber() == 2) {
mediumButton.isChecked();
} else hardButton.isChecked();
transaction.commit();
}
Why do i get a null pointer exception? I dont understand it.
Upvotes: 0
Views: 68
Reputation: 76
You should handle your instances in the Fragment class. You can set the position as an Argument and get the data from wherever you need and show the view.
Your EditTaskFragment should look something like this:
public class EditTaskFragment extends Fragment {
private static final String POSITION_PARAM = "positionParam";
private int position;
public EditTaskFragment() {
}
public static EditTaskFragment newInstance(int position) {
EditTaskFragment fragment = new EditTaskFragment();
Bundle args = new Bundle();
args.putInt(POSITION_PARAM, position);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
position = getArguments().getInt(POSITION_PARAM);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.yourfragmentlayout, container, false);
EditText editTaskName = (EditText) v.findViewById(R.id.edit_task_name_xml);
RadioButton easyButton = (RadioButton) v.findViewById(R.id.edit_radio_button_easy_xml);
RadioButton mediumButton = (RadioButton) v.findViewById(R.id.edit_radio_button_medium_xml);
RadioButton hardButton = (RadioButton) v.findViewById(R.id.edit_radio_button_hard_xml);
//Edit Text
editTaskName.setText(mTaskList.get(position).getName());
//Radio Buttons
if (mTaskList.get(position).getDifficultyNumber() == 1) {
easyButton.isChecked();
} else if (mTaskList.get(position).getDifficultyNumber() == 2) {
mediumButton.isChecked();
} else
hardButton.isChecked();
return v;
}
}
Then, your openEditTask function should be:
public void openEditTask(int position) {
if (newTaskFragment != null) closeNewTaskFragment();
if (editTaskFragment != null) closeEditTaskFragment();
editTaskFragment = EditTaskFragment.newInstance(position);
transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right);
transaction.replace(R.id.edit_task_frame_xml, editTaskFragment);
transaction.commit();
}
So, you have to look for the data according the position the user selected, in the Fragment class.
I hope this answer your question
Upvotes: 1