public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.fragment_create_new_goal, null);
// TODO: change theme of overlay
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set custom_row inflater
builder.setMessage("Input Goal Info");
builder.setView(v)
// Action Buttons
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DbManager db;
db = new DbManager(getActivity(), null, null, 2); // let the dbmanager take care of params
goalNameInput = (EditText) v.findViewById(R.id.goal_form);
stepsInput = (EditText) v.findViewById(R.id.goal_step_form);
Goals goal = new Goals();
goal.setName(goalNameInput.getText().toString());
goal.setStepTarget(Integer.valueOf(stepsInput.getText().toString()));
// now check toggle thingy
goal.setActive(true);
goal.setComplete(false);
goal.setNumSteps(0);
db.addGoal(goal);
// Need to add update list somehow..
mcallBack.updateList();
dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
The line with mcallBack.updateList(); is the point where I get the error described in my title.
The updateList method is used in another fragment to update a list with the new information from the first dialog fragment.
I am updating the list using a cursorAdapter like so:
public void populateListView(Context context) {
ListView myList;
SimpleCursorAdapter myCursorAdapter;
DbManager db = new DbManager(context, null, null, 2);
Cursor cursor = db.getAllRows();
String[] fromFieldNames = new String[]{
DbManager.COLUMN_GOAL_NAME, DbManager.COLUMN_STEP_GOALS}; // Placeholder
int[] toViewIDs = new int[]{R.id.goal_name, R.id.current_progress_added}; // Placeholder
// Set up the adapter
myCursorAdapter = new SimpleCursorAdapter(context, R.layout.custom_row, cursor, fromFieldNames, toViewIDs, 0);
myList = (ListView) v.findViewById(R.id.list_goals);
myList.setAdapter(myCursorAdapter);
}
I call populateListView from the onCreateView of the fragment showing the list.
The thing I aim to do here is to add a new goal from the dialog fragment, update the db with it and then refresh the listview to show the new updates.
Not sure I am going about it the correct way or why I am getting this error.
StackTrace:
--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.charl.walkthisway, PID: 2347 java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.charl.walkthisway.CreateNewGoal$CreateNewGoalDialogListener.updateList()' on a null object reference at com.example.charl.walkthisway.CreateNewGoal$2.onClick(CreateNewGoal.java:110) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
The line with mcallBack.updateList(); is the point where I get the error described in my title.