0
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.

  • Please post the stacktrace and show exactly what line causes the error. – Code-Apprentice Feb 13 '17 at 00:36
  • @Code-Apprentice updated – Charlotte A.Wilson Feb 13 '17 at 00:42
  • The error is telling you that `mcallBack` is `null`. You should find out why it has not been initialized and fix the problem. – Code-Apprentice Feb 13 '17 at 00:43
  • `// let the dbmanager take care of params` Then why have the params in the first place? – Code-Apprentice Feb 13 '17 at 00:44
  • Most likely, you should create the `DBManager` and set up the adapter for the `ListView` somewhere else, not in the `OnClickListener`. Then you only have to add the data to the database. If you do everything correctly, the data will automatically appear in the `ListView` without any effort on your part. To help you, I suggest you learn about `CursorAdapter`. – Code-Apprentice Feb 13 '17 at 00:46
  • Are you suggesting I put the adaptor in the DBManager? @Code-Apprentice – Charlotte A.Wilson Feb 13 '17 at 00:50
  • No. You should use the `DBManager` to create a `Cursor` that is used by a `CursorAdapter`. Then all the button does is add the data directly to the database using the `DBManager` (which is created outside the `OnClickListener` but still available there). When you do this, the `CursorAdapater` will automatically detect the change to the database and update the `ListView`. – Code-Apprentice Feb 13 '17 at 00:53

0 Answers0