0

@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);

    binding = FragmentAlarmBinding.inflate(getLayoutInflater());
    return binding.getRoot();
    createNotificationChannel(); //problem is here. The name of the problem "Unreachable statement"



    binding.cancelAlarmBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancelAlarm();
        }
    });
}
  • You return a `View` object on line 40, no logic beyond can be reached. – MrDetail Feb 02 '23 at 19:42
  • what does at mean? I don't understand sorry its my fault – Nisanur Turan Feb 02 '23 at 20:03
  • What @MrDetail means is your function ```onCreateView()``` will just run until reaching the ```return``` line. And the code between Line 41-51 will not be running. And you may refer to [this](https://stackoverflow.com/questions/34706399/how-to-use-data-binding-with-fragment) for a proper way to do it. – Enowneb Feb 03 '23 at 08:32
  • But please edit your question and do not post image for code and error in your question. See [reason why](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors). – Enowneb Feb 03 '23 at 08:33
  • Thank you so much sir for your help. I will consider your suggestions – Nisanur Turan Feb 03 '23 at 19:04
  • i want to ask question again i tried the directions but now i have different error can you solve it ? – Nisanur Turan Feb 03 '23 at 19:57
  • Can you edit your question based on your update? – Enowneb Feb 03 '23 at 20:21

1 Answers1

1

So you have to familiarize yourself with what a return keyword does in a method. You can go through this article.

In Java, the return keyword returns a value from a method. The method will return the value immediately when the keyword is encountered. This means that the method will not execute any more statements beyond the return keyword, and any local variables created in the method will be discarded.

So back to your case, you have the following code:

@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
    binding = FragmentAlarmBinding.inflate(getLayoutInflater());
    return binding.getRoot();
    createNotificationChannel(); //problem is here. The name of the problem "Unreachable statement"

    binding.cancelAlarmBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancelAlarm();
        }
    });
}

You are having return binding.getRoot(); in the second line of your method. And that is already an ending point of your method. All the remaining lines you have within the method will not be executed. And hence that's why Unreachable statement will be prompted.

So the correct code for onCreateView() function should look like this:

@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
    binding = FragmentAlarmBinding.inflate(getLayoutInflater());
    return binding.getRoot();
}

Then you may wonder how to deal with the remaining lines of your method? So for a Fragment, after the View is being loaded with onCreateView(), onViewCreated() will be called and you can do your next action there. So you should implement another override method onViewCreated():

@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
    binding = FragmentAlarmBinding.inflate(getLayoutInflater());
    return binding.getRoot();
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    createNotificationChannel();

    binding.cancelAlarmBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancelAlarm();
        }
    });
}
Enowneb
  • 943
  • 1
  • 2
  • 11