-2

I am getting Error while doing this

this is my code

String[] options = {getActivity().getResources().getString(R.string.capture_video),getActivity().getResources().getString(R.string.Cancel)};

This is in Fragment class

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You try to access `getActivity()` before attaching your `fragment` to `activity`. Try to initialize it inside `onCreateView` or `onViewCreated` – Md. Asaduzzaman Feb 11 '20 at 12:04
  • https://stackoverflow.com/questions/14112628/access-to-getstring-in-android-support-v4-app-fragmentpageradapter – Jaydeep Devda Feb 11 '20 at 12:05
  • Can you tell me how to initialize from onCreate()? – Pooja Patwa Feb 11 '20 at 12:20
  • This is my code what I ma trying to do Context context; protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = getApplicationContext();} String[] options = {context.getString(R.string.capture_video)} – Pooja Patwa Feb 11 '20 at 12:38

1 Answers1

0

I hope this will work for you.

Initialize your option variable in onViewCreated() method of fragment

String[] options;

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        options = new String[]{getActivity().getResources().getString(R.string.capture_video), getActivity().getResources().getString(R.string.Cancel)};
        Log.e("option",options[0]);
    }

above code will solve your problem.

GParekar
  • 1,209
  • 1
  • 8
  • 15