1
public class AddItemDialog extends DialogFragment implements View.OnClickListener {
Button addButton, cancelButton;
EditText showEditText, seasonEditText, episodeEditText;
Communicator communicator;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    communicator= (Communicator) context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.add_item_layout, null);
    addButton = (Button) view.findViewById(R.id.add_button);
    cancelButton = (Button) view.findViewById(R.id.cancel_add_button);
    showEditText = (EditText) view.findViewById(R.id.show_edit);
    seasonEditText = (EditText) view.findViewById(R.id.season_edit);
    episodeEditText = (EditText) view.findViewById(R.id.episode_edit);
    addButton.setOnClickListener(this);
    cancelButton.setOnClickListener(this);
    setCancelable(false);
    return view;
}

@Override
public void onClick(View view) {
    long mseconds = System.currentTimeMillis();

    if(view.getId()==R.id.add_button){
        communicator.yesOnAddClick(showEditText.getText().toString(), Integer.parseInt(seasonEditText.getText().toString()),Integer.parseInt(episodeEditText.getText().toString()), mseconds);
        dismiss();
    } else if(view.getId()==R.id.cancel_add_button){
        dismiss();
    }
}

interface Communicator {
    public void yesOnAddClick(String show, int season, int episode, long dateId);
}

The code works on a device emulator but not on an actual device. What is happenning?

I think I initialized everything properly so it should work. Plus the fact that it runs on a marshmallow emulator but not on an actual device. I've been testing it in my tablet which is Kitkat. Could be the version are at play here?

Error log

FATAL EXCEPTION: main Process: com.leviebora.showandepisodetracker, PID: 27412 java.lang.NullPointerException at com.leviebora.showandepisodetracker.AddItemDialog.onClick(AddItemDialog.java:49) at android.view.View.performClick(View.java:4445) at android.view.View$PerformClick.run(View.java:18429) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5013) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)

here is the error log. It says nullpointerexception on the onclick dialog

3 Answers3

5

onAttach(Context) was only added in Marshmallow. Thus it's never called in versions before and communicator never set. You need to work with onAttach(Activity) there. At the moment it would be enough to only overwrite the latter, because the former calls it anyway.

tynn
  • 38,113
  • 8
  • 108
  • 143
1

extended answer of @tynn's answer.

If the fragment requires the parent activity to implement the Communicator interface you can make it more clear also, like this:

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof Communicator) {
            communicator= (Communicator) activity;
        } else {
            throw new RuntimeException("Parent activity must implement Communicator interface");
        }
    }
malmling
  • 2,398
  • 4
  • 19
  • 33
1

Please use this

@Override
public void onAttach(Context context) {
  super.onAttach(context);
  Activity a;
  if (context instanceof Activity) {
      a = (Activity) context;
      communicator = (Communicator) a;
  }
}
Kaushik
  • 6,150
  • 5
  • 39
  • 54
Vishnu V S
  • 355
  • 1
  • 10